module tarantool.msgpack_ext.types
¶
module tarantool.msgpack_ext.types.datetime
¶
Tarantool datetime extension type implementation module.
- class tarantool.msgpack_ext.types.datetime.Datetime(*, timestamp=None, year=None, month=None, day=None, hour=None, minute=None, sec=None, nsec=None, tzoffset=0, tz='', timestamp_since_utc_epoch=False)¶
Class representing Tarantool datetime info. Internals are based on
datetime.datetime
.You can create
Datetime
objects by using the same API as in Tarantool:dt1 = tarantool.Datetime(year=2022, month=8, day=31, hour=18, minute=7, sec=54, nsec=308543321) dt2 = tarantool.Datetime(timestamp=1661969274) dt3 = tarantool.Datetime(timestamp=1661969274, nsec=308543321)
Datetime
exposesyear
,month
,day
,hour
,minute
,sec
,nsec
,timestamp
andvalue
(integer epoch time with nanoseconds precision) properties if you need to convertDatetime
to any other kind of datetime object:pdt = pandas.Timestamp(year=dt.year, month=dt.month, day=dt.day, hour=dt.hour, minute=dt.minute, second=dt.sec, microsecond=(dt.nsec // 1000), nanosecond=(dt.nsec % 1000))
Use
tzoffset
parameter to set up offset timezone:dt = tarantool.Datetime(year=2022, month=8, day=31, hour=18, minute=7, sec=54, nsec=308543321, tzoffset=180)
You may use the
tzoffset
property to get the timezone offset of a datetime object.Use
tz
parameter to set up timezone name:dt = tarantool.Datetime(year=2022, month=8, day=31, hour=18, minute=7, sec=54, nsec=308543321, tz='Europe/Moscow')
If both
tz
andtzoffset
are specified,tz
is used.You may use the
tz
property to get the timezone name of a datetime object.- Parameters:
timestamp¶ (
float
orint
, optional) – Timestamp since epoch. Cannot be provided together withyear
,month
,day
,hour
,minute
,sec
. Ifnsec
is provided, it must beint
. Refer totimestamp_since_utc_epoch
to clarify how timezone-aware datetime is computed from the timestamp.year¶ (
int
, optional) – Datetime year value. Must be a validdatetime.datetime
year
parameter. Must be provided unless the object is built withdata
ortimestamp
.month¶ (
int
, optional) – Datetime month value. Must be a validdatetime.datetime
month
parameter. Must be provided unless the object is built withdata
ortimestamp
.day¶ (
int
, optional) – Datetime day value. Must be a validdatetime.datetime
day
parameter. Must be provided unless the object is built withdata
ortimestamp
.hour¶ (
int
, optional) – Datetime hour value. Must be a validdatetime.datetime
hour
parameter.minute¶ (
int
, optional) – Datetime minute value. Must be a validdatetime.datetime
minute
parameter.sec¶ (
int
, optional) – Datetime seconds value. Must be a validdatetime.datetime
second
parameter.nsec¶ – Datetime nanoseconds value.
tzoffset¶ (
int
, optional) – Timezone offset. Ignored, if provided together withtz
.tz¶ (
str
, optional) – Timezone name from Olson timezone database.timestamp_since_utc_epoch¶ (
bool
, optional) –Parameter to set timestamp conversion behavior for timezone-aware datetimes.
If
False
(default), behaves similar to Tarantool datetime.new():>>> dt = tarantool.Datetime(timestamp=1640995200, timestamp_since_utc_epoch=False) >>> dt datetime: Timestamp('2022-01-01 00:00:00'), tz: "" >>> dt.timestamp 1640995200.0 >>> dt = tarantool.Datetime(timestamp=1640995200, tz='Europe/Moscow', ... timestamp_since_utc_epoch=False) >>> dt datetime: Timestamp('2022-01-01 00:00:00+0300', tz='Europe/Moscow'), tz: "Europe/Moscow" >>> dt.timestamp 1640984400.0
Thus, if
False
, datetime is computed from timestamp since epoch and then timezone is applied without any conversion. In that case,timestamp
won’t be equal to initializationtimestamp
for all timezones with non-zero offset.If
True
, behaves similar todatetime.datetime
:>>> dt = tarantool.Datetime(timestamp=1640995200, timestamp_since_utc_epoch=True) >>> dt datetime: Timestamp('2022-01-01 00:00:00'), tz: "" >>> dt.timestamp 1640995200.0 >>> dt = tarantool.Datetime(timestamp=1640995200, tz='Europe/Moscow', ... timestamp_since_utc_epoch=True) >>> dt datetime: Timestamp('2022-01-01 03:00:00+0300', tz='Europe/Moscow'), tz: "Europe/Moscow" >>> dt.timestamp 1640995200.0
Thus, if
True
, datetime is computed in a way thattimestamp
will always be equal to initializationtimestamp
.
- Raise:
ValueError
,MsgpackError
,datetime.datetime
exceptions
- __add__(other)¶
Valid operations:
Since
Interval
could containmonth
andyear
fields and such operations could be ambiguous, you can use theadjust
field to tune the logic. The behavior is the same as in Tarantool, see Interval arithmetic RFC.tarantool.IntervalAdjust.NONE
– only truncation toward the end of month is performed (default mode).>>> dt = tarantool.Datetime(year=2022, month=3, day=31) datetime: Timestamp('2022-03-31 00:00:00'), tz: "" >>> di = tarantool.Interval(month=1, adjust=tarantool.IntervalAdjust.NONE) >>> dt + di datetime: Timestamp('2022-04-30 00:00:00'), tz: ""
tarantool.IntervalAdjust.EXCESS
– overflow mode, without any snap or truncation to the end of month, straight addition of days in month, stopping over month boundaries if there is less number of days.>>> dt = tarantool.Datetime(year=2022, month=1, day=31) datetime: Timestamp('2022-01-31 00:00:00'), tz: "" >>> di = tarantool.Interval(month=1, adjust=tarantool.IntervalAdjust.EXCESS) >>> dt + di datetime: Timestamp('2022-03-02 00:00:00'), tz: ""
tarantool.IntervalAdjust.LAST
– mode when day snaps to the end of month, if it happens.>>> dt = tarantool.Datetime(year=2022, month=2, day=28) datetime: Timestamp('2022-02-28 00:00:00'), tz: "" >>> di = tarantool.Interval(month=1, adjust=tarantool.IntervalAdjust.LAST) >>> dt + di datetime: Timestamp('2022-03-31 00:00:00'), tz: ""
- __eq__(other)¶
Datetimes are equal when underlying datetime infos are equal.
- __sub__(other)¶
Valid operations:
Refer to
__add__()
for interval adjustment rules.
module tarantool.msgpack_ext.types.interval
¶
Tarantool datetime.interval extension type implementation module.
- class tarantool.msgpack_ext.types.interval.Adjust(value)¶
Interval adjustment mode for year and month arithmetic. Refer to
__add__()
.- EXCESS = 0¶
Overflow mode.
- LAST = 2¶
Mode when day snaps to the end of month, if it happens.
- NONE = 1¶
Only truncation toward the end of month is performed.
- class tarantool.msgpack_ext.types.interval.Interval(*, year=0, month=0, week=0, day=0, hour=0, minute=0, sec=0, nsec=0, adjust=Adjust.NONE)¶
Class representing Tarantool datetime.interval info.
You can create
Interval
objects either from MessagePack data or by using the same API as in Tarantool:di = tarantool.Interval(year=-1, month=2, week=-3, day=4, hour=5, minute=-6, sec=7, nsec=308543321, adjust=tarantool.IntervalAdjust.NONE)
Its attributes (same as in init API) are exposed, so you can use them if needed.
- Parameters:
adjust¶ (
IntervalAdjust
, optional) – Interval adjustment rule. Refer to__add__()
.
- Raise:
- __add__(other)¶
Valid operations:
Adjust of the first operand is used in result.
- __eq__(other)¶
Compare equality of each field, no casts.