Skip to content

Commit

Permalink
Let the Date constructor try to parse the date string first.
Browse files Browse the repository at this point in the history
`new Date(dateString)` should produce valid date objects from all
datestrings in the google calendar JSON. However, keep around the old
regex-based parsing as a fallback anyway.
  • Loading branch information
torque committed May 24, 2013
1 parent 302953c commit a0f04bd
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions jquery.gcal_flow.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,34 @@ class gCalFlow
}

parse_date: (dstr) ->
if m = dstr.match /^(\d{4})-(\d{2})-(\d{2})$/
return new Date(parseInt(m[1], 10), parseInt(m[2], 10) - 1, parseInt(m[3], 10), 0, 0, 0)

offset = (new Date()).getTimezoneOffset() * 60 * 1000
year = mon = day = null
hour = min = sec = 0
if m = dstr.match /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|([+-])(\d{2}):(\d{2}))$/
year = parseInt m[1], 10
mon = parseInt m[2], 10
day = parseInt m[3], 10
hour = parseInt m[4], 10
min = parseInt m[5], 10
sec = parseInt m[6], 10
if m[7] != "Z"
offset += (if m[8] is "+" then 1 else -1) * (parseInt(m[9], 10) * 60 + parseInt(m[10], 10)) * 1000 * 60
date = new Date dstr
unless isNaN date.getTime()
log.debug date
return date
else
log.warn "Time prase error! Unknown time pattern: #{dstr}"
return new Date(1970, 1, 1, 0, 0, 0)
if m = dstr.match /^(\d{4})-(\d{2})-(\d{2})$/
return new Date parseInt(m[1], 10), parseInt(m[2], 10) - 1, parseInt(m[3], 10), 0, 0, 0

log.debug "time parse (gap to local): #{offset}"
ret = new Date(new Date(year, mon - 1, day, hour, min, sec).getTime() - offset)
log.debug "time parse: #{dstr} -> ", ret
ret
offset = (new Date()).getTimezoneOffset() * 60 * 1000
year = mon = day = null
hour = min = sec = 0
if m = dstr.match /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|([+-])(\d{2}):(\d{2}))$/
year = parseInt m[1], 10
mon = parseInt m[2], 10
day = parseInt m[3], 10
hour = parseInt m[4], 10
min = parseInt m[5], 10
sec = parseInt m[6], 10
if m[7] != "Z"
offset += (if m[8] is "+" then 1 else -1) * (parseInt(m[9], 10) * 60 + parseInt(m[10], 10)) * 1000 * 60
else
log.warn "Time parse error! Unknown time pattern: #{dstr}"
return new Date 1970, 1, 1, 0, 0, 0

log.debug "time parse (gap to local): #{offset}"
ret = new Date(new Date(year, mon - 1, day, hour, min, sec).getTime() - offset)
log.debug "time parse: #{dstr} -> ", ret
return ret

render_data: (data) ->
log.debug "start rendering for data:", data
Expand Down

0 comments on commit a0f04bd

Please sign in to comment.