Temporal Data Types¶
Temporal data types are implemented by the neo4j.time
module.
It provides a set of types compliant with ISO-8601 and Cypher, which are similar to those found in the built-in datetime
module.
Sub-second values are measured to nanosecond precision and the types are compatible with pytz.
The table below shows the general mappings between Cypher and the temporal types provided by the driver.
In addition, the built-in temporal types can be passed as parameters and will be mapped appropriately.
Cypher |
Python driver type |
Python built-in type |
|
---|---|---|---|
Date |
|||
Time |
|
||
LocalTime |
|
||
DateTime |
|
||
LocalDateTime |
|
||
Duration |
Sub-second values are measured to nanosecond precision and the types are compatible with pytz.
Note
Cypher has built-in support for handling temporal values, and the underlying database supports storing these temporal values as properties on nodes and relationships, see https://neo4j.com/docs/cypher-manual/current/syntax/temporal/
Constants¶
- neo4j.time.MIN_YEAR = 1¶
The smallest year number allowed in a
Date
orDateTime
object to be compatible withdatetime.date
anddatetime.datetime
.
- neo4j.time.MAX_YEAR = 9999¶
The largest year number allowed in a
Date
orDateTime
object to be compatible withdatetime.date
anddatetime.datetime
.
Date¶
- class neo4j.time.Date(year, month, day)¶
Idealized date representation.
A
Date
object represents a date (year, month, and day) in the proleptic Gregorian Calendar.Years between 0001 and 9999 are supported, with additional support for the “zero date” used in some contexts.
Each date is based on a proleptic Gregorian ordinal, which models 1 Jan 0001 as day 1 and counts each subsequent day up to, and including, 31 Dec 9999. The standard year, month and day value of each date is also available.
Internally, the day of the month is always stored as-is, with the exception of the last three days of that month. These are always stored as -1, -2 and -3 (counting from the last day). This system allows some temporal arithmetic (particularly adding or subtracting months) to produce a more desirable outcome than would otherwise be produced. Externally, the day number is always the same as would be written on a calendar.
- Parameters:
year (int) – the year. Minimum
MIN_YEAR
(0001), maximumMAX_YEAR
(9999).month (int) – the month. Minimum 1, maximum 12.
day (int) – the day. Minimum 1, maximum
Date.days_in_month(year, month)
.
A zero date can also be acquired by passing all zeroes to the
neo4j.time.Date
constructor or by using theZeroDate
constant.
Class methods¶
- classmethod Date.today(tz=None)¶
Get the current date.
- Parameters:
tz (datetime.tzinfo or None) – timezone or None to get the local
Date
.- Return type:
- Raises:
OverflowError – if the timestamp is out of the range of values supported by the platform C localtime() function. It’s common for this to be restricted to years from 1970 through 2038.
- classmethod Date.from_timestamp(timestamp, tz=None)¶
Date
from a time stamp (seconds since unix epoch).- Parameters:
timestamp (float) – the unix timestamp (seconds since unix epoch).
tz (datetime.tzinfo or None) – timezone. Set to None to create a local
Date
.
- Return type:
- Raises:
OverflowError – if the timestamp is out of the range of values supported by the platform C localtime() function. It’s common for this to be restricted to years from 1970 through 2038.
- classmethod Date.utc_from_timestamp(timestamp)¶
Date
from a time stamp (seconds since unix epoch).Returns the Date as local date Date in UTC.
- Return type:
- classmethod Date.from_ordinal(ordinal)¶
The
Date
that corresponds to the proleptic Gregorian ordinal.0001-01-01 has ordinal 1 and 9999-12-31 has ordinal 3,652,059. Values outside of this range trigger a
ValueError
. The corresponding instance method for the reverse date-to-ordinal transformation isto_ordinal()
. The ordinal 0 has a special semantic and will returnZeroDate
.- Return type:
- Raises:
ValueError – if the ordinal is outside the range [0, 3652059] (both values included).
- classmethod Date.parse(s)¶
Parse a string to produce a
Date
.- Accepted formats:
‘Y-M-D’
- Parameters:
s (str) – the string to be parsed.
- Return type:
- Raises:
ValueError – if the string could not be parsed.
- classmethod Date.from_native(d)¶
Convert from a native Python datetime.date value.
- Parameters:
d (datetime.date) – the date to convert.
- Return type:
- classmethod Date.from_clock_time(clock_time, epoch)¶
Convert from a ClockTime relative to a given epoch.
- classmethod Date.is_leap_year(year)¶
Indicates whether or not year is a leap year.
- Parameters:
year (int) – the year to look up
- Return type:
- Raises:
ValueError – if year is out of range:
MIN_YEAR
<= year <=MAX_YEAR
- classmethod Date.days_in_year(year)¶
Return the number of days in year.
- Parameters:
year (int) – the year to look up
- Return type:
- Raises:
ValueError – if year is out of range:
MIN_YEAR
<= year <=MAX_YEAR
Class attributes¶
- Date.min = neo4j.time.Date(1, 1, 1)¶
The earliest date value possible.
- Date.max = neo4j.time.Date(9999, 12, 31)¶
The latest date value possible.
- Date.resolution = (0, 1, 0, 0)¶
The minimum resolution supported.
Instance attributes¶
- Date.year_month_day¶
3-tuple of (year, month, day) describing the date.
- Date.year_week_day¶
3-tuple of (year, week_of_year, day_of_week) describing the date.
day_of_week will be 1 for Monday and 7 for Sunday.
Operations¶
- Date.__hash__()¶
- Date.__eq__(other)¶
== comparison with
Date
ordatetime.date
.
- Date.__ne__(other)¶
!= comparison with
Date
ordatetime.date
.
- Date.__lt__(other)¶
< comparison with
Date
ordatetime.date
.
- Date.__gt__(other)¶
> comparison with
Date
ordatetime.date
.
- Date.__le__(other)¶
<= comparison with
Date
ordatetime.date
.
- Date.__ge__(other)¶
>= comparison with
Date
ordatetime.date
.
- Date.__add__(other)¶
Add a
Duration
.- Return type:
- Raises:
ValueError – if the added duration has a time component.
Instance methods¶
- Date.replace(**kwargs)¶
Return a
Date
with one or more components replaced.- Keyword Arguments:
year (int): overwrite the year - default: self.year
month (int): overwrite the month - default: self.month
day (int): overwrite the day - default: self.day
- Date.time_tuple()¶
Convert the date to
time.struct_time
.- Return type:
- Date.to_ordinal()¶
The date’s proleptic Gregorian ordinal.
The corresponding class method for the reverse ordinal-to-date transformation is
Date.from_ordinal()
.- Return type:
- Date.to_clock_time(epoch)¶
Convert the date to
ClockTime
relative to epoch.- Parameters:
epoch (Date) – the epoch to which the date is relative
- Return type:
ClockTime
- Date.to_native()¶
Convert to a native Python
datetime.date
value.- Return type:
- Date.iso_calendar()¶
Alias for
year_week_day
- Date.__repr__()¶
- Date.__str__()¶
- Date.__format__(format_spec)¶
Special values¶
- neo4j.time.ZeroDate = neo4j.time.ZeroDate¶
A
neo4j.time.Date
instance set to 0000-00-00. This has an ordinal value of 0.
Time¶
- class neo4j.time.Time(hour=0, minute=0, second=0, nanosecond=0, tzinfo=None)¶
Time of day.
The
Time
class is a nanosecond-precision drop-in replacement for the standard librarydatetime.time
class.A high degree of API compatibility with the standard library classes is provided.
neo4j.time.Time
objects introduce the concept of ticks. This is simply a count of the number of seconds since midnight, in many ways analogous to theneo4j.time.Date
ordinal. ticks values can be fractional, with a minimum value of 0 and a maximum of 86399.999999999.Local times are represented by
Time
with no tzinfo.Note
Staring with version 5.0,
ticks
will change to be integers counting nanoseconds since midnight. Currently available asticks_ns
.- Parameters:
hour (int) – the hour of the time. Must be in range 0 <= hour < 24.
minute (int) – the minute of the time. Must be in range 0 <= minute < 60.
second (float) –
the second of the time. Here, a float is accepted to denote sub-second precision up to nanoseconds. Must be in range 0 <= second < 60.
Starting with version 5 0, this parameter will only accept
int
.nanosecond (int) – the nanosecond .Must be in range 0 <= nanosecond < 999999999.
tzinfo (datetime.tzinfo or None) – timezone or None to get a local
Time
.
- Raises:
ValueError – if one of the parameters is out of range.
Class methods¶
- classmethod Time.now(tz=None)¶
Get the current time.
- Parameters:
tz (datetime.tzinfo) – optional timezone
- Return type:
- Raises:
OverflowError – if the timestamp is out of the range of values supported by the platform C localtime() function. It’s common for this to be restricted to years from 1970 through 2038.
- classmethod Time.from_ticks(ticks, tz=None)¶
Create a time from legacy ticks (seconds since midnight).
Deprecated since version 4.4: Staring from 5.0, this method’s signature will be replaced with that of
from_ticks_ns()
.- Parameters:
ticks (float) – seconds since midnight
tz (datetime.tzinfo) – optional timezone
- Return type:
- Raises:
ValueError – if ticks is out of bounds (0 <= ticks < 86400)
- classmethod Time.from_ticks_ns(ticks, tz=None)¶
Create a time from ticks (nanoseconds since midnight).
- Parameters:
ticks (int) – nanoseconds since midnight
tz (datetime.tzinfo) – optional timezone
- Return type:
- Raises:
ValueError – if ticks is out of bounds (0 <= ticks < 86400000000000)
- classmethod Time.from_native(t)¶
Convert from a native Python
datetime.time
value.- Parameters:
t (datetime.time) – time to convert from
- Return type:
- classmethod Time.from_clock_time(clock_time, epoch)¶
Convert from a
ClockTime
relative to a given epoch.This method, in contrast to most others of this package, assumes days of exactly 24 hours.
Class attributes¶
- Time.min = neo4j.time.Time(0, 0, 0, 0)¶
The earliest time value possible.
- Time.max = neo4j.time.Time(23, 59, 59, 999999999)¶
The latest time value possible.
- Time.resolution = (0, 0, 0, 1)¶
The minimum resolution supported.
Instance attributes¶
- Time.ticks¶
The total number of seconds since midnight.
Deprecated since version 4.4: will return
ticks_ns
starting with version 5.0.- Type:
- Time.ticks_ns¶
The total number of nanoseconds since midnight.
Note
This will be removed in 5.0 and replace
ticks
.- Type:
- Time.second¶
The seconds of the time.
This contains seconds and nanoseconds of the time. int(:attr:.seconds`)` will yield the seconds without nanoseconds.
- Type:
Changed in version 4.4: The property’s type changed from
float
todecimal.Decimal
to mitigate rounding issues.
- Time.hour_minute_second¶
The time as a tuple of (hour, minute, second).
- Type:
(int, int, decimal.Decimal)
Changed in version 4.4: Last element of the property changed from
float
todecimal.Decimal
to mitigate rounding issues.
- Time.hour_minute_second_nanosecond¶
The time as a tuple of (hour, minute, second, nanosecond).
- Time.tzinfo¶
The timezone of this time.
- Type:
datetime.tzinfo or None
Operations¶
- Time.__hash__()¶
- Time.__eq__(other)¶
== comparison with
Time
ordatetime.time
.
- Time.__ne__(other)¶
!= comparison with
Time
ordatetime.time
.
- Time.__lt__(other)¶
< comparison with
Time
ordatetime.time
.
- Time.__gt__(other)¶
> comparison with
Time
ordatetime.time
.
- Time.__le__(other)¶
<= comparison with
Time
ordatetime.time
.
- Time.__ge__(other)¶
>= comparison with
Time
ordatetime.time
.
Instance methods¶
- Time.replace(**kwargs)¶
Return a
Time
with one or more components replaced.- Keyword Arguments:
hour (int): overwrite the hour - default: self.hour
minute (int): overwrite the minute - default: self.minute
second (int): overwrite the second - default: int(self.second)
nanosecond (int): overwrite the nanosecond - default: self.nanosecond
tzinfo (datetime.tzinfo or None): overwrite the timezone - default: self.tzinfo
- Return type:
- Time.utc_offset()¶
Return the UTC offset of this time.
- Returns:
None if this is a local time (
tzinfo
is None), else returns self.tzinfo.utcoffset(self).- Return type:
- Raises:
ValueError – if self.tzinfo.utcoffset(self) is not None and a
timedelta
with a magnitude greater equal 1 day or that is not a whole number of minutes.TypeError – if self.tzinfo.utcoffset(self) does return anything but None or a
datetime.timedelta
.
- Time.dst()¶
Get the daylight saving time adjustment (DST).
- Returns:
None if this is a local time (
tzinfo
is None), else returns self.tzinfo.dst(self).- Return type:
- Raises:
ValueError – if self.tzinfo.dst(self) is not None and a
timedelta
with a magnitude greater equal 1 day or that is not a whole number of minutes.TypeError – if self.tzinfo.dst(self) does return anything but None or a
datetime.timedelta
.
- Time.tzname()¶
Get the name of the
Time
’s timezone.- Returns:
None if the time is local (i.e., has no timezone), else return self.tzinfo.tzname(self)
- Return type:
str or None
- Time.to_clock_time()¶
Convert to
ClockTime
.- Return type:
ClockTime
- Time.to_native()¶
Convert to a native Python datetime.time value.
This conversion is lossy as the native time implementation only supports a resolution of microseconds instead of nanoseconds.
- Return type:
- Time.__repr__()¶
- Time.__str__()¶
- Time.__format__(format_spec)¶
Special values¶
DateTime¶
- class neo4j.time.DateTime(year, month, day, hour=0, minute=0, second=0, nanosecond=0, tzinfo=None)¶
A point in time represented as a date and a time.
The
DateTime
class is a nanosecond-precision drop-in replacement for the standard librarydatetime.datetime
class.As such, it contains both
Date
andTime
information and draws functionality from those individual classes.A
DateTime
object is fully compatible with the Python time zone library pytz. Functions such as normalize and localize can be used in the same way as they are with the standard library classes.Regular construction of a
DateTime
object requires at least the year, month and day arguments to be supplied. The optional hour, minute and second arguments default to zero and tzinfo defaults toNone
.year, month, and day are passed to the constructor of
Date
. hour, minute, second, nanosecond, and tzinfo are passed to the constructor ofTime
. See their documentation for more details.>>> dt = DateTime(2018, 4, 30, 12, 34, 56, 789123456); dt neo4j.time.DateTime(2018, 4, 30, 12, 34, 56, 789123456) >>> dt.second 56.789123456
Class methods¶
- classmethod DateTime.now(tz=None)¶
Get the current date and time.
- Parameters:
tz (datetime.tzinfo` or None) – timezone. Set to None to create a local
DateTime
.- Return type:
- Raises:
OverflowError – if the timestamp is out of the range of values supported by the platform C localtime() function. It’s common for this to be restricted to years from 1970 through 2038.
- classmethod DateTime.from_timestamp(timestamp, tz=None)¶
DateTime
from a time stamp (seconds since unix epoch).- Parameters:
timestamp (float) – the unix timestamp (seconds since unix epoch).
tz (datetime.tzinfo or None) – timezone. Set to None to create a local
DateTime
.
- Return type:
- Raises:
OverflowError – if the timestamp is out of the range of values supported by the platform C localtime() function. It’s common for this to be restricted to years from 1970 through 2038.
- classmethod DateTime.utc_from_timestamp(timestamp)¶
DateTime
from a time stamp (seconds since unix epoch).Returns the DateTime as local date DateTime in UTC.
- Return type:
- classmethod DateTime.from_ordinal(ordinal)¶
DateTime
from an ordinal.For more info about ordinals see
Date.from_ordinal()
.- Return type:
- classmethod DateTime.combine(date, time)¶
Combine a
Date
and aTime
to aDateTime
.- Parameters:
- Return type:
- Raises:
AssertionError – if the parameter types don’t match.
- classmethod DateTime.from_native(dt)¶
Convert from a native Python
datetime.datetime
value.- Parameters:
dt (datetime.datetime) – the datetime to convert
- Return type:
- classmethod DateTime.from_clock_time(clock_time, epoch)¶
Convert from a
ClockTime
relative to a given epoch.- Parameters:
- Return type:
- Raises:
ValueError – if clock_time is invalid.
Class attributes¶
- DateTime.min = neo4j.time.DateTime(1, 1, 1, 0, 0, 0, 0)¶
The earliest date time value possible.
- DateTime.max = neo4j.time.DateTime(9999, 12, 31, 23, 59, 59, 999999999)¶
The latest date time value possible.
- DateTime.resolution = (0, 0, 0, 1)¶
The minimum resolution supported.
Instance attributes¶
- DateTime.year_month_day¶
The year_month_day of the
DateTime
’s date.See
Date.year_month_day
.
- DateTime.year_week_day¶
The year_week_day of the
DateTime
’s date.See
Date.year_week_day
.
- DateTime.year_day¶
The year_day of the
DateTime
’s date.See
Date.year_day
.
- DateTime.minute¶
The minute of the
DateTime
’s time.See
Time.minute
.
- DateTime.second¶
The second of the
DateTime
’s time.See
Time.second
.
- DateTime.tzinfo¶
The tzinfo of the
DateTime
’s time.See
Time.tzinfo
.
Operations¶
- DateTime.__hash__()¶
- DateTime.__eq__(other)¶
== comparison with
DateTime
ordatetime.datetime
.
- DateTime.__ne__(other)¶
!= comparison with
DateTime
ordatetime.datetime
.
- DateTime.__lt__(other)¶
< comparison with
DateTime
ordatetime.datetime
.
- DateTime.__gt__(other)¶
> comparison with
DateTime
ordatetime.datetime
.
- DateTime.__le__(other)¶
<= comparison with
DateTime
ordatetime.datetime
.
- DateTime.__ge__(other)¶
>= comparison with
DateTime
ordatetime.datetime
.
- DateTime.__add__(other)¶
Add a
datetime.timedelta
.- Return type:
- DateTime.__sub__(other)¶
Subtract a datetime or a timedelta.
Supported
DateTime
(returnsDuration
),datetime.datetime
(returnsdatetime.timedelta
), anddatetime.timedelta
(returnsDateTime
).- Return type:
Instance methods¶
- DateTime.replace(**kwargs)¶
Return a
DateTime
with one or more components replaced.See
Date.replace()
andTime.replace()
for available arguments.- Return type:
- DateTime.as_timezone(tz)¶
Convert this
DateTime
to another timezone.- Parameters:
tz (datetime.tzinfo or None) – the new timezone
- Returns:
the same object if tz is None. Else, a new
DateTime
that’s the same point in time but in a different timezone.- Return type:
- DateTime.utc_offset()¶
Get the date times utc offset.
See
Time.utc_offset()
.
- DateTime.dst()¶
Get the daylight saving time adjustment (DST).
See
Time.dst()
.
- DateTime.tzname()¶
Get the timezone name.
See
Time.tzname()
.
- DateTime.to_clock_time()¶
Convert to
ClockTime
.- Return type:
ClockTime
- DateTime.to_native()¶
Convert to a native Python
datetime.datetime
value.This conversion is lossy as the native time implementation only supports a resolution of microseconds instead of nanoseconds.
- Return type:
- DateTime.weekday()¶
Get the weekday.
See
Date.weekday()
- DateTime.iso_weekday()¶
Get the ISO weekday.
- DateTime.iso_calendar()¶
Get date as ISO tuple.
- DateTime.iso_format(sep='T')¶
Return the
DateTime
as ISO formatted string.This method joins self.date().iso_format() (see
Date.iso_format()
) and self.timetz().iso_format() (seeTime.iso_format()
) with sep in between.
- DateTime.__repr__()¶
- DateTime.__str__()¶
- DateTime.__format__(format_spec)¶
Special values¶
Duration¶
- class neo4j.time.Duration(years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0, subseconds=0, milliseconds=0, microseconds=0, nanoseconds=0)¶
A difference between two points in time.
A
Duration
represents the difference between two points in time. Duration objects store a composite value of months, days, seconds, and nanoseconds. Unlikedatetime.timedelta
however, days, and seconds/nanoseconds are never interchanged. All values except seconds and nanoseconds are applied separately in calculations.A
Duration
stores four primary instance attributes internally: months, days, seconds and nanoseconds. These are maintained as individual values and are immutable. Each of these four attributes can carry its own siggn, with the exception of nanoseconds, which always has the same sign as seconds. The constructor will establish this state, should the duration be initialized with conflicting seconds and nanoseconds signs. This structure allows the modelling of durations such as 3 months minus 2 days.To determine if a
Duration
d is overflowing the accepted values of the database, first, all nanoseconds outside the range -999_999_999 and 999_999_999 are transferred into the seconds field. Then, months, days, and seconds are summed up like so: months * 2629746 + days * 86400 + d.seconds + d.nanoseconds // 1000000000. (Like the integer division in Python, this one is to be understood as rounding down rather than towards 0.) This value must be between -(263) and (263 - 1) inclusive.- Parameters:
years (float) – will be added times 12 to months
weeks (float) – will be added times 7 to days
hours (float) – will be added times 3,600,000,000,000 to nanoseconds
minutes (float) – will be added times 60,000,000,000 to nanoseconds
seconds (float) – will be added times 1,000,000,000 to nanoseconds`
subseconds (float) –
will be added times 1,000,000,000 to nanosubseconds`
Deprecated since version 4.4: Will be removed in 5.0. Use milliseconds, microseconds, or nanoseconds instead or add subseconds to seconds
milliseconds (float) – will be added times 1,000,000 to nanoseconds
microseconds (float) – will be added times 1,000 to nanoseconds
nanoseconds (float) – will be truncated to
int
(int(nanoseconds))
- Raises:
ValueError – the components exceed the limits as described above.
Class attributes¶
- Duration.min = (0, 0, -9223372036854775808, 0)¶
The lowest duration value possible.
- Duration.max = (0, 0, 9223372036854775807, 999999999)¶
The highest duration value possible.
Instance attributes¶
- Duration.subseconds¶
The subseconds of the
Duration
.Deprecated since version 4.4: Will be removed in 5.0. Use
nanoseconds
instead.- Type:
- Duration.years_months_days¶
- Returns:
- Duration.hours_minutes_seconds¶
A 3-tuple of (hours, minutes, seconds).
Where seconds is a
decimal.Decimal
that combines seconds and nanoseconds.Deprecated since version 4.4: Will be removed in 5.0. Use
hours_minutes_seconds_nanoseconds
instead.- Type:
(int, int, decimal.Decimal)
Operations¶
- Duration.__bool__()¶
Falsy if all primary instance attributes are.
- Duration.__add__(other)¶
Add a
Duration
ordatetime.timedelta
.- Return type:
- Duration.__sub__(other)¶
Subtract a
Duration
ordatetime.timedelta
.- Return type:
- Duration.__pos__()¶
- Duration.__neg__()¶
- Duration.__abs__()¶
- Duration.__repr__()¶
- Duration.__str__()¶