From 23606cd50136fec64d201a4803d1cae7c848b174 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 7 Feb 2016 12:48:43 -0800 Subject: [PATCH] fix time parsing Ref #2 --- .gitignore | 1 + src/time/.gitignore | 2 ++ src/time/gotime.nim | 23 +++++++++++++++++++++++ src/time/time.go | 27 +++++++++++++++++++++++++++ src/twtxt.nim | 4 ++-- twtxtlist.nimble | 6 ++++++ 6 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/time/.gitignore create mode 100644 src/time/gotime.nim create mode 100644 src/time/time.go diff --git a/.gitignore b/.gitignore index a40ae19..22f4eb7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ twtxt worker nimcache *.db +*.so diff --git a/src/time/.gitignore b/src/time/.gitignore new file mode 100644 index 0000000..61a28ca --- /dev/null +++ b/src/time/.gitignore @@ -0,0 +1,2 @@ +gotime.so +gotime.h diff --git a/src/time/gotime.nim b/src/time/gotime.nim new file mode 100644 index 0000000..bf22a25 --- /dev/null +++ b/src/time/gotime.nim @@ -0,0 +1,23 @@ +import times + +proc fixTime(p0: cstring): ptr clonglong {. importc, dynlib: "./gotime.so", noSideEffect .} + +proc parseTime*(inp: string): TimeInfo = + let + cst = inp.cstring + res = cst.fixTime + + if res == nil: + raise newException(ValueError, "Invalid time passed to this thing") + + return res[].fromSeconds.timeToTimeInfo + +when isMainModule: + let myTime = fixTime("2006-01-02T15:04:05.000000-07:00") + + if myTime == nil: + quit "lol failed" + + echo myTime[] + + echo parseTime("2006-01-02T15:04:05-07:00") diff --git a/src/time/time.go b/src/time/time.go new file mode 100644 index 0000000..6869f03 --- /dev/null +++ b/src/time/time.go @@ -0,0 +1,27 @@ +package main + +import ( + "time" +) + +import "C" + +//export fixTime +func fixTime(inp *C.char) *int64 { + gostr := C.GoString(inp) + + t, err := time.Parse("2006-01-02T15:04:05.000000-07:00", gostr) + if err != nil { + t, err = time.Parse("2006-01-02T15:04:05-07:00", gostr) + + if err != nil { + return nil + } + } + + res := t.Unix() + + return &res +} + +func main() {} diff --git a/src/twtxt.nim b/src/twtxt.nim index f373235..87ad532 100644 --- a/src/twtxt.nim +++ b/src/twtxt.nim @@ -1,4 +1,4 @@ -import db_sqlite, json, httpclient, os, strutils, times +import db_sqlite, json, httpclient, os, strutils, times, time/gotime const ISOTime*: string = "yyyy-MM-dd'T'HH:mm:ss'.000000'zzz" @@ -34,7 +34,7 @@ proc parseTweet*(user, inp: string): Tweet = raise newException(ValueError, "Invalid tweet") let - tdate = splitTweet[0].parse(ISOTime) + tdate = splitTweet[0].parseTime message = splitTweet[1] Tweet(date: tdate, diff --git a/twtxtlist.nimble b/twtxtlist.nimble index 586d00e..b04119d 100644 --- a/twtxtlist.nimble +++ b/twtxtlist.nimble @@ -16,6 +16,12 @@ mode = ScriptMode.Verbose task db, "SQLite database prompt": exec "sqlite3 data/twtxt.db" +task godep, "build go deps": + withDir "src/time": + exec "go build -buildmode=c-shared -o gotime.so time.go" + exec "cp gotime.so ../.." + task run, "Run app": exec "nimble build" + exec "nimble godep" exec "./twtxtlist"