import times const zeroTime = fromUnix(0) const ISO_8601_FULL_FORMAT = "yyyy:MM:dd'T'HH:mm:sszzz" proc format*(ti: TimeInterval, fmt: string): string = let info = (fromUnix(0) + ti).utc return info.format(fmt) # Will be deprecated in Nim 0.18.1 as it will exist in the standard times module. proc `-`*(a, b: DateTime): TimeInterval = return seconds(cast[int](a.toTime - b.toTime)) proc `>`*(a, b: DateTime): bool = return a.toTime > b.toTime proc `<`*(a, b: DateTime): bool = return a.toTime < b.toTime proc `>=`*(a, b: DateTime): bool = return a.toTime >= b.toTime proc `<=`*(a, b: DateTime): bool = return a.toTime <= b.toTime proc `>`*(a, b: TimeInterval): bool = return (zeroTime + a) > (zeroTime + b) proc `<`*(a, b: TimeInterval): bool = return (zeroTime + a) < (zeroTime + b) proc `>=`*(a, b: TimeInterval): bool = return (zeroTime + a) >= (zeroTime + b) proc `<=`*(a, b: TimeInterval): bool = return (zeroTime + a) <= (zeroTime + b) proc between*(a, s, e: DateTime): bool = return a >= s and a < e proc cmp*(a, b: DateTime): int = if b == a: return 0 elif a > b: return 1 else: return -1 proc startOfDay*(ti: DateTime): DateTime = result = ti result.hour = 0 result.minute = 0 result.second = 0 proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime = var diff = (ti.weekday.ord - startDay.ord) if diff < 0: diff += 7 return (ti - diff.days).startOfDay proc parseIso8601*(val: string): DateTime = return val.parse(ISO_8601_FULL_FORMAT) proc formatIso8601*(d: DateTime): string = return d.format(ISO_8601_FULL_FORMAT) # This is a workaround needed due a bug in Nim's times.parse procedure. # see: https://github.com/nim-lang/Nim/issues/4922 template fixedParse*(value, format: string): DateTime {.deprecated.} = parse(value, format)