Make getTimezone() to work properly on FreeBSD and NetBSD.
This commit is contained in:
cheatfate 2016-05-30 13:18:31 +03:00
commit b7f2058833

View file

@ -64,8 +64,9 @@ when defined(posix) and not defined(JS):
proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {. proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {.
importc: "gettimeofday", header: "<sys/time.h>".} importc: "gettimeofday", header: "<sys/time.h>".}
when not defined(freebsd) and not defined(netbsd):
var timezone {.importc, header: "<time.h>".}: int
var var
timezone {.importc, header: "<time.h>".}: int
tzname {.importc, header: "<time.h>" .}: array[0..1, cstring] tzname {.importc, header: "<time.h>" .}: array[0..1, cstring]
# we also need tzset() to make sure that tzname is initialized # we also need tzset() to make sure that tzname is initialized
proc tzset() {.importc, header: "<time.h>".} proc tzset() {.importc, header: "<time.h>".}
@ -416,6 +417,7 @@ when not defined(JS):
when not defined(JS): when not defined(JS):
# C wrapper: # C wrapper:
when defined(freebsd) or defined(netbsd):
type type
StructTM {.importc: "struct tm", final.} = object StructTM {.importc: "struct tm", final.} = object
second {.importc: "tm_sec".}, second {.importc: "tm_sec".},
@ -427,7 +429,20 @@ when not defined(JS):
weekday {.importc: "tm_wday".}, weekday {.importc: "tm_wday".},
yearday {.importc: "tm_yday".}, yearday {.importc: "tm_yday".},
isdst {.importc: "tm_isdst".}: cint isdst {.importc: "tm_isdst".}: cint
gmtoff {.importc: "tm_gmtoff".}: clong
else:
type
StructTM {.importc: "struct tm", final.} = object
second {.importc: "tm_sec".},
minute {.importc: "tm_min".},
hour {.importc: "tm_hour".},
monthday {.importc: "tm_mday".},
month {.importc: "tm_mon".},
year {.importc: "tm_year".},
weekday {.importc: "tm_wday".},
yearday {.importc: "tm_yday".},
isdst {.importc: "tm_isdst".}: cint
type
TimeInfoPtr = ptr StructTM TimeInfoPtr = ptr StructTM
Clock {.importc: "clock_t".} = distinct int Clock {.importc: "clock_t".} = distinct int
@ -457,6 +472,28 @@ when not defined(JS):
const const
weekDays: array [0..6, WeekDay] = [ weekDays: array [0..6, WeekDay] = [
dSun, dMon, dTue, dWed, dThu, dFri, dSat] dSun, dMon, dTue, dWed, dThu, dFri, dSat]
when defined(freebsd) or defined(netbsd):
TimeInfo(second: int(tm.second),
minute: int(tm.minute),
hour: int(tm.hour),
monthday: int(tm.monthday),
month: Month(tm.month),
year: tm.year + 1900'i32,
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",
# BSD stores in `gmtoff` offset east of UTC in seconds,
# but posix systems using west of UTC in seconds
timezone: if local: -(tm.gmtoff) else: 0
)
else:
TimeInfo(second: int(tm.second), TimeInfo(second: int(tm.second),
minute: int(tm.minute), minute: int(tm.minute),
hour: int(tm.hour), hour: int(tm.hour),
@ -476,6 +513,7 @@ when not defined(JS):
timezone: if local: getTimezone() else: 0 timezone: if local: getTimezone() else: 0
) )
proc timeInfoToTM(t: TimeInfo): StructTM = proc timeInfoToTM(t: TimeInfo): StructTM =
const const
weekDays: array [WeekDay, int8] = [1'i8,2'i8,3'i8,4'i8,5'i8,6'i8,0'i8] weekDays: array [WeekDay, int8] = [1'i8,2'i8,3'i8,4'i8,5'i8,6'i8,0'i8]
@ -564,6 +602,13 @@ when not defined(JS):
return ($tzname[0], $tzname[1]) return ($tzname[0], $tzname[1])
proc getTimezone(): int = proc getTimezone(): int =
when defined(freebsd) or defined(netbsd):
var a = timec(nil)
let lt = localtime(addr(a))
# BSD stores in `gmtoff` offset east of UTC in seconds,
# but posix systems using west of UTC in seconds
return -(lt.gmtoff)
else:
return timezone return timezone
proc fromSeconds(since1970: float): Time = Time(since1970) proc fromSeconds(since1970: float): Time = Time(since1970)