oeffisearch/src/backend/hafas/parse/date.nim
2020-02-07 14:09:42 +01:00

32 lines
844 B
Nim

import ../types
import options
import strutils
import times
proc parseDate*(common: CommonData, time: Option[string], tzoffset: Option[int]): Option[int64] =
if time.isNone:
return none(int64)
let tzoffset = tzoffset.get(60) # FIXME: sometimes no time zone is given. how to deal with that?
let date = common.dateStr
var time = time.get
var dayoffset = 0
if time.len == 8:
dayoffset = parseInt(time[0..1])
time = time[2..7]
var tzoffhours = align($(int(tzoffset / 60)), 2, '0')
var tzoffmins = align($(tzoffset mod 60), 2, '0')
var tzoff = tzoffhours & ":" & tzoffmins
if tzoffset >= 0:
tzoff = "+" & tzoff
let datestr = date & time & tzoff
let dateformat = "yyyyMMddHHmmsszzz"
var dt = datestr.parse(dateformat)
dt = dt + initTimeInterval(days = dayoffset)
return some(dt.toTime().toUnix())