Skip to content

Commit 9f2aa5d

Browse files
Update transition dates and add a module for generating them
1 parent c83da7c commit 9f2aa5d

File tree

4 files changed

+89
-38
lines changed

4 files changed

+89
-38
lines changed

requirements_dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pandas

sp2ts/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
try:
2-
#py2
3-
from sp2ts import sp2ts, to_unixtime, from_unixtime, sp2dt, ts2sp, dt2sp
4-
except:
5-
#py3+
6-
from sp2ts.sp2ts import sp2ts, to_unixtime, from_unixtime, sp2dt, ts2sp, dt2sp
1+
from sp2ts.sp2ts import sp2ts, to_unixtime, from_unixtime, sp2dt, ts2sp, dt2sp
72

83
__all__ = ["sp2ts", "to_unixtime", "from_unixtime", "sp2dt", "ts2sp", "dt2sp"]

sp2ts/get_transition_dates.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Utility for generating a list of transition date timestamps for use in sp2ts.
3+
Always test the outputs carefully in case there have been changes in the behvaiour of pytz or
4+
datetime!
5+
6+
Jamie Taylor
7+
2024-02-20
8+
"""
9+
10+
from datetime import datetime, time
11+
from pytz import timezone
12+
13+
import pandas as pd
14+
15+
from sp2ts import to_unixtime
16+
17+
def main():
18+
tz = timezone("Europe/London")
19+
transition_dates = pd.DataFrame([
20+
[d.year, d.date(), to_unixtime(datetime.combine(d.date(), time(0)), timezone_="UTC")]
21+
for d in tz._utc_transition_times[20:]
22+
], columns=["year", "transition_date", "transition_date_ts"])
23+
transition_dates = transition_dates.loc[(transition_dates.year >= 1990)]
24+
transition_dates["transition_number"] = transition_dates.groupby("year")["transition_date"]\
25+
.rank().astype("Int64")
26+
transition_dates_ = transition_dates.set_index(["year", "transition_number"])\
27+
.unstack(level=-1)\
28+
.reset_index()\
29+
.reset_index()\
30+
.values
31+
print(
32+
"TRANSITION_DATES_TS = [\r\n ",
33+
"\r\n ".join([f"({t[4]}, {t[5]}), # {t[1]}: {t[2]}, {t[3]}" for t in transition_dates_]),
34+
"\r\n]"
35+
)
36+
37+
if __name__ == "__main__":
38+
main()

sp2ts/sp2ts.py

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,56 @@
1414
import pytz
1515
import numpy as np
1616

17+
# See get_transition_dates.py
1718
TRANSITION_DATES_TS = [
18-
(922579200, 941328000), # 1999
19-
(954028800, 972777600), # 2000
20-
(985478400, 1004227200), # 2001
21-
(1017532800, 1035676800), # 2002
22-
(1048982400, 1067126400), # 2003
23-
(1080432000, 1099180800), # 2004
24-
(1111881600, 1130630400), # 2005
25-
(1143331200, 1162080000), # 2006
26-
(1174780800, 1193529600), # 2007
27-
(1206835200, 1224979200), # 2008
28-
(1238284800, 1256428800), # 2009
29-
(1269734400, 1288483200), # 2010
30-
(1301184000, 1319932800), # 2011
31-
(1332633600, 1351382400), # 2012
32-
(1364688000, 1382832000), # 2013
33-
(1396137600, 1414281600), # 2014
34-
(1427587200, 1445731200), # 2015
35-
(1459036800, 1477785600), # 2016
36-
(1490486400, 1509235200), # 2017
37-
(1521936000, 1540684800), # 2018
38-
(1553990400, 1572134400), # 2019
39-
(1585440000, 1603584000), # 2020
40-
(1616889600, 1635638400), # 2021
41-
(1648339200, 1667088000), # 2022
42-
(1679788800, 1698537600), # 2023
43-
(1711843200, 1729987200), # 2024
44-
(1743292800, 1761436800), # 2025
45-
(1774742400, 1792886400), # 2026
46-
(1806192000, 1824940800), # 2027
47-
(1837641600, 1856390400), # 2028
48-
(1869091200, 1887840000), # 2029
49-
(1901145600, 1919289600) # 2030
19+
(638323200, 657072000), # 1990: 1990-03-25, 1990-10-28
20+
(670377600, 688521600), # 1991: 1991-03-31, 1991-10-27
21+
(701827200, 719971200), # 1992: 1992-03-29, 1992-10-25
22+
(733276800, 751420800), # 1993: 1993-03-28, 1993-10-24
23+
(764726400, 782870400), # 1994: 1994-03-27, 1994-10-23
24+
(796176000, 814320000), # 1995: 1995-03-26, 1995-10-22
25+
(828230400, 846374400), # 1996: 1996-03-31, 1996-10-27
26+
(859680000, 877824000), # 1997: 1997-03-30, 1997-10-26
27+
(891129600, 909273600), # 1998: 1998-03-29, 1998-10-25
28+
(922579200, 941328000), # 1999: 1999-03-28, 1999-10-31
29+
(954028800, 972777600), # 2000: 2000-03-26, 2000-10-29
30+
(985478400, 1004227200), # 2001: 2001-03-25, 2001-10-28
31+
(1017532800, 1035676800), # 2002: 2002-03-31, 2002-10-27
32+
(1048982400, 1067126400), # 2003: 2003-03-30, 2003-10-26
33+
(1080432000, 1099180800), # 2004: 2004-03-28, 2004-10-31
34+
(1111881600, 1130630400), # 2005: 2005-03-27, 2005-10-30
35+
(1143331200, 1162080000), # 2006: 2006-03-26, 2006-10-29
36+
(1174780800, 1193529600), # 2007: 2007-03-25, 2007-10-28
37+
(1206835200, 1224979200), # 2008: 2008-03-30, 2008-10-26
38+
(1238284800, 1256428800), # 2009: 2009-03-29, 2009-10-25
39+
(1269734400, 1288483200), # 2010: 2010-03-28, 2010-10-31
40+
(1301184000, 1319932800), # 2011: 2011-03-27, 2011-10-30
41+
(1332633600, 1351382400), # 2012: 2012-03-25, 2012-10-28
42+
(1364688000, 1382832000), # 2013: 2013-03-31, 2013-10-27
43+
(1396137600, 1414281600), # 2014: 2014-03-30, 2014-10-26
44+
(1427587200, 1445731200), # 2015: 2015-03-29, 2015-10-25
45+
(1459036800, 1477785600), # 2016: 2016-03-27, 2016-10-30
46+
(1490486400, 1509235200), # 2017: 2017-03-26, 2017-10-29
47+
(1521936000, 1540684800), # 2018: 2018-03-25, 2018-10-28
48+
(1553990400, 1572134400), # 2019: 2019-03-31, 2019-10-27
49+
(1585440000, 1603584000), # 2020: 2020-03-29, 2020-10-25
50+
(1616889600, 1635638400), # 2021: 2021-03-28, 2021-10-31
51+
(1648339200, 1667088000), # 2022: 2022-03-27, 2022-10-30
52+
(1679788800, 1698537600), # 2023: 2023-03-26, 2023-10-29
53+
(1711843200, 1729987200), # 2024: 2024-03-31, 2024-10-27
54+
(1743292800, 1761436800), # 2025: 2025-03-30, 2025-10-26
55+
(1774742400, 1792886400), # 2026: 2026-03-29, 2026-10-25
56+
(1806192000, 1824940800), # 2027: 2027-03-28, 2027-10-31
57+
(1837641600, 1856390400), # 2028: 2028-03-26, 2028-10-29
58+
(1869091200, 1887840000), # 2029: 2029-03-25, 2029-10-28
59+
(1901145600, 1919289600), # 2030: 2030-03-31, 2030-10-27
60+
(1932595200, 1950739200), # 2031: 2031-03-30, 2031-10-26
61+
(1964044800, 1982793600), # 2032: 2032-03-28, 2032-10-31
62+
(1995494400, 2014243200), # 2033: 2033-03-27, 2033-10-30
63+
(2026944000, 2045692800), # 2034: 2034-03-26, 2034-10-29
64+
(2058393600, 2077142400), # 2035: 2035-03-25, 2035-10-28
65+
(2090448000, 2108592000), # 2036: 2036-03-30, 2036-10-26
66+
(2121897600, 2140041600), # 2037: 2037-03-29, 2037-10-25
5067
]
5168

5269
def to_unixtime(datetime_: datetime, timezone_: Optional[str] = None) -> int:

0 commit comments

Comments
 (0)