Skip to content

Instantly share code, notes, and snippets.

@mizuneko
Last active September 21, 2019 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizuneko/1fa353c01ee82c839d577f28a21bcb10 to your computer and use it in GitHub Desktop.
Save mizuneko/1fa353c01ee82c839d577f28a21bcb10 to your computer and use it in GitHub Desktop.
[日本の祝日取得]内閣府のURLから取得した日本の祝日をDictionaryに格納します
static readonly string URL = @"http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv";
public Dictionary<DateTime, string> GetHolidays()
{
var str = DownloadHolidaysStr(URL);
var dic = new Dictionary<DateTime, string>();
// 行区切りが改行のため、改行毎に分割
string[] rows = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
// 考え得るフォーマットで変換できるようにしておく
string[] formats = { "yyyy/MM/dd", "yyyy-MM-dd", "yyyy/M/d", "yyyy-M-d" };
// タイトル行をSkipして格納
rows.Skip(1).ToList().ForEach(row =>
{
var cols = row.Split(',');
dic.Add(DateTime.ParseExact(cols[0],
formats,
DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.NoCurrentDateDefault), cols[1]);
});
return dic;
}
private string DownloadHolidaysStr(string url)
{
var client = new WebClient();
byte[] buffer = client.DownloadData(url);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
return Encoding.GetEncoding("Shift_JIS").GetString(buffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment