summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2013-04-14 02:41:12 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2013-04-14 02:41:12 +0100
commitbadf792f22b33aa7b330383b4aa0909c34fc64c4 (patch)
tree3e0bd45cc982d04c6fae79765674ff3b5a934748
parentcc4250d746513e188784bcd5ec2a2924af40f239 (diff)
downloadNim-badf792f22b33aa7b330383b4aa0909c34fc64c4.tar.gz
Fixed some problems with the times module, added interval tests.
-rw-r--r--lib/pure/times.nim31
1 files changed, 15 insertions, 16 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index a28c9a24e..03a05aea1 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -153,15 +153,15 @@ proc TimeInfoToTime*(timeInfo: TTimeInfo): TTime {.tags: [].}
   ## them from the other information in the broken-down time structure.
 
 proc fromSeconds*(since1970: float): TTime {.tags: [].}
-  ## Takes a float which contains the number of seconds since 1970 and
+  ## Takes a float which contains the number of seconds since the unix epoch and
   ## returns a time object.
 
 proc fromSeconds*(since1970: int|int64): TTime = fromSeconds(float(since1970))
-  ## Takes an in which contains the number of seconds since 1970 and
+  ## Takes an int which contains the number of seconds since the unix epoch and
   ## returns a time object.
 
 proc toSeconds*(time: TTime): float {.tags: [].}
-  ## Returns the time in seconds since 1970.
+  ## Returns the time in seconds since the unix epoch.
 
 proc `$` *(timeInfo: TTimeInfo): string {.tags: [].}
   ## converts a `TTimeInfo` object to a string representation.
@@ -226,14 +226,9 @@ proc getDaysInMonth(month: TMonth, year: int): int =
   of mApr, mJun, mSep, mNov: result = 30
   else: result = 31
 
-proc `-`*(interval: TTimeInterval): TTimeInterval =
-  for a, b in fields(result, interval):
-    a = -b
-
-proc toSeconds*(a: TTimeInfo, interval: TTimeInterval): float =
-  ## Returns the time the interval will be at that point in time. This
-  ## needs a time as well, because e.g. a month is not always the same
-  ## length.
+proc toSeconds(a: TTimeInfo, interval: TTimeInterval): float =
+  ## Calculates how many seconds the interval is worth by adding up
+  ## all the fields
 
   var anew = a
   var newinterv = interval
@@ -253,9 +248,6 @@ proc toSeconds*(a: TTimeInfo, interval: TTimeInterval): float =
   result += float(newinterv.seconds)
   result += newinterv.miliseconds / 1000
 
-proc toSeconds*(a: TTime, interval: TTimeInterval): float =
-  result = toSeconds(getGMTime(a), interval)
-
 proc `+`*(a: TTimeInfo, interval: TTimeInterval): TTimeInfo =
   ## adds ``interval`` time.
   ##
@@ -273,7 +265,12 @@ proc `-`*(a: TTimeInfo, interval: TTimeInterval): TTimeInfo =
   ##
   ## **Note:** This has been only briefly tested, it is inaccurate especially
   ## when you subtract so much that you reach the Julian calendar.
-  result = a + -interval
+  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))
 
 when not defined(JS):  
   proc epochTime*(): float {.rtl, extern: "nt$1", tags: [FTime].}
@@ -742,4 +739,6 @@ when isMainModule:
   var t4 = getGMTime(fromSeconds(876124714)) # Mon  6 Oct 08:58:34 BST 1997
   assert t4.format("M MM MMM MMMM") == "10 10 Oct October"
   
-  
+  # Interval tests
+  assert((t4 - initInterval(years = 2)).format("yyyy") == "1995")
+  assert((t4 - initInterval(years = 7, minutes = 34, seconds = 24)).format("yyyy mm ss") == "1990 24 10")