src/chrono/timestamps

Source   Edit  

chrono/timestamps

If you are going to just parse or format dates. I recommend using just the import chrono/timestamps module. It it imports the Timestamp that is enough for most cases involved with times. I always recommend storing dates as a float64 number of seconds since 1970. This is exactly what Timestamp is. When you need to parse it or display it use parseTs or formatTs.

var ts = parseTs(
  "{year/4}-{month/2}-{day/2}T{hour/2}:{minute/2}:{second/2}Z",
  "1988-02-09T03:34:12Z"
)

echo ts

echo formatTs(
  ts,
  "{year/4}-{month/2}-{day/2}T{hour/2}:{minute/2}:{second/2}Z",
)

If you need to parse ISO dates which is a very common format you find all over the internet. You can even use faster optimized versions here:

echo parseIsoTs("2017-11-08T08:01:43Z")
echo Timestamp(1510128103.0).formatIso()

Types

Timestamp = distinct float64
Always seconds since 1970 UTC. Source   Edit  

Procs

proc `$`(a: Timestamp): string {....raises: [], tags: [], forbids: [].}
Display a timestamps as a float64. Source   Edit  
proc `<`(a, b: Timestamp): bool {....raises: [], tags: [], forbids: [].}
Compare timestamps. Source   Edit  
proc `<=`(a, b: Timestamp): bool {....raises: [], tags: [], forbids: [].}
Compare timestamps. Source   Edit  
proc `==`(a, b: Timestamp): bool {....raises: [], tags: [], forbids: [].}
Compare timestamps. Source   Edit  
proc `>`(a, b: Timestamp): bool {....raises: [], tags: [], forbids: [].}
Compare timestamps. Source   Edit  
proc `>=`(a, b: Timestamp): bool {....raises: [], tags: [], forbids: [].}
Compare timestamps. Source   Edit  
proc add(ts: Timestamp; timeScale: TimeScale; number: int): Timestamp {.
    ...raises: [], tags: [], forbids: [].}
Add Seconds, Minutes, Hours, Days ... to Timestamp. Source   Edit  
proc calendar(ts: Timestamp): Calendar {....raises: [], tags: [], forbids: [].}
Converts a Timestamp to a Calendar. Source   Edit  
proc calendar(ts: Timestamp; tzOffset: float64): Calendar {....raises: [],
    tags: [], forbids: [].}
Converts a Timestamp to a Calendar with a tz offset. Does not deal with DST. Source   Edit  
proc format(ts: Timestamp; fmt: string): string {....raises: [ValueError],
    tags: [], forbids: [].}
Format a Timestamp using the format string. Source   Edit  
proc formatIso(ts: Timestamp): string {....raises: [], tags: [], forbids: [].}
Fastest way to convert Timestamp to an ISO 8601 string representation. Use this instead of the format function when dealing with ISO format. Source   Edit  
proc formatIso(ts: Timestamp; tzOffset: float64): string {....raises: [], tags: [],
    forbids: [].}
Fastest way to convert Timestamp to an ISO 8601 string representation. Use this instead of the format function when dealing with ISO format. Source   Edit  
proc parseIsoTs(iso: string): Timestamp {....raises: [ValueError], tags: [],
    forbids: [].}
Fastest way to convert an ISO 8601 string representation to a Timestamp. Use this instead of the parseTimestamp function when dealing with ISO format. Source   Edit  
proc parseTs(fmt: string; value: string): Timestamp {....raises: [ValueError],
    tags: [], forbids: [].}
Parse time using the Chrono format string into a Timestamp. Source   Edit  
proc sub(ts: Timestamp; timeScale: TimeScale; number: int): Timestamp {.
    ...raises: [], tags: [], forbids: [].}
Subtract Seconds, Minutes, Hours, Days ... to Timestamp. Source   Edit  
proc toEndOf(ts: Timestamp; timeScale: TimeScale): Timestamp {....raises: [],
    tags: [], forbids: [].}
Move the time stamp to an end of a time scale. Source   Edit  
proc toStartOf(ts: Timestamp; timeScale: TimeScale): Timestamp {....raises: [],
    tags: [], forbids: [].}
Move the time stamp to a start of a time scale. Source   Edit  
proc ts(cal: Calendar): Timestamp {....raises: [], tags: [], forbids: [].}
Converts Calendar to a Timestamp. Source   Edit