import times const zeroTime = fromSeconds(0) proc format*(ti: TimeInterval, fmt: string): string = let info = getGMTime(fromSeconds(0) + ti) return info.format(fmt) proc `-`*(a, b: TimeInfo): TimeInterval = return seconds(cast[int](a.toTime - b.toTime)) proc `>`*(a, b: TimeInfo): bool = return a.toTime > b.toTime proc `<`*(a, b: TimeInfo): bool = return a.toTime < b.toTime proc `>=`*(a, b: TimeInfo): bool = return a.toTime >= b.toTime proc `<=`*(a, b: TimeInfo): 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 cmp*(a, b: TimeInfo): int = if b == a: return 0 elif a > b: return 1 else: return -1 proc startOfDay*(ti: TimeInfo): TimeInfo = result = ti result.hour = 0 result.minute = 0 result.second = 0 proc startOfWeek*(ti: TimeInfo, startDay = dMon): TimeInfo = var diff = (ti.weekday.ord - startDay.ord) if diff < 0: diff += 7 return (ti - diff.days).startOfDay # This is a workaround needed due a bug in Nim's times.parse procedure. # see: https://github.com/nim-lang/Nim/issues/4922 proc fixedParse*(value, format: string): TimeInfo = let firstParsed = parse(value, format) let rectified = getLocalTime(toTime(firstParsed)) if firstParsed.isDST and not rectified.isDST: return rectified + 1.hours elif not firstParsed.isDST and rectified.isDST: return rectified - 1.hours else: return rectified