From 612cd2161aa1a145cc11f559b135a948f972f344 Mon Sep 17 00:00:00 2001 From: JamesP Date: Thu, 10 Sep 2015 17:29:52 +1000 Subject: [PATCH 1/4] add examples block to top of times module --- lib/pure/times.nim | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/pure/times.nim b/lib/pure/times.nim index aa4ae5ace8..5d960b11a4 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -11,6 +11,25 @@ ## This module contains routines and types for dealing with time. ## This module is available for the `JavaScript target ## `_. +## +## Examples: +## +## .. code-block:: nim +## +## import times, os +## var +## t = cpuTime() +## +## sleep(100) # replace this with something to be timed +## echo "Time taken: ",cpuTime() - t +## +## echo "My formatted time: ", format(getLocalTime(getTime()), "d MMMM yyyy HH:mm") +## echo "Using predefined formats: ", getClockStr(), " ", getDateStr() +## +## echo "epochTime() float value: ", epochTime() +## echo "getTime() float value: ", toSeconds(getTime()) +## echo "cpuTime() float value: ", cpuTime() +## echo "An hour from now: ", getLocalTime(getTime()) + initInterval(0,0,0,1) {.push debugger:off.} # the user does not want to trace a part # of the standard library! From c122d71e66e9edbbeb9f6bb32475321447439862 Mon Sep 17 00:00:00 2001 From: JamesP Date: Thu, 10 Sep 2015 17:38:00 +1000 Subject: [PATCH 2/4] bug fix: `+` and `-` handle GM and Local time correctly eg, getGmTime(getTime()) + initInterval(hours=1) gave incorrect results --- lib/pure/times.nim | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 5d960b11a4..f0455baac7 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -307,10 +307,10 @@ proc `+`*(a: TimeInfo, interval: TimeInterval): TimeInfo = ## very accurate. let t = toSeconds(timeInfoToTime(a)) let secs = toSeconds(a, interval) - if a.tzname == "UTC": - result = getGMTime(fromSeconds(t + secs)) - else: - result = getLocalTime(fromSeconds(t + secs)) + #if a.tzname == "UTC": + # result = getGMTime(fromSeconds(t + secs)) + #else: + result = getLocalTime(fromSeconds(t + secs)) proc `-`*(a: TimeInfo, interval: TimeInterval): TimeInfo = ## subtracts ``interval`` time. @@ -319,10 +319,10 @@ proc `-`*(a: TimeInfo, interval: TimeInterval): TimeInfo = ## when you subtract so much that you reach the Julian calendar. let t = toSeconds(timeInfoToTime(a)) let secs = toSeconds(a, interval) - if a.tzname == "UTC": - result = getGMTime(fromSeconds(t - secs)) - else: - result = getLocalTime(fromSeconds(t - secs)) + #if a.tzname == "UTC": + # result = getGMTime(fromSeconds(t - secs)) + #else: + result = getLocalTime(fromSeconds(t - secs)) when not defined(JS): proc epochTime*(): float {.rtl, extern: "nt$1", tags: [TimeEffect].} From f5d55ad0124870ff5d574e5069d9f8cf2f9e8b9c Mon Sep 17 00:00:00 2001 From: JamesP Date: Thu, 10 Sep 2015 19:21:27 +1000 Subject: [PATCH 3/4] tests added: toSeconds with local/GM timezones, interval add/sub with local/GM timezones --- lib/pure/times.nim | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/pure/times.nim b/lib/pure/times.nim index f0455baac7..94aeeffe17 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -1288,3 +1288,28 @@ when isMainModule: assert getDayOfWeekJulian(21, 9, 1970) == dMon assert getDayOfWeekJulian(1, 1, 2000) == dSat assert getDayOfWeekJulian(1, 1, 2021) == dFri + + # toSeconds tests with GM and Local timezones + #var t4 = getGMTime(fromSeconds(876124714)) # Mon 6 Oct 08:58:34 BST 1997 + var t4L = getLocalTime(fromSeconds(876124714)) + assert toSeconds(timeInfoToTime(t4L)) == 876124714 # fromSeconds is effectively "localTime" + assert toSeconds(timeInfoToTime(t4L)) + t4L.timezone.float == toSeconds(timeInfoToTime(t4)) + + assert toSeconds(t4, initInterval(seconds=0)) == 0.0 + assert toSeconds(t4L, initInterval(milliseconds=1)) == toSeconds(t4, initInterval(milliseconds=1)) + assert toSeconds(t4L, initInterval(seconds=1)) == toSeconds(t4, initInterval(seconds=1)) + assert toSeconds(t4L, initInterval(minutes=1)) == toSeconds(t4, initInterval(minutes=1)) + assert toSeconds(t4L, initInterval(hours=1)) == toSeconds(t4, initInterval(hours=1)) + assert toSeconds(t4L, initInterval(days=1)) == toSeconds(t4, initInterval(days=1)) + assert toSeconds(t4L, initInterval(months=1)) == toSeconds(t4, initInterval(months=1)) + assert toSeconds(t4L, initInterval(years=1)) == toSeconds(t4, initInterval(years=1)) + + # adding intervals + var + a1L = toSeconds(timeInfoToTime(t4L + initInterval(hours = 1))) + t4L.timezone.float + a1G = toSeconds(timeInfoToTime(t4)) + 60.0 * 60.0 + assert a1L == a1G + # subtracting intervals + a1L = toSeconds(timeInfoToTime(t4L - initInterval(hours = 1))) + t4L.timezone.float + a1G = toSeconds(timeInfoToTime(t4)) - (60.0 * 60.0) + assert a1L == a1G From 2d1f26302c50188b49bbaf17a92a276f61311f8f Mon Sep 17 00:00:00 2001 From: JamesP Date: Thu, 10 Sep 2015 19:29:26 +1000 Subject: [PATCH 4/4] Example: add extra getGmTime interval addition example --- lib/pure/times.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 94aeeffe17..3142952e70 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -29,7 +29,8 @@ ## echo "epochTime() float value: ", epochTime() ## echo "getTime() float value: ", toSeconds(getTime()) ## echo "cpuTime() float value: ", cpuTime() -## echo "An hour from now: ", getLocalTime(getTime()) + initInterval(0,0,0,1) +## echo "An hour from now : ", getLocalTime(getTime()) + initInterval(0,0,0,1) +## echo "An hour from (UTC) now: ", getGmTime(getTime()) + initInterval(0,0,0,1) {.push debugger:off.} # the user does not want to trace a part # of the standard library!