Removed tzname because it's broken

* No mapping between TimeInfo.tzname and TimeInfo.timezone
 * tzname of time.h is not well-defined, may have almost arbitrary
   length, and localization may differ
 * Code used hardcoded "UTC" string
This commit is contained in:
Felix Krause
2016-11-01 20:26:50 +01:00
parent c1f0b7643c
commit 170745eb39
2 changed files with 14 additions and 44 deletions

View File

@@ -66,12 +66,6 @@ when defined(posix) and not defined(JS):
when not defined(freebsd) and not defined(netbsd) and not defined(openbsd):
var timezone {.importc, header: "<time.h>".}: int
var
tzname {.importc, header: "<time.h>" .}: array[0..1, cstring]
# we also need tzset() to make sure that tzname is initialized
proc tzset() {.importc, header: "<time.h>".}
# calling tzset() implicitly to initialize tzname data.
tzset()
elif defined(windows):
import winlean
@@ -82,12 +76,10 @@ elif defined(windows):
# visual c's c runtime exposes these under a different name
var
timezone {.importc: "_timezone", header: "<time.h>".}: int
tzname {.importc: "_tzname", header: "<time.h>"}: array[0..1, cstring]
else:
type TimeImpl {.importc: "time_t", header: "<time.h>".} = int
var
timezone {.importc, header: "<time.h>".}: int
tzname {.importc, header: "<time.h>" .}: array[0..1, cstring]
type
Time* = distinct TimeImpl
@@ -154,7 +146,6 @@ type
## Always 0 if the target is JS.
isDST*: bool ## Determines whether DST is in effect. Always
## ``False`` if time is UTC.
tzname*: string ## The timezone this time is in. E.g. GMT
timezone*: int ## The offset of the (non-DST) timezone in seconds
## west of UTC.
@@ -237,12 +228,6 @@ proc `==`*(a, b: Time): bool {.
## returns true if ``a == b``, that is if both times represent the same value
result = a - b == 0
when not defined(JS):
proc getTzname*(): tuple[nonDST, DST: string] {.tags: [TimeEffect], raises: [],
benign.}
## returns the local timezone; ``nonDST`` is the name of the local non-DST
## timezone, ``DST`` is the name of the local DST timezone.
proc getTimezone*(): int {.tags: [TimeEffect], raises: [], benign.}
## returns the offset of the local (non-DST) timezone in seconds west of UTC.
@@ -371,7 +356,7 @@ proc `+`*(a: TimeInfo, interval: TimeInterval): TimeInfo =
## very accurate.
let t = toSeconds(toTime(a))
let secs = toSeconds(a, interval)
if a.tzname == "UTC":
if a.timezone == 0:
result = getGMTime(fromSeconds(t + secs))
else:
result = getLocalTime(fromSeconds(t + secs))
@@ -391,7 +376,7 @@ proc `-`*(a: TimeInfo, interval: TimeInterval): TimeInfo =
intval.months = - interval.months
intval.years = - interval.years
let secs = toSeconds(a, intval)
if a.tzname == "UTC":
if a.timezone == 0:
result = getGMTime(fromSeconds(t + secs))
else:
result = getLocalTime(fromSeconds(t + secs))
@@ -491,13 +476,6 @@ when not defined(JS):
weekday: weekDays[int(tm.weekday)],
yearday: int(tm.yearday),
isDST: tm.isdst > 0,
tzname: if local:
if tm.isdst > 0:
getTzname().DST
else:
getTzname().nonDST
else:
"UTC",
timezone: if local: getTimezone() else: 0
)
@@ -597,9 +575,6 @@ when not defined(JS):
## converts a Windows time to a UNIX `Time` (``time_t``)
result = Time((t - epochDiff) div rateDiff)
proc getTzname(): tuple[nonDST, DST: string] =
return ($tzname[0], $tzname[1])
proc getTimezone(): int =
when defined(freebsd) or defined(netbsd) or defined(openbsd):
var a = timec(nil)
@@ -892,8 +867,6 @@ proc formatToken(info: TimeInfo, token: string, buf: var string) =
if hrs.abs < 10:
var atIndex = buf.len-(($hrs & ":00").len-(if hrs < 0: 1 else: 0))
buf.insert("0", atIndex)
of "ZZZ":
buf.add(info.tzname)
of "":
discard
else:
@@ -1154,9 +1127,6 @@ proc parseToken(info: var TimeInfo; token, value: string; j: var int) =
j += 4
info.timezone += factor * value[j..j+1].parseInt() * 60
j += 2
of "ZZZ":
info.tzname = value[j..j+2].toUpperAscii()
j += 3
else:
# Ignore the token and move forward in the value string by the same length
j += token.len

