fix time parsing

Ref #2
This commit is contained in:
Christine Dodrill 2016-02-07 12:48:43 -08:00
parent bdc99a3c5e
commit 23606cd501
6 changed files with 61 additions and 2 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ twtxt
worker worker
nimcache nimcache
*.db *.db
*.so

2
src/time/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
gotime.so
gotime.h

23
src/time/gotime.nim Normal file
View File

@ -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")

27
src/time/time.go Normal file
View File

@ -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() {}

View File

@ -1,4 +1,4 @@
import db_sqlite, json, httpclient, os, strutils, times import db_sqlite, json, httpclient, os, strutils, times, time/gotime
const const
ISOTime*: string = "yyyy-MM-dd'T'HH:mm:ss'.000000'zzz" 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") raise newException(ValueError, "Invalid tweet")
let let
tdate = splitTweet[0].parse(ISOTime) tdate = splitTweet[0].parseTime
message = splitTweet[1] message = splitTweet[1]
Tweet(date: tdate, Tweet(date: tdate,

View File

@ -16,6 +16,12 @@ mode = ScriptMode.Verbose
task db, "SQLite database prompt": task db, "SQLite database prompt":
exec "sqlite3 data/twtxt.db" 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": task run, "Run app":
exec "nimble build" exec "nimble build"
exec "nimble godep"
exec "./twtxtlist" exec "./twtxtlist"