Skip to content

Commit

Permalink
Merge pull request #1 from jda/master
Browse files Browse the repository at this point in the history
support local and UTC date-time
  • Loading branch information
luxifer authored Jul 11, 2017
2 parents e70b73e + 245a401 commit a0f7feb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
19 changes: 14 additions & 5 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,18 +504,27 @@ func hasProperty(name string, properties []*Property) bool {

// parseDate transform an ical date into a time.Time
func parseDate(prop *Property) (time.Time, error) {
layout := dateTimeLayoutUTC
if tz, ok := prop.Params["TZID"]; ok {
loc, _ := time.LoadLocation(tz.Values[0])
return time.ParseInLocation(dateTimeLayoutLocalized, prop.Value, loc)
}

layout := dateTimeLayoutLocalized

if strings.HasSuffix(prop.Value, "Z") {
return time.Parse(dateTimeLayoutUTC, prop.Value)
}

if len(prop.Value) == 15 {
return time.ParseInLocation(dateTimeLayoutLocalized, prop.Value, time.Local)
}

if val, ok := prop.Params["VALUE"]; ok {
switch val.Values[0] {
case "DATE":
layout = dateLayout
}
}
if tz, ok := prop.Params["TZID"]; ok {
loc, _ := time.LoadLocation(tz.Values[0])
return time.ParseInLocation(dateTimeLayoutLocalized, prop.Value, loc)
}

return time.Parse(layout, prop.Value)
}
24 changes: 23 additions & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,36 @@ var dateList = []*Property{
},
Value: "19980119T020000",
},
// Floating (local) date-time (no time zone)
&Property{
Name: "DSTART",
Params: map[string]*Param{
"VALUE": &Param{
Values: []string{"DATE"},
},
},
Value: "19980119T020000",
},
// Date-time in UTC
&Property{
Name: "DSTART",
Params: map[string]*Param{
"VALUE": &Param{
Values: []string{"DATE"},
},
},
Value: "19980119T070000Z",
},
}

func TestParseDate(t *testing.T) {
for _, prop := range dateList {
_, err := parseDate(prop)
out, err := parseDate(prop)

if err != nil {
t.Error(err)
} else {
t.Logf("in: %+v, out: %s", prop.Value, out)
}
}
}

0 comments on commit a0f7feb

Please sign in to comment.