all repos — min @ 9cf4f25a7d10a4f60346a79c4d2b12252330a1bd

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
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().int.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.fromSeconds
    else:
      time = t.floatVal.fromSeconds
    let tinfo = time.getLocalTime
    var info = newSeq[MinValue](0).newVal(i.scope)
    info.qVal.add @["year".newSym, tinfo.year.newVal].newVal(i.scope)
    info.qVal.add @["month".newSym, (tinfo.month.int+1).newVal].newVal(i.scope)
    info.qVal.add @["day".newSym, tinfo.monthday.newVal].newVal(i.scope)
    info.qVal.add @["weekday".newSym, (tinfo.weekday.int+1).newVal].newVal(i.scope)
    info.qVal.add @["yearday".newSym, tinfo.yearday.newVal].newVal(i.scope)
    info.qVal.add @["hour".newSym, tinfo.hour.newVal].newVal(i.scope)
    info.qVal.add @["minute".newSym, tinfo.minute.newVal].newVal(i.scope)
    info.qVal.add @["second".newSym, tinfo.second.newVal].newVal(i.scope)
    i.push info

  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.fromSeconds
    else:
      time = t.floatVal.fromSeconds
    i.push time.getLocalTime.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.fromSeconds
    else:
      time = t.floatVal.fromSeconds
    i.push time.getLocalTime.format(s.getString).newVal
  
  def.finalize("time")