ctucx.git: nimhafas

[nimlang] hafas-client library

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
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())