View File

@@ -10,26 +10,26 @@ import
# Tue 19 Jan 03:14:07 GMT 2038
var t = getGMTime(fromSeconds(2147483647))
doAssert t.format("ddd dd MMM hh:mm:ss ZZZ yyyy") == "Tue 19 Jan 03:14:07 UTC 2038"
doAssert t.format("ddd ddMMMhh:mm:ssZZZyyyy") == "Tue 19Jan03:14:07UTC2038"
doAssert t.format("ddd dd MMM hh:mm:ss yyyy") == "Tue 19 Jan 03:14:07 2038"
doAssert t.format("ddd ddMMMhh:mm:ssyyyy") == "Tue 19Jan03:14:072038"
doAssert 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"
" ss t tt y yy yyy yyyy yyyyy z zz 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"
doAssert t.format("yyyyMMddhhmmss") == "20380119031407"
var t2 = getGMTime(fromSeconds(160070789)) # Mon 27 Jan 16:06:29 GMT 1975
doAssert 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"
" ss t tt y yy yyy yyyy yyyyy z zz 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"
when not defined(JS):
when sizeof(Time) == 8:
var t3 = getGMTime(fromSeconds(889067643645)) # Fri 7 Jun 19:20:45 BST 30143
doAssert 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"
" ss t tt y yy yyy yyyy yyyyy z zz 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"
doAssert t3.format(":,[]()-/") == ":,[]()-/"
var t4 = getGMTime(fromSeconds(876124714)) # Mon 6 Oct 08:58:34 BST 1997
@@ -52,22 +52,22 @@ parseTest("Tuesday at 09:04am on Dec 15, 2015",
parseTest("Thu Jan 12 15:04:05 2006", "ddd MMM dd HH:mm:ss yyyy",
"Thu Jan 12 15:04:05 2006", 11)
# UnixDate = "Mon Jan _2 15:04:05 MST 2006"
parseTest("Thu Jan 12 15:04:05 MST 2006", "ddd MMM dd HH:mm:ss ZZZ yyyy",
parseTest("Thu Jan 12 15:04:05 2006", "ddd MMM dd HH:mm:ss yyyy",
"Thu Jan 12 15:04:05 2006", 11)
# RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
parseTest("Mon Feb 29 15:04:05 -07:00 2016", "ddd MMM dd HH:mm:ss zzz yyyy",
"Mon Feb 29 15:04:05 2016", 59) # leap day
# RFC822 = "02 Jan 06 15:04 MST"
parseTest("12 Jan 16 15:04 MST", "dd MMM yy HH:mm ZZZ",
parseTest("12 Jan 16 15:04", "dd MMM yy HH:mm",
"Tue Jan 12 15:04:00 2016", 11)
# RFC822Z = "02 Jan 06 15:04 -0700" # RFC822 with numeric zone
parseTest("01 Mar 16 15:04 -07:00", "dd MMM yy HH:mm zzz",
"Tue Mar 1 15:04:00 2016", 60) # day after february in leap year
# RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
parseTest("Monday, 12-Jan-06 15:04:05 MST", "dddd, dd-MMM-yy HH:mm:ss ZZZ",
parseTest("Monday, 12-Jan-06 15:04:05", "dddd, dd-MMM-yy HH:mm:ss",
"Thu Jan 12 15:04:05 2006", 11)
# RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
parseTest("Sun, 01 Mar 2015 15:04:05 MST", "ddd, dd MMM yyyy HH:mm:ss ZZZ",
parseTest("Sun, 01 Mar 2015 15:04:05", "ddd, dd MMM yyyy HH:mm:ss",
"Sun Mar 1 15:04:05 2015", 59) # day after february in non-leap year
# RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" # RFC1123 with numeric zone
parseTest("Thu, 12 Jan 2006 15:04:05 -07:00", "ddd, dd MMM yyyy HH:mm:ss zzz",