Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Doc/includes/tzinfo-examples.py at v3.5.3 · python/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
35.4k
Star
77.2k
Code
Issues
5k+
Pull requests
2.6k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
v3.5.3
Breadcrumbs
cpython
/
Doc
/
includes
/
tzinfo-examples.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
169 lines (131 loc) · 4.94 KB
v3.5.3
Breadcrumbs
cpython
/
Doc
/
includes
/
tzinfo-examples.py
Copy path
Top
File metadata and controls
Code
Blame
169 lines (131 loc) · 4.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from
datetime
import
tzinfo
,
timedelta
,
datetime
ZERO
=
timedelta
(
0
)
HOUR
=
timedelta
(
hours
=
1
)
# A UTC class.
class
UTC
(
tzinfo
):
"""UTC"""
def
utcoffset
(
self
,
dt
):
return
ZERO
def
tzname
(
self
,
dt
):
return
"UTC"
def
dst
(
self
,
dt
):
return
ZERO
utc
=
UTC
()
# A class building tzinfo objects for fixed-offset time zones.
# Note that FixedOffset(0, "UTC") is a different way to build a
# UTC tzinfo object.
class
FixedOffset
(
tzinfo
):
"""Fixed offset in minutes east from UTC."""
def
__init__
(
self
,
offset
,
name
):
self
.
__offset
=
timedelta
(
minutes
=
offset
)
self
.
__name
=
name
def
utcoffset
(
self
,
dt
):
return
self
.
__offset
def
tzname
(
self
,
dt
):
return
self
.
__name
def
dst
(
self
,
dt
):
return
ZERO
# A class capturing the platform's idea of local time.
import
time
as
_time
STDOFFSET
=
timedelta
(
seconds
=
-
_time
.
timezone
)
if
_time
.
daylight
:
DSTOFFSET
=
timedelta
(
seconds
=
-
_time
.
altzone
)
else
:
DSTOFFSET
=
STDOFFSET
DSTDIFF
=
DSTOFFSET
-
STDOFFSET
class
LocalTimezone
(
tzinfo
):
def
utcoffset
(
self
,
dt
):
if
self
.
_isdst
(
dt
):
return
DSTOFFSET
else
:
return
STDOFFSET
def
dst
(
self
,
dt
):
if
self
.
_isdst
(
dt
):
return
DSTDIFF
else
:
return
ZERO
def
tzname
(
self
,
dt
):
return
_time
.
tzname
[
self
.
_isdst
(
dt
)]
def
_isdst
(
self
,
dt
):
tt
=
(
dt
.
year
,
dt
.
month
,
dt
.
day
,
dt
.
hour
,
dt
.
minute
,
dt
.
second
,
dt
.
weekday
(),
0
,
0
)
stamp
=
_time
.
mktime
(
tt
)
tt
=
_time
.
localtime
(
stamp
)
return
tt
.
tm_isdst
>
0
Local
=
LocalTimezone
()
# A complete implementation of current DST rules for major US time zones.
def
first_sunday_on_or_after
(
dt
):
days_to_go
=
6
-
dt
.
weekday
()
if
days_to_go
:
dt
+=
timedelta
(
days_to_go
)
return
dt
# US DST Rules
#
# This is a simplified (i.e., wrong for a few cases) set of rules for US
# DST start and end times. For a complete and up-to-date set of DST rules
# and timezone definitions, visit the Olson Database (or try pytz):
# http://www.twinsun.com/tz/tz-link.htm
# http://sourceforge.net/projects/pytz/ (might not be up-to-date)
#
# In the US, since 2007, DST starts at 2am (standard time) on the second
# Sunday in March, which is the first Sunday on or after Mar 8.
DSTSTART_2007
=
datetime
(
1
,
3
,
8
,
2
)
# and ends at 2am (DST time; 1am standard time) on the first Sunday of Nov.
DSTEND_2007
=
datetime
(
1
,
11
,
1
,
1
)
# From 1987 to 2006, DST used to start at 2am (standard time) on the first
# Sunday in April and to end at 2am (DST time; 1am standard time) on the last
# Sunday of October, which is the first Sunday on or after Oct 25.
DSTSTART_1987_2006
=
datetime
(
1
,
4
,
1
,
2
)
DSTEND_1987_2006
=
datetime
(
1
,
10
,
25
,
1
)
# From 1967 to 1986, DST used to start at 2am (standard time) on the last
# Sunday in April (the one on or after April 24) and to end at 2am (DST time;
# 1am standard time) on the last Sunday of October, which is the first Sunday
# on or after Oct 25.
DSTSTART_1967_1986
=
datetime
(
1
,
4
,
24
,
2
)
DSTEND_1967_1986
=
DSTEND_1987_2006
class
USTimeZone
(
tzinfo
):
def
__init__
(
self
,
hours
,
reprname
,
stdname
,
dstname
):
self
.
stdoffset
=
timedelta
(
hours
=
hours
)
self
.
reprname
=
reprname
self
.
stdname
=
stdname
self
.
dstname
=
dstname
def
__repr__
(
self
):
return
self
.
reprname
def
tzname
(
self
,
dt
):
if
self
.
dst
(
dt
):
return
self
.
dstname
else
:
return
self
.
stdname
def
utcoffset
(
self
,
dt
):
return
self
.
stdoffset
+
self
.
dst
(
dt
)
def
dst
(
self
,
dt
):
if
dt
is
None
or
dt
.
tzinfo
is
None
:
# An exception may be sensible here, in one or both cases.
# It depends on how you want to treat them. The default
# fromutc() implementation (called by the default astimezone()
# implementation) passes a datetime with dt.tzinfo is self.
return
ZERO
assert
dt
.
tzinfo
is
self
# Find start and end times for US DST. For years before 1967, return
# ZERO for no DST.
if
2006
<
dt
.
year
:
dststart
,
dstend
=
DSTSTART_2007
,
DSTEND_2007
elif
1986
<
dt
.
year
<
2007
:
dststart
,
dstend
=
DSTSTART_1987_2006
,
DSTEND_1987_2006
elif
1966
<
dt
.
year
<
1987
:
dststart
,
dstend
=
DSTSTART_1967_1986
,
DSTEND_1967_1986
else
:
return
ZERO
start
=
first_sunday_on_or_after
(
dststart
.
replace
(
year
=
dt
.
year
))
end
=
first_sunday_on_or_after
(
dstend
.
replace
(
year
=
dt
.
year
))
# Can't compare naive to aware objects, so strip the timezone from
# dt first.
if
start
<=
dt
.
replace
(
tzinfo
=
None
)
<
end
:
return
HOUR
else
:
return
ZERO
Eastern
=
USTimeZone
(
-
5
,
"Eastern"
,
"EST"
,
"EDT"
)
Central
=
USTimeZone
(
-
6
,
"Central"
,
"CST"
,
"CDT"
)
Mountain
=
USTimeZone
(
-
7
,
"Mountain"
,
"MST"
,
"MDT"
)
Pacific
=
USTimeZone
(
-
8
,
"Pacific"
,
"PST"
,
"PDT"
)
You can’t perform that action at this time.