Dates and Times
Graphersal has six date and time types. They are values of their own, like numbers and
UUIDs: a property holds a date, not a string that looks like
one, and a query compares, sorts and computes with it. The model follows PostgreSQL and the SQL
standard; the part TinkerPop defines (datetime(), asDate(), dateAdd(), dateDiff(),
DT.second to DT.day) behaves exactly as TinkerPop 3.8 specifies it
(TinkerPop compatibility).
The six types
Graphersal (type_of, GType) | What it is | Literal | PostgreSQL | SQL standard | Python | TinkerPop / java.time |
|---|---|---|---|---|---|---|
datetime (GType.DATETIME) | an instant with the UTC offset it was written with, and the IANA zone name when it was written with one | datetime("2023-08-02T09:30:00+02:00"), datetime("2023-08-02T09:30[Europe/Berlin]") | timestamptz (keeps no offset) | TIMESTAMP WITH TIME ZONE | aware datetime.datetime (a zoneinfo.ZoneInfo for a zone name) | datetime, OffsetDateTime, ZonedDateTime |
localdatetime (GType.LOCALDATETIME) | a wall-clock date and time, no zone | localdatetime("2023-08-02T09:30") | timestamp | TIMESTAMP | naive datetime.datetime | LocalDateTime |
date (GType.DATE) | a calendar day | date("2023-08-02") | date | DATE | datetime.date | LocalDate |
time (GType.TIME) | a time of day, no zone | time("09:30:15.5") | time | TIME | naive datetime.time | LocalTime |
offsettime (GType.OFFSETTIME) | a time of day with a UTC offset | offsettime("09:30:15.5+02:00") | timetz | TIME WITH TIME ZONE (GQL ZONED TIME) | datetime.time with a datetime.timezone | OffsetTime |
duration (GType.DURATION) | months, days and nanoseconds, each with its own sign | duration("P1Y2M3DT4H") | interval | INTERVAL | datetime.timedelta, or graphersal.Interval with months | Duration, Period |
type_of(datetime("2023-08-02T09:30:00+02:00")) // "datetime"
date("2024-02-29").day // 29
duration("P1Y2M3DT4H").months // 14
g.inject(datetime("2023-08-02T09:30Z")).is(P.typeOf(GType.DATETIME)).count().next() // 1
- Resolution: one nanosecond for every type with a time, as in
java.timeand TinkerPop (PostgreSQL and Python stop at the microsecond). TinkerPop's millisecond views (as_number()of adatetime,date_diff()) round down to milliseconds. Python'sdatetimeholds microseconds: a value with a part below them comes back as apandas.Timestamp/pandas.Timedelta(From Python). The store format, GraphSON, GraphML and the serialized change set keep every nanosecond (Storage and Exchange). The clock behindnow()has the digits the platform gives (nanoseconds on Linux, microseconds on macOS, milliseconds in the browser: Clock precision). - Range: the years -9999 to 9999 in the proleptic Gregorian calendar (
date("-0044-03-15")is year 44 BC counted astronomically, year 0 = 1 BC). A value outside the range, or a result that would leave it, is an error, never a wrapped value. - Offsets: a
datetimekeeps its offset (±18:00, whole seconds) and shows it, unlike PostgreSQL'stimestamptz, which keeps only the instant. Adatetimewritten with an IANA zone name keeps the name too (2024-03-31T12:00:00+02:00[Europe/Berlin]) and follows the zone's daylight saving rules in its calendar arithmetic; a step given an IANA zone (at_zone("Europe/Berlin")) returns a value carrying that name, one given a fixed offset (at_zone("+02:00")) a value with the offset only (Time Zones). Anoffsettimekeeps its offset. - A
durationis not normalized:P1M,P30DandPT720Hare three different values. A month has no fixed length and a day is not always 24 hours in a time zone, so the three fields stay apart, as in PostgreSQL'sinterval. - Duration range: the months and the days are 32-bit integers each; the time part has the
range of
java.time.Duration(TinkerPop) and of Neo4j's duration, a 64-bit count of seconds plus the nanoseconds of one second: from-PT9223372036854775808StoPT9223372036854775807.999999999S(about ±292 billion years). Adding such a duration to a date or time whose result leaves the years -9999 to 9999 is an error. - No guessing: a string stays a string whatever it looks like. A date comes from a
constructor, a conversion step (Conversions), a schema that declares the
property (Schema), a typed value in a file (Storage and Exchange) or
a Python
datetime(From Python).
Script values
In a script each type is a value of its own Rhai type with getters and operators:
| Type | Getters |
|---|---|
datetime | year, month, day, hour, minute, second, microsecond, nanosecond (on its own wall clock), offset (seconds), timezone (the zone name, else the offset text), epoch_millis |
localdatetime | year, month, day, hour, minute, second, microsecond, nanosecond |
date | year, month, day |
time | hour, minute, second, microsecond, nanosecond |
offsettime | hour, minute, second, microsecond, nanosecond (of its local time), offset (seconds), timezone (the offset text) |
duration | months, days, micros (the time part in whole microseconds), nanos (in nanoseconds); micros beyond a 64-bit integer (about 292,000 years) and nanos beyond one (about 292 years) are a range error |
microsecond is the whole microseconds of the second (0-999999), nanosecond all its
nanoseconds (0-999999999): time("12:00:00.123456789") has microsecond 123456 and
nanosecond 123456789.
let d = datetime("2023-08-02T09:30:15.25+02:00"); [d.hour, d.offset, d.microsecond] // [9, 7200, 250000]
datetime("2023-08-02T12:00Z") + duration("P1M") // datetime("2023-09-02T12:00:00Z")
date("2023-08-02") - date("2023-01-01") // duration("P213D")
time("10:00") - time("09:30") // duration("PT30M")
offsettime("10:00+02:00") - offsettime("07:30Z") // duration("PT30M")
datetime("2024-03-30T12:00[Europe/Berlin]") + duration("P1D") // datetime("2024-03-31T12:00:00+02:00[Europe/Berlin]")
datetime("2023-08-02T09:30+02:00").as_string() // "2023-08-02T09:30:00+02:00"
+ and - with a duration work as date_add() does; the difference of two
values of one type is a duration (two datetimes or localdatetimes give days of 24 hours and
a time part, two dates days, two times the elapsed time, two offsettimes the time between
their UTC times of day). as_string() is the canonical text,
to_string() the DSL literal. A value printed or shown as a script value is its literal
(date("2023-08-02")); tables, JSON and the playground show the canonical text.
The pages of this part
- Comparing and Sorting: equality by instant (and by UTC time of day), field-wise durations, the order of types.
- Text Formats: the canonical output, what is read, strict and lenient input.
- Time Zones: the bundled IANA database, offsets, zone names kept in a value, daylight saving.
- Arithmetic:
date_add(),date_diff(),age(). - Truncation and Parts:
date_trunc(),date_part(). - Conversions:
as_date(),as_duration(),as_number(),as_string(). - now() and today(): the evaluation's start time and how a host pins it.
- Dates in a Schema: the date
formats, bounds and coercion. - Storage and Exchange: the binary format, GraphSON, GraphML, change sets.
- From Python: the
datetimemapping. - TinkerPop Compatibility: what is TinkerPop and what is an extension.
- Dates in GQL: the openCypher temporal functions of
gql()(date(),x.year,date.truncate(),duration.between()) beside the steps of this part.
The steps have generated reference pages: as_date,
as_duration, date_add,
date_diff, age,
date_trunc, date_part,
at_zone and as_local; the
constructors, now(), today() and DT are on
Functions, Tokens and Values.