Fixes segfault in db_sqlite. Fixes problems with times.format.

This commit is contained in:
Dominik Picheta 2012-10-07 15:37:12 +01:00
commit 8f3dc6ebd3
2 changed files with 58 additions and 17 deletions

View file

@ -128,8 +128,10 @@ proc GetValue*(db: TDbConn, query: TSqlQuery,
## executes the query and returns the result dataset's the first column ## executes the query and returns the result dataset's the first column
## of the first row. Returns "" if the dataset contains no rows. ## of the first row. Returns "" if the dataset contains no rows.
var stmt = setupQuery(db, query, args) var stmt = setupQuery(db, query, args)
if step(stmt) == SQLITE_ROW: if step(stmt) == SQLITE_ROW:
result = newStringOfCap(column_bytes(stmt, 0)) let cb = column_bytes(stmt, 0)
if cb == 0: return ""
result = newStringOfCap(cb)
add(result, column_text(stmt, 0)) add(result, column_text(stmt, 0))
if finalize(stmt) != SQLITE_OK: dbError(db) if finalize(stmt) != SQLITE_OK: dbError(db)
else: else:

View file

@ -317,9 +317,9 @@ when not defined(ECMAScript):
result.isDST = tm.isDST > 0 result.isDST = tm.isDST > 0
if local: if local:
if result.isDST: if result.isDST:
result.tzname = getTzname()[0] result.tzname = getTzname().DST
if not result.isDST: else:
result.tzname = getTzname()[1] result.tzname = getTzname().nonDST
else: else:
result.tzname = "UTC" result.tzname = "UTC"
@ -538,13 +538,14 @@ proc format*(info: TTimeInfo, f: string): string =
## ========== ================================================================================= ================================================ ## ========== ================================================================================= ================================================
## ##
## Other strings can be inserted by putting them in ``''``. For example ``hh'->'mm`` will give ``01->56``. ## Other strings can be inserted by putting them in ``''``. For example ``hh'->'mm`` will give ``01->56``.
## The following characters can be inserted without quoting them: ``:`` ``-`` ``(`` ``)`` ``/`` ``[`` ``]`` ``,``
result = "" result = ""
var i = 0 var i = 0
var currentF = "" var currentF = ""
while True: while True:
case f[i] case f[i]
of ' ', '-', '/', ':', '\'', '\0': of ' ', '-', '/', ':', '\'', '\0', '(', ')', '[', ']', ',':
case currentF case currentF
of "d": of "d":
result.add($info.monthday) result.add($info.monthday)
@ -557,9 +558,9 @@ proc format*(info: TTimeInfo, f: string): string =
of "dddd": of "dddd":
result.add($info.weekday) result.add($info.weekday)
of "h": of "h":
result.add($(info.hour - 12)) result.add($(if info.hour > 12: info.hour - 12 else: info.hour))
of "hh": of "hh":
let amerHour = info.hour - 12 let amerHour = if info.hour > 12: info.hour - 12 else: info.hour
if amerHour < 10: if amerHour < 10:
result.add('0') result.add('0')
result.add($amerHour) result.add($amerHour)
@ -578,7 +579,7 @@ proc format*(info: TTimeInfo, f: string): string =
of "M": of "M":
result.add($(int(info.month)+1)) result.add($(int(info.month)+1))
of "MM": of "MM":
if int(info.month) < 10: if info.month < mOct:
result.add('0') result.add('0')
result.add($(int(info.month)+1)) result.add($(int(info.month)+1))
of "MMM": of "MMM":
@ -600,22 +601,33 @@ proc format*(info: TTimeInfo, f: string): string =
result.add("PM") result.add("PM")
else: result.add("AM") else: result.add("AM")
of "y": of "y":
var fr = ($info.year).len()-2 var fr = ($info.year).len()-1
if fr < 0: fr = 0 if fr < 0: fr = 0
result.add(($info.year)[fr .. ($info.year).len()-1]) result.add(($info.year)[fr .. ($info.year).len()-1])
of "yy": of "yy":
var fr = ($info.year).len()-2
if fr < 0: fr = 0
var fyear = ($info.year)[fr .. ($info.year).len()-1]
if fyear.len != 2: fyear = repeatChar(2-fyear.len(), '0') & fyear
result.add(fyear)
of "yyy":
var fr = ($info.year).len()-3 var fr = ($info.year).len()-3
if fr < 0: fr = 0 if fr < 0: fr = 0
result.add(($info.year)[fr .. ($info.year).len()-1]) var fyear = ($info.year)[fr .. ($info.year).len()-1]
of "yyy": if fyear.len != 3: fyear = repeatChar(3-fyear.len(), '0') & fyear
result.add(fyear)
of "yyyy":
var fr = ($info.year).len()-4 var fr = ($info.year).len()-4
if fr < 0: fr = 0 if fr < 0: fr = 0
result.add(($info.year)[fr .. ($info.year).len()-1]) var fyear = ($info.year)[fr .. ($info.year).len()-1]
of "yyyy": if fyear.len != 4: fyear = repeatChar(4-fyear.len(), '0') & fyear
result.add($info.year) result.add(fyear)
of "yyyyy": of "yyyyy":
result.add('0') var fr = ($info.year).len()-5
result.add($info.year) if fr < 0: fr = 0
var fyear = ($info.year)[fr .. ($info.year).len()-1]
if fyear.len != 5: fyear = repeatChar(5-fyear.len(), '0') & fyear
result.add(fyear)
of "z": of "z":
let hrs = (info.timezone div 60) div 60 let hrs = (info.timezone div 60) div 60
result.add($hrs) result.add($hrs)
@ -654,3 +666,30 @@ proc format*(info: TTimeInfo, f: string): string =
inc(i) inc(i)
{.pop.} {.pop.}
when isMainModule:
# $ date --date='@2147483647'
# Tue 19 Jan 03:14:07 GMT 2038
var t = getGMTime(TTime(2147483647))
echo t.format("ddd dd MMM hh:mm:ss ZZZ yyyy")
assert t.format("ddd dd MMM hh:mm:ss ZZZ yyyy") == "Tue 19 Jan 03:14:07 UTC 2038"
assert t.format("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
" ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
"19 19 Tue Tuesday 3 03 3 03 14 14 1 01 Jan January 7 07 A AM 8 38 038 2038 02038 0 00 00:00 UTC"
var t2 = getGMTime(TTime(160070789)) # Mon 27 Jan 16:06:29 GMT 1975
assert t2.format("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
" ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
"27 27 Mon Monday 4 04 16 16 6 06 1 01 Jan January 29 29 P PM 5 75 975 1975 01975 0 00 00:00 UTC"
var t3 = getGMTime(TTime(889067643645)) # Fri 7 Jun 19:20:45 BST 30143
assert t3.format("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
" ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
"7 07 Fri Friday 6 06 18 18 20 20 6 06 Jun June 45 45 P PM 3 43 143 0143 30143 0 00 00:00 UTC"
var t4 = getGMTime(TTime(876124714)) # Mon 6 Oct 08:58:34 BST 1997
assert t4.format("M MM MMM MMMM") == "10 10 Oct October"
assert t3.format(":,[]()-/") == ":,[]()-/"