summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorMichael Voronin <m.voronin@ngenix.net>2018-05-10 10:28:08 +0300
committerMichael Voronin <m.voronin@ngenix.net>2018-05-10 13:18:38 +0300
commit8a080acda915f24bdd61a01175dcef6f6735d90e (patch)
treee02e6c39662e97c3769f89b1290e46b0ef91b13f /lib
parent0ff0dfbfce6a26903709bf2f6e06d22c86e70c07 (diff)
downloadNim-8a080acda915f24bdd61a01175dcef6f6735d90e.tar.gz
[add] Add some wrappers
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/times.nim20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index c7063b252..8b134f226 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -1440,6 +1440,19 @@ proc format*(dt: DateTime, f: string): string {.tags: [].}=
     inc(i)
   formatToken(dt, currentF, result)
 
+proc format*(time: Time, f: string, zone_info: proc(t: Time): DateTime): string {.tags: [].} =
+  ## converts a `Time` value to a string representation. It will use format from
+  ## ``format(dt: DateTime, f: string)``.
+  runnableExamples:
+    var dt = initDateTime(01, mJan, 1970, 00, 00, 00, local())
+    var tm = dt.toTime()
+    doAssert format(tm, "yyyy-MM-dd'T'HH:mm:ss", local) == "1970-01-01T00:00:00"
+    dt = initDateTime(01, mJan, 1970, 00, 00, 00, utc())
+    tm = dt.toTime()
+    doAssert format(tm, "yyyy-MM-dd'T'HH:mm:ss", utc) == "1970-01-01T00:00:00"
+
+  zone_info(time).format(f)
+
 proc `$`*(dt: DateTime): string {.tags: [], raises: [], benign.} =
   ## Converts a `DateTime` object to a string representation.
   ## It uses the format ``yyyy-MM-dd'T'HH-mm-sszzz``.
@@ -1770,6 +1783,13 @@ proc parse*(value, layout: string, zone: Timezone = local()): DateTime =
     # Otherwise convert to `zone`
     result = dt.toTime.inZone(zone)
 
+proc parseTime*(value, layout: string, zone: Timezone): Time =
+  ## Simple wrapper for parsing string to time
+  runnableExamples:
+    let tStr = "1970-01-01T00:00:00+00:00"
+    doAssert parseTime(tStr, "yyyy-MM-dd'T'HH:mm:sszzz", local()) == fromUnix(0)
+  parse(value, layout, zone).toTime()
+
 proc countLeapYears*(yearSpan: int): int =
   ## Returns the number of leap years spanned by a given number of years.
   ##