Last active
September 21, 2019 08:28
-
-
Save mizuneko/1fa353c01ee82c839d577f28a21bcb10 to your computer and use it in GitHub Desktop.
[日本の祝日取得]内閣府のURLから取得した日本の祝日をDictionaryに格納します
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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