Created
March 5, 2013 15:03
-
-
Save jsakamoto/5090911 to your computer and use it in GitHub Desktop.
Setup holidays of .gan file.
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
// download from http://sourceforge.net/projects/dday-ical/files/ | |
//#r @"DDay.Collections.dll" | |
#r "DDay.iCal.dll" | |
#r "System.Xml.Linq" | |
open System | |
open System.IO | |
open System.Linq | |
open System.Xml | |
open System.Xml.Linq | |
open DDay.iCal | |
if fsi.CommandLineArgs.Length < 2 then | |
raise <| new Exception("error: .gan file name not specified.") | |
let Element name (xelem:XElement) = xelem.Element(XName.Get(name)) | |
let Descendants name (xelem:XElement) = xelem.Descendants(XName.Get(name)) | |
let getAttr name (xelem:XElement) = xelem.Attribute(XName.Get(name)).Value | |
let attr name value = new XAttribute(XName.Get(name), value) | |
let path = fsi.CommandLineArgs.[1] | |
let doc = XDocument.Load(path) | |
let calendars = doc.Root |> Element "calendars" | |
let presets = | |
calendars | |
|> Descendants "date" | |
|> Seq.map (fun e -> ((getAttr "year" e), (getAttr "month" e), (getAttr "date" e))) | |
|> Seq.toArray | |
new Uri("http://ical.mac.com/ical/Japanese32Holidays.ics") | |
|> iCalendar.LoadFromUri | |
|> Seq.collect (fun x -> x.Events) | |
|> Seq.map (fun x -> x.Start) | |
|> Seq.map (fun x -> (x.Year.ToString(), x.Month.ToString(), x.Day.ToString())) | |
|> Seq.filter (fun (y1, m1, d1) -> not (presets |> Seq.exists (fun (y2, m2, d2) -> y1 = y2 && m1 = m2 && d1 = d2))) | |
|> Seq.toArray | |
|> Array.iter (fun (y, m, d) -> calendars.Add(new XElement(XName.Get("date"), (attr "year" y), (attr "month" m), (attr "date" d)))) | |
doc.Save(path); | |
printfn "Complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment