all repos — min @ 2660bf2b92938be12251ff420bb825c93c55a2f2

A small but practical concatenative programming language.

lib/min_time.nim

 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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
import
  times,
  tables
import
  ../core/parser,
  ../core/value,
  ../core/interpreter,
  ../core/utils

# Time


proc time_module*(i: In)=
  let def = i.define()

  def.symbol("timestamp") do (i: In):
    i.push getTime().toUnix().newVal

  def.symbol("now") do (i: In):
    i.push epochTime().newVal

  def.symbol("timeinfo") do (i: In):
    let vals = i.expect("num")
    let t = vals[0]
    var time: Time
    if t.kind == minInt:
      time = t.intVal.fromUnix
    else:
      time = t.floatVal.int64.fromUnix
    let tinfo = time.local
    var info = newDict(i.scope)
    info.objType = "timeinfo"
    i.dset info, "year", tinfo.year.newVal
    i.dset info, "month", (tinfo.month.int+1).newVal
    i.dset info, "day", tinfo.monthday.newVal
    i.dset info, "weekday", (tinfo.weekday.int+1).newVal
    i.dset info, "yearday", tinfo.yearday.newVal
    i.dset info, "hour", tinfo.hour.newVal
    i.dset info, "minute", tinfo.minute.newVal
    i.dset info, "second", tinfo.second.newVal
    i.dset info, "dst", tinfo.isDST.newVal
    i.dset info, "timezone", tinfo.utcOffset.newVal
    i.push info

  def.symbol("to-timestamp") do (i: In):
    let vals = i.expect("dict:timeinfo")
    let dict = vals[0]
    try:
      let year = i.dget(dict, "year").intVal.int
      let month = i.dget(dict, "month").intVal.int - 1
      let monthday = i.dget(dict, "day").intVal.int
      let hour = i.dget(dict, "hour").intVal.int
      let minute = i.dget(dict, "minute").intVal.int
      let second = i.dget(dict, "second").intVal.int
      let dst = i.dget(dict, "dst").boolVal
      let timezone = i.dget(dict, "timezone").intVal.int
      let tinfo = Datetime(year: year, month: Month(month), monthday: monthday, hour: hour, minute: minute, second: second, isDST: dst, utcOffset: timezone)
      i.push tinfo.toTime.toUnix.int.newVal
    except:
      raiseInvalid("An invalid timeinfo dictionary was provided.")

  def.symbol("datetime") do (i: In):
    let vals = i.expect("num")
    let t = vals[0]
    var time: Time
    if t.kind == minInt:
      time = t.intVal.fromUnix
    else:
      time = t.floatVal.int64.fromUnix
    i.push time.utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'").newVal

  def.symbol("tformat") do (i: In):
    let vals = i.expect("string", "num")
    let s = vals[0]
    let t = vals[1]
    var time: Time
    if t.kind == minInt:
      time = t.intVal.fromUnix
    else:
      time = t.floatVal.int64.fromUnix
    i.push time.local.format(s.getString).newVal

  def.finalize("time")