-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
msgpack: support tzindex in datetime
Support non-zero tzindex in datetime extended type. If both tzoffset and tzindex are specified, tzindex is prior (same as in Tarantool [1]). 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') ``` You may use `tz` property to get timezone name of a datetime object. pytz is used to build timezone info. Tarantool index to Olson name map and inverted one are built with gen_timezones.sh script based on tarantool/go-tarantool script [2]. All Tarantool unique and alias timezones presents in pytz.all_timezones list. Only the following abbreviated timezones from Tarantool presents in pytz.all_timezones (version 2022.2.1): - CET - EET - EST - GMT - HST - MST - UTC - WET pytz does not natively support work with abbreviated timezones due to its possibly ambiguous nature [3-5]. Tarantool itself do not support work with ambiguous abbreviated timezones: ``` Tarantool 2.10.1-0-g482d91c66 tarantool> datetime.new({tz = 'BST'}) --- - error: 'builtin/datetime.lua:477: could not parse ''BST'' - ambiguous timezone' ... ``` If ambiguous timezone is specified, the exception is raised. Tarantool header timezones.h [6] provides a map for all abbreviated timezones with category info (all ambiguous timezones are marked with TZ_AMBIGUOUS flag) and offset info. We parse this info to build pytz.FixedOffset() timezone for each Tarantool abbreviated timezone not supported natively by pytz. 1. https://www.tarantool.io/en/doc/latest/reference/reference_lua/datetime/new/ 2. https://github.com/tarantool/go-tarantool/blob/5801dc6f5ce69db7c8bc0c0d0fe4fb6042d5ecbc/datetime/gen-timezones.sh 3. https://stackoverflow.com/questions/37109945/how-to-use-abbreviated-timezone-namepst-ist-in-pytz 4. https://stackoverflow.com/questions/27531718/datetime-timezone-conversion-using-pytz 5. https://stackoverflow.com/questions/30315485/pytz-return-olson-timezone-name-from-only-a-gmt-offset 6. https://github.com/tarantool/tarantool/9ee45289e01232b8df1413efea11db170ae3b3b4/src/lib/tzcode/timezones.h Closes #204
- Loading branch information
1 parent
229f63d
commit e71a4ac
Showing
7 changed files
with
2,020 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from tarantool.msgpack_ext.types.timezones.timezones import ( | ||
TZ_AMBIGUOUS, | ||
indexToTimezone, | ||
timezoneToIndex, | ||
timezoneAbbrevInfo, | ||
) | ||
|
||
__all__ = ['TZ_AMBIGUOUS', 'indexToTimezone', 'timezoneToIndex', | ||
'timezoneAbbrevInfo'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env bash | ||
set -xeuo pipefail | ||
|
||
SRC_COMMIT="9ee45289e01232b8df1413efea11db170ae3b3b4" | ||
SRC_FILE=timezones.h | ||
DST_FILE=timezones.py | ||
|
||
[ -e ${SRC_FILE} ] && rm ${SRC_FILE} | ||
wget -O ${SRC_FILE} \ | ||
https://raw.githubusercontent.com/tarantool/tarantool/${SRC_COMMIT}/src/lib/tzcode/timezones.h | ||
|
||
# We don't need aliases in indexToTimezone because Tarantool always replace it: | ||
# | ||
# tarantool> T = date.parse '2022-01-01T00:00 Pacific/Enderbury' | ||
# --- | ||
# ... | ||
# tarantool> T | ||
# --- | ||
# - 2022-01-01T00:00:00 Pacific/Kanton | ||
# ... | ||
# | ||
# So we can do the same and don't worry, be happy. | ||
|
||
cat <<EOF > ${DST_FILE} | ||
# Automatically generated by gen-timezones.sh | ||
TZ_UTC = 0x01 | ||
TZ_RFC = 0x02 | ||
TZ_MILITARY = 0x04 | ||
TZ_AMBIGUOUS = 0x08 | ||
TZ_NYI = 0x10 | ||
TZ_OLSON = 0x20 | ||
TZ_ALIAS = 0x40 | ||
TZ_DST = 0x80 | ||
indexToTimezone = { | ||
EOF | ||
|
||
grep ZONE_ABBREV ${SRC_FILE} | sed "s/ZONE_ABBREV( *//g" | sed "s/[),]//g" \ | ||
| awk '{printf("\t%s : %s,\n", $1, $3)}' >> ${DST_FILE} | ||
grep ZONE_UNIQUE ${SRC_FILE} | sed "s/ZONE_UNIQUE( *//g" | sed "s/[),]//g" \ | ||
| awk '{printf("\t%s : %s,\n", $1, $2)}' >> ${DST_FILE} | ||
|
||
cat <<EOF >> ${DST_FILE} | ||
} | ||
timezoneToIndex = { | ||
EOF | ||
|
||
grep ZONE_ABBREV ${SRC_FILE} | sed "s/ZONE_ABBREV( *//g" | sed "s/[),]//g" \ | ||
| awk '{printf("\t%s : %s,\n", $3, $1)}' >> ${DST_FILE} | ||
grep ZONE_UNIQUE ${SRC_FILE} | sed "s/ZONE_UNIQUE( *//g" | sed "s/[),]//g" \ | ||
| awk '{printf("\t%s : %s,\n", $2, $1)}' >> ${DST_FILE} | ||
grep ZONE_ALIAS ${SRC_FILE} | sed "s/ZONE_ALIAS( *//g" | sed "s/[),]//g" \ | ||
| awk '{printf("\t%s : %s,\n", $2, $1)}' >> ${DST_FILE} | ||
|
||
cat <<EOF >> ${DST_FILE} | ||
} | ||
timezoneAbbrevInfo = { | ||
EOF | ||
|
||
grep ZONE_ABBREV ${SRC_FILE} | sed "s/ZONE_ABBREV( *//g" | sed "s/[),]//g" \ | ||
| awk '{printf("\t%s : {\"offset\" : %d, \"category\" : %s},\n", $3, $2, $4)}' >> ${DST_FILE} | ||
echo "}" >> ${DST_FILE} | ||
|
||
rm timezones.h | ||
|
||
python validate_timezones.py |
Oops, something went wrong.