Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 3.142.200.102
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
49.27
KB
-rw-r--r--
__builtin__.html
10.26
KB
-rw-r--r--
__future__.html
13.79
KB
-rw-r--r--
__main__.html
7.05
KB
-rw-r--r--
_winreg.html
59.21
KB
-rw-r--r--
abc.html
23.9
KB
-rw-r--r--
aepack.html
13.16
KB
-rw-r--r--
aetools.html
14.91
KB
-rw-r--r--
aetypes.html
18.88
KB
-rw-r--r--
aifc.html
22.4
KB
-rw-r--r--
al.html
17.34
KB
-rw-r--r--
allos.html
33.72
KB
-rw-r--r--
anydbm.html
16.33
KB
-rw-r--r--
archiving.html
9.26
KB
-rw-r--r--
argparse.html
237.62
KB
-rw-r--r--
array.html
29.29
KB
-rw-r--r--
ast.html
34.98
KB
-rw-r--r--
asynchat.html
31.43
KB
-rw-r--r--
asyncore.html
36.51
KB
-rw-r--r--
atexit.html
16.8
KB
-rw-r--r--
audioop.html
31.36
KB
-rw-r--r--
autogil.html
8.19
KB
-rw-r--r--
base64.html
19.67
KB
-rw-r--r--
basehttpserver.html
34.04
KB
-rw-r--r--
bastion.html
11.04
KB
-rw-r--r--
bdb.html
36.68
KB
-rw-r--r--
binascii.html
20.67
KB
-rw-r--r--
binhex.html
10.58
KB
-rw-r--r--
bisect.html
23.24
KB
-rw-r--r--
bsddb.html
26.43
KB
-rw-r--r--
bz2.html
26.08
KB
-rw-r--r--
calendar.html
37.79
KB
-rw-r--r--
carbon.html
48.94
KB
-rw-r--r--
cd.html
27.96
KB
-rw-r--r--
cgi.html
49.92
KB
-rw-r--r--
cgihttpserver.html
13.1
KB
-rw-r--r--
cgitb.html
11.41
KB
-rw-r--r--
chunk.html
14.66
KB
-rw-r--r--
cmath.html
25.63
KB
-rw-r--r--
cmd.html
26.09
KB
-rw-r--r--
code.html
24.58
KB
-rw-r--r--
codecs.html
100.64
KB
-rw-r--r--
codeop.html
14.84
KB
-rw-r--r--
collections.html
133.96
KB
-rw-r--r--
colorpicker.html
7.52
KB
-rw-r--r--
colorsys.html
11.04
KB
-rw-r--r--
commands.html
14.36
KB
-rw-r--r--
compileall.html
16.83
KB
-rw-r--r--
compiler.html
67.75
KB
-rw-r--r--
configparser.html
62.13
KB
-rw-r--r--
constants.html
12.83
KB
-rw-r--r--
contextlib.html
19.39
KB
-rw-r--r--
cookie.html
39.07
KB
-rw-r--r--
cookielib.html
83.82
KB
-rw-r--r--
copy.html
12.19
KB
-rw-r--r--
copy_reg.html
13.76
KB
-rw-r--r--
crypt.html
10.04
KB
-rw-r--r--
crypto.html
7.59
KB
-rw-r--r--
csv.html
67.37
KB
-rw-r--r--
ctypes.html
238.78
KB
-rw-r--r--
curses.ascii.html
22.29
KB
-rw-r--r--
curses.html
146.63
KB
-rw-r--r--
curses.panel.html
14.39
KB
-rw-r--r--
custominterp.html
7.62
KB
-rw-r--r--
datatypes.html
16.84
KB
-rw-r--r--
datetime.html
226.59
KB
-rw-r--r--
dbhash.html
15.48
KB
-rw-r--r--
dbm.html
12.07
KB
-rw-r--r--
debug.html
10.15
KB
-rw-r--r--
decimal.html
194.44
KB
-rw-r--r--
development.html
14.17
KB
-rw-r--r--
difflib.html
84.83
KB
-rw-r--r--
dircache.html
11.41
KB
-rw-r--r--
dis.html
69.95
KB
-rw-r--r--
distutils.html
8.05
KB
-rw-r--r--
dl.html
16.33
KB
-rw-r--r--
doctest.html
165.54
KB
-rw-r--r--
docxmlrpcserver.html
16.43
KB
-rw-r--r--
dumbdbm.html
14.02
KB
-rw-r--r--
dummy_thread.html
9.43
KB
-rw-r--r--
dummy_threading.html
8.37
KB
-rw-r--r--
easydialogs.html
30.55
KB
-rw-r--r--
email-examples.html
45.65
KB
-rw-r--r--
email.charset.html
26.8
KB
-rw-r--r--
email.encoders.html
11.86
KB
-rw-r--r--
email.errors.html
15.77
KB
-rw-r--r--
email.generator.html
20.77
KB
-rw-r--r--
email.header.html
26.92
KB
-rw-r--r--
email.html
44.24
KB
-rw-r--r--
email.iterators.html
11.52
KB
-rw-r--r--
email.message.html
63.16
KB
-rw-r--r--
email.mime.html
27.93
KB
-rw-r--r--
email.parser.html
30.45
KB
-rw-r--r--
email.util.html
24.46
KB
-rw-r--r--
errno.html
37.99
KB
-rw-r--r--
exceptions.html
56.13
KB
-rw-r--r--
fcntl.html
22.67
KB
-rw-r--r--
filecmp.html
22.3
KB
-rw-r--r--
fileformats.html
9.14
KB
-rw-r--r--
fileinput.html
24.28
KB
-rw-r--r--
filesys.html
10.2
KB
-rw-r--r--
fl.html
49.92
KB
-rw-r--r--
fm.html
11.91
KB
-rw-r--r--
fnmatch.html
14.58
KB
-rw-r--r--
formatter.html
34.06
KB
-rw-r--r--
fpectl.html
16.01
KB
-rw-r--r--
fpformat.html
10.59
KB
-rw-r--r--
fractions.html
22.61
KB
-rw-r--r--
framework.html
33.34
KB
-rw-r--r--
frameworks.html
7.14
KB
-rw-r--r--
ftplib.html
43.99
KB
-rw-r--r--
functions.html
183.14
KB
-rw-r--r--
functools.html
27.17
KB
-rw-r--r--
future_builtins.html
13.04
KB
-rw-r--r--
gc.html
25.75
KB
-rw-r--r--
gdbm.html
15.96
KB
-rw-r--r--
gensuitemodule.html
11.51
KB
-rw-r--r--
getopt.html
23.66
KB
-rw-r--r--
getpass.html
10.65
KB
-rw-r--r--
gettext.html
78.76
KB
-rw-r--r--
gl.html
22.09
KB
-rw-r--r--
glob.html
13.26
KB
-rw-r--r--
grp.html
10.49
KB
-rw-r--r--
gzip.html
18.99
KB
-rw-r--r--
hashlib.html
18.2
KB
-rw-r--r--
heapq.html
31.61
KB
-rw-r--r--
hmac.html
10.46
KB
-rw-r--r--
hotshot.html
18.65
KB
-rw-r--r--
htmllib.html
25.32
KB
-rw-r--r--
htmlparser.html
39.11
KB
-rw-r--r--
httplib.html
62.95
KB
-rw-r--r--
i18n.html
9.52
KB
-rw-r--r--
ic.html
17.17
KB
-rw-r--r--
idle.html
20.9
KB
-rw-r--r--
imageop.html
14.76
KB
-rw-r--r--
imaplib.html
51.99
KB
-rw-r--r--
imgfile.html
11.71
KB
-rw-r--r--
imghdr.html
11.3
KB
-rw-r--r--
imp.html
34.34
KB
-rw-r--r--
importlib.html
8.26
KB
-rw-r--r--
imputil.html
31.81
KB
-rw-r--r--
index.html
72.78
KB
-rw-r--r--
inspect.html
50.71
KB
-rw-r--r--
internet.html
24.87
KB
-rw-r--r--
intro.html
8.93
KB
-rw-r--r--
io.html
98.13
KB
-rw-r--r--
ipc.html
13.41
KB
-rw-r--r--
itertools.html
115.91
KB
-rw-r--r--
jpeg.html
12.74
KB
-rw-r--r--
json.html
67.04
KB
-rw-r--r--
keyword.html
7.68
KB
-rw-r--r--
language.html
11.03
KB
-rw-r--r--
linecache.html
10.59
KB
-rw-r--r--
locale.html
55.14
KB
-rw-r--r--
logging.config.html
63.36
KB
-rw-r--r--
logging.handlers.html
69.64
KB
-rw-r--r--
logging.html
95.64
KB
-rw-r--r--
mac.html
21.79
KB
-rw-r--r--
macos.html
14.76
KB
-rw-r--r--
macosa.html
12.96
KB
-rw-r--r--
macostools.html
15.52
KB
-rw-r--r--
macpath.html
7.76
KB
-rw-r--r--
mailbox.html
156.75
KB
-rw-r--r--
mailcap.html
13.21
KB
-rw-r--r--
markup.html
18.77
KB
-rw-r--r--
marshal.html
17.98
KB
-rw-r--r--
math.html
39.24
KB
-rw-r--r--
md5.html
13.97
KB
-rw-r--r--
mhlib.html
21.54
KB
-rw-r--r--
mimetools.html
19.25
KB
-rw-r--r--
mimetypes.html
28.39
KB
-rw-r--r--
mimewriter.html
15.02
KB
-rw-r--r--
mimify.html
13.36
KB
-rw-r--r--
miniaeframe.html
12.2
KB
-rw-r--r--
misc.html
6.87
KB
-rw-r--r--
mm.html
9.03
KB
-rw-r--r--
mmap.html
28.36
KB
-rw-r--r--
modulefinder.html
15.31
KB
-rw-r--r--
modules.html
8.46
KB
-rw-r--r--
msilib.html
52.43
KB
-rw-r--r--
msvcrt.html
19.37
KB
-rw-r--r--
multifile.html
24.3
KB
-rw-r--r--
multiprocessing.html
365.71
KB
-rw-r--r--
mutex.html
11.23
KB
-rw-r--r--
netdata.html
16.98
KB
-rw-r--r--
netrc.html
12.3
KB
-rw-r--r--
new.html
12.12
KB
-rw-r--r--
nis.html
10.64
KB
-rw-r--r--
nntplib.html
41.92
KB
-rw-r--r--
numbers.html
37.75
KB
-rw-r--r--
numeric.html
13.55
KB
-rw-r--r--
operator.html
82
KB
-rw-r--r--
optparse.html
222.56
KB
-rw-r--r--
os.html
214.25
KB
-rw-r--r--
os.path.html
38.34
KB
-rw-r--r--
ossaudiodev.html
41.5
KB
-rw-r--r--
othergui.html
9.08
KB
-rw-r--r--
parser.html
39.36
KB
-rw-r--r--
pdb.html
33.96
KB
-rw-r--r--
persistence.html
14.87
KB
-rw-r--r--
pickle.html
102.27
KB
-rw-r--r--
pickletools.html
10.63
KB
-rw-r--r--
pipes.html
18.01
KB
-rw-r--r--
pkgutil.html
25.11
KB
-rw-r--r--
platform.html
28.37
KB
-rw-r--r--
plistlib.html
17.03
KB
-rw-r--r--
popen2.html
25.43
KB
-rw-r--r--
poplib.html
22.32
KB
-rw-r--r--
posix.html
14.41
KB
-rw-r--r--
posixfile.html
19.76
KB
-rw-r--r--
pprint.html
29.92
KB
-rw-r--r--
profile.html
63.56
KB
-rw-r--r--
pty.html
9.48
KB
-rw-r--r--
pwd.html
11.43
KB
-rw-r--r--
py_compile.html
11.12
KB
-rw-r--r--
pyclbr.html
14.71
KB
-rw-r--r--
pydoc.html
11.48
KB
-rw-r--r--
pyexpat.html
71.53
KB
-rw-r--r--
python.html
12.27
KB
-rw-r--r--
queue.html
24.22
KB
-rw-r--r--
quopri.html
11.9
KB
-rw-r--r--
random.html
37.83
KB
-rw-r--r--
re.html
134.74
KB
-rw-r--r--
readline.html
28.24
KB
-rw-r--r--
repr.html
20.43
KB
-rw-r--r--
resource.html
26.48
KB
-rw-r--r--
restricted.html
11.65
KB
-rw-r--r--
rexec.html
37.41
KB
-rw-r--r--
rfc822.html
42.22
KB
-rw-r--r--
rlcompleter.html
13.51
KB
-rw-r--r--
robotparser.html
12.27
KB
-rw-r--r--
runpy.html
19.34
KB
-rw-r--r--
sched.html
18.54
KB
-rw-r--r--
scrolledtext.html
9.32
KB
-rw-r--r--
select.html
39.67
KB
-rw-r--r--
sets.html
36.92
KB
-rw-r--r--
sgi.html
9.71
KB
-rw-r--r--
sgmllib.html
30.77
KB
-rw-r--r--
sha.html
12.09
KB
-rw-r--r--
shelve.html
27.02
KB
-rw-r--r--
shlex.html
32.1
KB
-rw-r--r--
shutil.html
40.22
KB
-rw-r--r--
signal.html
31.14
KB
-rw-r--r--
simplehttpserver.html
18.41
KB
-rw-r--r--
simplexmlrpcserver.html
31.39
KB
-rw-r--r--
site.html
23.64
KB
-rw-r--r--
smtpd.html
12.46
KB
-rw-r--r--
smtplib.html
42.13
KB
-rw-r--r--
sndhdr.html
10.02
KB
-rw-r--r--
socket.html
106.34
KB
-rw-r--r--
socketserver.html
59.83
KB
-rw-r--r--
someos.html
15.11
KB
-rw-r--r--
spwd.html
10.33
KB
-rw-r--r--
sqlite3.html
139.5
KB
-rw-r--r--
ssl.html
65.62
KB
-rw-r--r--
stat.html
32.31
KB
-rw-r--r--
statvfs.html
10.6
KB
-rw-r--r--
stdtypes.html
260.4
KB
-rw-r--r--
string.html
106.65
KB
-rw-r--r--
stringio.html
18.81
KB
-rw-r--r--
stringprep.html
16.13
KB
-rw-r--r--
strings.html
14.93
KB
-rw-r--r--
struct.html
40.88
KB
-rw-r--r--
subprocess.html
84.91
KB
-rw-r--r--
sun.html
6.84
KB
-rw-r--r--
sunau.html
27.1
KB
-rw-r--r--
sunaudio.html
17.79
KB
-rw-r--r--
symbol.html
7.66
KB
-rw-r--r--
symtable.html
22.94
KB
-rw-r--r--
sys.html
98.7
KB
-rw-r--r--
sysconfig.html
23.84
KB
-rw-r--r--
syslog.html
17.92
KB
-rw-r--r--
tabnanny.html
10.63
KB
-rw-r--r--
tarfile.html
78.68
KB
-rw-r--r--
telnetlib.html
25.48
KB
-rw-r--r--
tempfile.html
29.42
KB
-rw-r--r--
termios.html
16.01
KB
-rw-r--r--
test.html
52.62
KB
-rw-r--r--
textwrap.html
27.25
KB
-rw-r--r--
thread.html
20.47
KB
-rw-r--r--
threading.html
76.69
KB
-rw-r--r--
time.html
56.93
KB
-rw-r--r--
timeit.html
36.27
KB
-rw-r--r--
tix.html
46.96
KB
-rw-r--r--
tk.html
23.64
KB
-rw-r--r--
tkinter.html
67.67
KB
-rw-r--r--
token.html
19.62
KB
-rw-r--r--
tokenize.html
18.45
KB
-rw-r--r--
trace.html
25.54
KB
-rw-r--r--
traceback.html
33.44
KB
-rw-r--r--
ttk.html
101.75
KB
-rw-r--r--
tty.html
9.06
KB
-rw-r--r--
turtle.html
211.74
KB
-rw-r--r--
types.html
27.59
KB
-rw-r--r--
undoc.html
23.16
KB
-rw-r--r--
unicodedata.html
18.55
KB
-rw-r--r--
unittest.html
202.85
KB
-rw-r--r--
unix.html
10.55
KB
-rw-r--r--
urllib.html
58.68
KB
-rw-r--r--
urllib2.html
100.58
KB
-rw-r--r--
urlparse.html
40.41
KB
-rw-r--r--
user.html
11.83
KB
-rw-r--r--
userdict.html
29.73
KB
-rw-r--r--
uu.html
11.03
KB
-rw-r--r--
uuid.html
28.19
KB
-rw-r--r--
warnings.html
46.6
KB
-rw-r--r--
wave.html
22.22
KB
-rw-r--r--
weakref.html
36.52
KB
-rw-r--r--
webbrowser.html
23.07
KB
-rw-r--r--
whichdb.html
8.85
KB
-rw-r--r--
windows.html
9.33
KB
-rw-r--r--
winsound.html
18.75
KB
-rw-r--r--
wsgiref.html
81.04
KB
-rw-r--r--
xdrlib.html
29.94
KB
-rw-r--r--
xml.dom.html
89.04
KB
-rw-r--r--
xml.dom.minidom.html
40.42
KB
-rw-r--r--
xml.dom.pulldom.html
12.71
KB
-rw-r--r--
xml.etree.elementtree.html
93.22
KB
-rw-r--r--
xml.html
16.49
KB
-rw-r--r--
xml.sax.handler.html
38.63
KB
-rw-r--r--
xml.sax.html
20.22
KB
-rw-r--r--
xml.sax.reader.html
39.09
KB
-rw-r--r--
xml.sax.utils.html
14.26
KB
-rw-r--r--
xmlrpclib.html
60.79
KB
-rw-r--r--
zipfile.html
53.14
KB
-rw-r--r--
zipimport.html
20.42
KB
-rw-r--r--
zlib.html
25.46
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : datetime.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>8.1. datetime — Basic date and time types — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="8. Data Types" href="datatypes.html" /> <link rel="next" title="8.2. calendar — General calendar-related functions" href="calendar.html" /> <link rel="prev" title="8. Data Types" href="datatypes.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="calendar.html" title="8.2. calendar — General calendar-related functions" accesskey="N">next</a> |</li> <li class="right" > <a href="datatypes.html" title="8. Data Types" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-datetime"> <span id="datetime-basic-date-and-time-types"></span><h1>8.1. <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> — Basic date and time types<a class="headerlink" href="#module-datetime" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> <p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation. For related functionality, see also the <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></a> and <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><tt class="xref py py-mod docutils literal"><span class="pre">calendar</span></tt></a> modules.</p> <p>There are two kinds of date and time objects: “naive” and “aware”.</p> <p>An aware object has sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used to represent a specific moment in time that is not open to interpretation <a class="footnote-reference" href="#id2" id="id1">[1]</a>.</p> <p>A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it’s up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.</p> <p>For applications requiring aware objects, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects have an optional time zone information attribute, <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a>, that can be set to an instance of a subclass of the abstract <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> class. These <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> objects capture information about the offset from UTC time, the time zone name, and whether Daylight Saving Time is in effect. Note that no concrete <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> classes are supplied by the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module. Supporting timezones at whatever level of detail is required is up to the application. The rules for time adjustment across the world are more political than rational, and there is no standard suitable for every application.</p> <p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module exports the following constants:</p> <dl class="data"> <dt id="datetime.MINYEAR"> <tt class="descclassname">datetime.</tt><tt class="descname">MINYEAR</tt><a class="headerlink" href="#datetime.MINYEAR" title="Permalink to this definition">¶</a></dt> <dd><p>The smallest year number allowed in a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object. <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> is <tt class="docutils literal"><span class="pre">1</span></tt>.</p> </dd></dl> <dl class="data"> <dt id="datetime.MAXYEAR"> <tt class="descclassname">datetime.</tt><tt class="descname">MAXYEAR</tt><a class="headerlink" href="#datetime.MAXYEAR" title="Permalink to this definition">¶</a></dt> <dd><p>The largest year number allowed in a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object. <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a> is <tt class="docutils literal"><span class="pre">9999</span></tt>.</p> </dd></dl> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt>Module <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><tt class="xref py py-mod docutils literal"><span class="pre">calendar</span></tt></a></dt> <dd>General calendar related functions.</dd> <dt>Module <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></a></dt> <dd>Time access and conversions.</dd> </dl> </div> <div class="section" id="available-types"> <h2>8.1.1. Available Types<a class="headerlink" href="#available-types" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">date</tt></dt> <dd><p>An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: <tt class="xref py py-attr docutils literal"><span class="pre">year</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">month</span></tt>, and <tt class="xref py py-attr docutils literal"><span class="pre">day</span></tt>.</p> </dd></dl> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">time</tt></dt> <dd><p>An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds (there is no notion of “leap seconds” here). Attributes: <tt class="xref py py-attr docutils literal"><span class="pre">hour</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">minute</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">second</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">microsecond</span></tt>, and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a>.</p> </dd></dl> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">datetime</tt></dt> <dd><p>A combination of a date and a time. Attributes: <tt class="xref py py-attr docutils literal"><span class="pre">year</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">month</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">day</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">hour</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">minute</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">second</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">microsecond</span></tt>, and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a>.</p> </dd></dl> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">timedelta</tt></dt> <dd><p>A duration expressing the difference between two <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a>, <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> instances to microsecond resolution.</p> </dd></dl> <dl class="class"> <dt id="datetime.tzinfo"> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">tzinfo</tt><a class="headerlink" href="#datetime.tzinfo" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class for time zone information objects. These are used by the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> classes to provide a customizable notion of time adjustment (for example, to account for time zone and/or daylight saving time).</p> </dd></dl> <p>Objects of these types are immutable.</p> <p>Objects of the <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> type are always naive.</p> <p>An object of type <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> may be naive or aware. A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object <em>d</em> is aware if <tt class="docutils literal"><span class="pre">d.tzinfo</span></tt> is not <tt class="docutils literal"><span class="pre">None</span></tt> and <tt class="docutils literal"><span class="pre">d.tzinfo.utcoffset(d)</span></tt> does not return <tt class="docutils literal"><span class="pre">None</span></tt>. If <tt class="docutils literal"><span class="pre">d.tzinfo</span></tt> is <tt class="docutils literal"><span class="pre">None</span></tt>, or if <tt class="docutils literal"><span class="pre">d.tzinfo</span></tt> is not <tt class="docutils literal"><span class="pre">None</span></tt> but <tt class="docutils literal"><span class="pre">d.tzinfo.utcoffset(d)</span></tt> returns <tt class="docutils literal"><span class="pre">None</span></tt>, <em>d</em> is naive. A <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object <em>t</em> is aware if <tt class="docutils literal"><span class="pre">t.tzinfo</span></tt> is not <tt class="docutils literal"><span class="pre">None</span></tt> and <tt class="docutils literal"><span class="pre">t.tzinfo.utcoffset(None)</span></tt> does not return <tt class="docutils literal"><span class="pre">None</span></tt>. Otherwise, <em>t</em> is naive.</p> <p>The distinction between naive and aware doesn’t apply to <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects.</p> <p>Subclass relationships:</p> <div class="highlight-python"><pre>object timedelta tzinfo time date datetime</pre> </div> </div> <div class="section" id="timedelta-objects"> <span id="datetime-timedelta"></span><h2>8.1.2. <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> Objects<a class="headerlink" href="#timedelta-objects" title="Permalink to this headline">¶</a></h2> <p>A <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object represents a duration, the difference between two dates or times.</p> <dl class="class"> <dt id="datetime.timedelta"> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">timedelta</tt><big>(</big><span class="optional">[</span><em>days</em><span class="optional">[</span>, <em>seconds</em><span class="optional">[</span>, <em>microseconds</em><span class="optional">[</span>, <em>milliseconds</em><span class="optional">[</span>, <em>minutes</em><span class="optional">[</span>, <em>hours</em><span class="optional">[</span>, <em>weeks</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.timedelta" title="Permalink to this definition">¶</a></dt> <dd><p>All arguments are optional and default to <tt class="docutils literal"><span class="pre">0</span></tt>. Arguments may be ints, longs, or floats, and may be positive or negative.</p> <p>Only <em>days</em>, <em>seconds</em> and <em>microseconds</em> are stored internally. Arguments are converted to those units:</p> <ul class="simple"> <li>A millisecond is converted to 1000 microseconds.</li> <li>A minute is converted to 60 seconds.</li> <li>An hour is converted to 3600 seconds.</li> <li>A week is converted to 7 days.</li> </ul> <p>and days, seconds and microseconds are then normalized so that the representation is unique, with</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microseconds</span> <span class="pre"><</span> <span class="pre">1000000</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">seconds</span> <span class="pre"><</span> <span class="pre">3600*24</span></tt> (the number of seconds in one day)</li> <li><tt class="docutils literal"><span class="pre">-999999999</span> <span class="pre"><=</span> <span class="pre">days</span> <span class="pre"><=</span> <span class="pre">999999999</span></tt></li> </ul> <p>If any argument is a float and there are fractional microseconds, the fractional microseconds left over from all arguments are combined and their sum is rounded to the nearest microsecond. If no argument is a float, the conversion and normalization processes are exact (no information is lost).</p> <p>If the normalized value of days lies outside the indicated range, <a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> is raised.</p> <p>Note that normalization of negative values may be surprising at first. For example,</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">microseconds</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span> <span class="gp">>>> </span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">days</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">seconds</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">microseconds</span><span class="p">)</span> <span class="go">(-1, 86399, 999999)</span> </pre></div> </div> </dd></dl> <p>Class attributes are:</p> <dl class="attribute"> <dt id="datetime.timedelta.min"> <tt class="descclassname">timedelta.</tt><tt class="descname">min</tt><a class="headerlink" href="#datetime.timedelta.min" title="Permalink to this definition">¶</a></dt> <dd><p>The most negative <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object, <tt class="docutils literal"><span class="pre">timedelta(-999999999)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.timedelta.max"> <tt class="descclassname">timedelta.</tt><tt class="descname">max</tt><a class="headerlink" href="#datetime.timedelta.max" title="Permalink to this definition">¶</a></dt> <dd><p>The most positive <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object, <tt class="docutils literal"><span class="pre">timedelta(days=999999999,</span> <span class="pre">hours=23,</span> <span class="pre">minutes=59,</span> <span class="pre">seconds=59,</span> <span class="pre">microseconds=999999)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.timedelta.resolution"> <tt class="descclassname">timedelta.</tt><tt class="descname">resolution</tt><a class="headerlink" href="#datetime.timedelta.resolution" title="Permalink to this definition">¶</a></dt> <dd><p>The smallest possible difference between non-equal <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects, <tt class="docutils literal"><span class="pre">timedelta(microseconds=1)</span></tt>.</p> </dd></dl> <p>Note that, because of normalization, <tt class="docutils literal"><span class="pre">timedelta.max</span></tt> > <tt class="docutils literal"><span class="pre">-timedelta.min</span></tt>. <tt class="docutils literal"><span class="pre">-timedelta.max</span></tt> is not representable as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object.</p> <p>Instance attributes (read-only):</p> <table border="1" class="docutils"> <colgroup> <col width="29%" /> <col width="71%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Attribute</th> <th class="head">Value</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">days</span></tt></td> <td>Between -999999999 and 999999999 inclusive</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">seconds</span></tt></td> <td>Between 0 and 86399 inclusive</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">microseconds</span></tt></td> <td>Between 0 and 999999 inclusive</td> </tr> </tbody> </table> <p>Supported operations:</p> <table border="1" class="docutils"> <colgroup> <col width="41%" /> <col width="59%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Operation</th> <th class="head">Result</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">+</span> <span class="pre">t3</span></tt></td> <td>Sum of <em>t2</em> and <em>t3</em>. Afterwards <em>t1</em>-<em>t2</em> == <em>t3</em> and <em>t1</em>-<em>t3</em> == <em>t2</em> are true. (1)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">-</span> <span class="pre">t3</span></tt></td> <td>Difference of <em>t2</em> and <em>t3</em>. Afterwards <em>t1</em> == <em>t2</em> - <em>t3</em> and <em>t2</em> == <em>t1</em> + <em>t3</em> are true. (1)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">*</span> <span class="pre">i</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">i</span> <span class="pre">*</span> <span class="pre">t2</span></tt></td> <td>Delta multiplied by an integer or long. Afterwards <em>t1</em> // i == <em>t2</em> is true, provided <tt class="docutils literal"><span class="pre">i</span> <span class="pre">!=</span> <span class="pre">0</span></tt>.</td> </tr> <tr class="row-odd"><td> </td> <td>In general, <em>t1</em> * i == <em>t1</em> * (i-1) + <em>t1</em> is true. (1)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">//</span> <span class="pre">i</span></tt></td> <td>The floor is computed and the remainder (if any) is thrown away. (3)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">+t1</span></tt></td> <td>Returns a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object with the same value. (2)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">-t1</span></tt></td> <td>equivalent to <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a>(-<em>t1.days</em>, -<em>t1.seconds</em>, -<em>t1.microseconds</em>), and to <em>t1</em>* -1. (1)(4)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">abs(t)</span></tt></td> <td>equivalent to +<em>t</em> when <tt class="docutils literal"><span class="pre">t.days</span> <span class="pre">>=</span> <span class="pre">0</span></tt>, and to -<em>t</em> when <tt class="docutils literal"><span class="pre">t.days</span> <span class="pre"><</span> <span class="pre">0</span></tt>. (2)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">str(t)</span></tt></td> <td>Returns a string in the form <tt class="docutils literal"><span class="pre">[D</span> <span class="pre">day[s],</span> <span class="pre">][H]H:MM:SS[.UUUUUU]</span></tt>, where D is negative for negative <tt class="docutils literal"><span class="pre">t</span></tt>. (5)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">repr(t)</span></tt></td> <td>Returns a string in the form <tt class="docutils literal"><span class="pre">datetime.timedelta(D[,</span> <span class="pre">S[,</span> <span class="pre">U]])</span></tt>, where D is negative for negative <tt class="docutils literal"><span class="pre">t</span></tt>. (5)</td> </tr> </tbody> </table> <p>Notes:</p> <ol class="arabic"> <li><p class="first">This is exact, but may overflow.</p> </li> <li><p class="first">This is exact, and cannot overflow.</p> </li> <li><p class="first">Division by 0 raises <a class="reference internal" href="exceptions.html#exceptions.ZeroDivisionError" title="exceptions.ZeroDivisionError"><tt class="xref py py-exc docutils literal"><span class="pre">ZeroDivisionError</span></tt></a>.</p> </li> <li><p class="first">-<em>timedelta.max</em> is not representable as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object.</p> </li> <li><p class="first">String representations of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects are normalized similarly to their internal representation. This leads to somewhat unusual results for negative timedeltas. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=-</span><span class="mi">5</span><span class="p">)</span> <span class="go">datetime.timedelta(-1, 68400)</span> <span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">_</span><span class="p">)</span> <span class="go">-1 day, 19:00:00</span> </pre></div> </div> </li> </ol> <p>In addition to the operations listed above <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects support certain additions and subtractions with <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> and <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects (see below).</p> <p>Comparisons of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects are supported with the <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing the smaller duration considered to be the smaller timedelta. In order to stop mixed-type comparisons from falling back to the default comparison by object address, when a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised unless the comparison is <tt class="docutils literal"><span class="pre">==</span></tt> or <tt class="docutils literal"><span class="pre">!=</span></tt>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> or <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, respectively.</p> <p><a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects are <a class="reference internal" href="../glossary.html#term-hashable"><em class="xref std std-term">hashable</em></a> (usable as dictionary keys), support efficient pickling, and in Boolean contexts, a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object is considered to be true if and only if it isn’t equal to <tt class="docutils literal"><span class="pre">timedelta(0)</span></tt>.</p> <p>Instance methods:</p> <dl class="method"> <dt id="datetime.timedelta.total_seconds"> <tt class="descclassname">timedelta.</tt><tt class="descname">total_seconds</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.timedelta.total_seconds" title="Permalink to this definition">¶</a></dt> <dd><p>Return the total number of seconds contained in the duration. Equivalent to <tt class="docutils literal"><span class="pre">(td.microseconds</span> <span class="pre">+</span> <span class="pre">(td.seconds</span> <span class="pre">+</span> <span class="pre">td.days</span> <span class="pre">*</span> <span class="pre">24</span> <span class="pre">*</span> <span class="pre">3600)</span> <span class="pre">*</span> <span class="pre">10**6)</span> <span class="pre">/</span> <span class="pre">10**6</span></tt> computed with true division enabled.</p> <p>Note that for very large time intervals (greater than 270 years on most platforms) this method will lose microsecond accuracy.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> </dd></dl> <p>Example usage:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span> <span class="gp">>>> </span><span class="n">year</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">365</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">another_year</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">days</span><span class="o">=</span><span class="mi">84</span><span class="p">,</span> <span class="n">hours</span><span class="o">=</span><span class="mi">23</span><span class="p">,</span> <span class="gp">... </span> <span class="n">minutes</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">seconds</span><span class="o">=</span><span class="mi">600</span><span class="p">)</span> <span class="c"># adds up to 365 days</span> <span class="gp">>>> </span><span class="n">year</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span> <span class="go">31536000.0</span> <span class="gp">>>> </span><span class="n">year</span> <span class="o">==</span> <span class="n">another_year</span> <span class="go">True</span> <span class="gp">>>> </span><span class="n">ten_years</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">year</span> <span class="gp">>>> </span><span class="n">ten_years</span><span class="p">,</span> <span class="n">ten_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span> <span class="go">(datetime.timedelta(3650), 10)</span> <span class="gp">>>> </span><span class="n">nine_years</span> <span class="o">=</span> <span class="n">ten_years</span> <span class="o">-</span> <span class="n">year</span> <span class="gp">>>> </span><span class="n">nine_years</span><span class="p">,</span> <span class="n">nine_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span> <span class="go">(datetime.timedelta(3285), 9)</span> <span class="gp">>>> </span><span class="n">three_years</span> <span class="o">=</span> <span class="n">nine_years</span> <span class="o">//</span> <span class="mi">3</span><span class="p">;</span> <span class="gp">>>> </span><span class="n">three_years</span><span class="p">,</span> <span class="n">three_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span> <span class="go">(datetime.timedelta(1095), 3)</span> <span class="gp">>>> </span><span class="nb">abs</span><span class="p">(</span><span class="n">three_years</span> <span class="o">-</span> <span class="n">ten_years</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">three_years</span> <span class="o">+</span> <span class="n">year</span> <span class="go">True</span> </pre></div> </div> </div> <div class="section" id="date-objects"> <span id="datetime-date"></span><h2>8.1.3. <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> Objects<a class="headerlink" href="#date-objects" title="Permalink to this headline">¶</a></h2> <p>A <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object represents a date (year, month and day) in an idealized calendar, the current Gregorian calendar indefinitely extended in both directions. January 1 of year 1 is called day number 1, January 2 of year 1 is called day number 2, and so on. This matches the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold’s book Calendrical Calculations, where it’s the base calendar for all computations. See the book for algorithms for converting between proleptic Gregorian ordinals and many other calendar systems.</p> <dl class="class"> <dt id="datetime.date"> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">date</tt><big>(</big><em>year</em>, <em>month</em>, <em>day</em><big>)</big><a class="headerlink" href="#datetime.date" title="Permalink to this definition">¶</a></dt> <dd><p>All arguments are required. Arguments may be ints or longs, in the following ranges:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">MINYEAR</span> <span class="pre"><=</span> <span class="pre">year</span> <span class="pre"><=</span> <span class="pre">MAXYEAR</span></tt></li> <li><tt class="docutils literal"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">month</span> <span class="pre"><=</span> <span class="pre">12</span></tt></li> <li><tt class="docutils literal"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">day</span> <span class="pre"><=</span> <span class="pre">number</span> <span class="pre">of</span> <span class="pre">days</span> <span class="pre">in</span> <span class="pre">the</span> <span class="pre">given</span> <span class="pre">month</span> <span class="pre">and</span> <span class="pre">year</span></tt></li> </ul> <p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</p> </dd></dl> <p>Other constructors, all class methods:</p> <dl class="classmethod"> <dt id="datetime.date.today"> <em class="property">classmethod </em><tt class="descclassname">date.</tt><tt class="descname">today</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.today" title="Permalink to this definition">¶</a></dt> <dd><p>Return the current local date. This is equivalent to <tt class="docutils literal"><span class="pre">date.fromtimestamp(time.time())</span></tt>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.date.fromtimestamp"> <em class="property">classmethod </em><tt class="descclassname">date.</tt><tt class="descname">fromtimestamp</tt><big>(</big><em>timestamp</em><big>)</big><a class="headerlink" href="#datetime.date.fromtimestamp" title="Permalink to this definition">¶</a></dt> <dd><p>Return the local date corresponding to the POSIX timestamp, such as is returned by <a class="reference internal" href="time.html#time.time" title="time.time"><tt class="xref py py-func docutils literal"><span class="pre">time.time()</span></tt></a>. This may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>, if the timestamp is out of the range of values supported by the platform C <tt class="xref c c-func docutils literal"><span class="pre">localtime()</span></tt> function. It’s common for this to be restricted to years from 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by <a class="reference internal" href="#datetime.date.fromtimestamp" title="datetime.date.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.date.fromordinal"> <em class="property">classmethod </em><tt class="descclassname">date.</tt><tt class="descname">fromordinal</tt><big>(</big><em>ordinal</em><big>)</big><a class="headerlink" href="#datetime.date.fromordinal" title="Permalink to this definition">¶</a></dt> <dd><p>Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised unless <tt class="docutils literal"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">ordinal</span> <span class="pre"><=</span> <span class="pre">date.max.toordinal()</span></tt>. For any date <em>d</em>, <tt class="docutils literal"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span> <span class="pre">d</span></tt>.</p> </dd></dl> <p>Class attributes:</p> <dl class="attribute"> <dt id="datetime.date.min"> <tt class="descclassname">date.</tt><tt class="descname">min</tt><a class="headerlink" href="#datetime.date.min" title="Permalink to this definition">¶</a></dt> <dd><p>The earliest representable date, <tt class="docutils literal"><span class="pre">date(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.date.max"> <tt class="descclassname">date.</tt><tt class="descname">max</tt><a class="headerlink" href="#datetime.date.max" title="Permalink to this definition">¶</a></dt> <dd><p>The latest representable date, <tt class="docutils literal"><span class="pre">date(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.date.resolution"> <tt class="descclassname">date.</tt><tt class="descname">resolution</tt><a class="headerlink" href="#datetime.date.resolution" title="Permalink to this definition">¶</a></dt> <dd><p>The smallest possible difference between non-equal date objects, <tt class="docutils literal"><span class="pre">timedelta(days=1)</span></tt>.</p> </dd></dl> <p>Instance attributes (read-only):</p> <dl class="attribute"> <dt id="datetime.date.year"> <tt class="descclassname">date.</tt><tt class="descname">year</tt><a class="headerlink" href="#datetime.date.year" title="Permalink to this definition">¶</a></dt> <dd><p>Between <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a> inclusive.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.date.month"> <tt class="descclassname">date.</tt><tt class="descname">month</tt><a class="headerlink" href="#datetime.date.month" title="Permalink to this definition">¶</a></dt> <dd><p>Between 1 and 12 inclusive.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.date.day"> <tt class="descclassname">date.</tt><tt class="descname">day</tt><a class="headerlink" href="#datetime.date.day" title="Permalink to this definition">¶</a></dt> <dd><p>Between 1 and the number of days in the given month of the given year.</p> </dd></dl> <p>Supported operations:</p> <table border="1" class="docutils"> <colgroup> <col width="40%" /> <col width="60%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Operation</th> <th class="head">Result</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">+</span> <span class="pre">timedelta</span></tt></td> <td><em>date2</em> is <tt class="docutils literal"><span class="pre">timedelta.days</span></tt> days removed from <em>date1</em>. (1)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">timedelta</span></tt></td> <td>Computes <em>date2</em> such that <tt class="docutils literal"><span class="pre">date2</span> <span class="pre">+</span> <span class="pre">timedelta</span> <span class="pre">==</span> <span class="pre">date1</span></tt>. (2)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">date2</span></tt></td> <td>(3)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></tt></td> <td><em>date1</em> is considered less than <em>date2</em> when <em>date1</em> precedes <em>date2</em> in time. (4)</td> </tr> </tbody> </table> <p>Notes:</p> <ol class="arabic simple"> <li><em>date2</em> is moved forward in time if <tt class="docutils literal"><span class="pre">timedelta.days</span> <span class="pre">></span> <span class="pre">0</span></tt>, or backward if <tt class="docutils literal"><span class="pre">timedelta.days</span> <span class="pre"><</span> <span class="pre">0</span></tt>. Afterward <tt class="docutils literal"><span class="pre">date2</span> <span class="pre">-</span> <span class="pre">date1</span> <span class="pre">==</span> <span class="pre">timedelta.days</span></tt>. <tt class="docutils literal"><span class="pre">timedelta.seconds</span></tt> and <tt class="docutils literal"><span class="pre">timedelta.microseconds</span></tt> are ignored. <a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> is raised if <tt class="docutils literal"><span class="pre">date2.year</span></tt> would be smaller than <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> or larger than <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a>.</li> <li>This isn’t quite equivalent to date1 + (-timedelta), because -timedelta in isolation can overflow in cases where date1 - timedelta does not. <tt class="docutils literal"><span class="pre">timedelta.seconds</span></tt> and <tt class="docutils literal"><span class="pre">timedelta.microseconds</span></tt> are ignored.</li> <li>This is exact, and cannot overflow. timedelta.seconds and timedelta.microseconds are 0, and date2 + timedelta == date1 after.</li> <li>In other words, <tt class="docutils literal"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></tt> if and only if <tt class="docutils literal"><span class="pre">date1.toordinal()</span> <span class="pre"><</span> <span class="pre">date2.toordinal()</span></tt>. In order to stop comparison from falling back to the default scheme of comparing object addresses, date comparison normally raises <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> if the other comparand isn’t also a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object. However, <tt class="docutils literal"><span class="pre">NotImplemented</span></tt> is returned instead if the other comparand has a <tt class="xref py py-meth docutils literal"><span class="pre">timetuple()</span></tt> attribute. This hook gives other kinds of date objects a chance at implementing mixed-type comparison. If not, when a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised unless the comparison is <tt class="docutils literal"><span class="pre">==</span></tt> or <tt class="docutils literal"><span class="pre">!=</span></tt>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> or <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, respectively.</li> </ol> <p>Dates can be used as dictionary keys. In Boolean contexts, all <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> objects are considered to be true.</p> <p>Instance methods:</p> <dl class="method"> <dt id="datetime.date.replace"> <tt class="descclassname">date.</tt><tt class="descname">replace</tt><big>(</big><em>year</em>, <em>month</em>, <em>day</em><big>)</big><a class="headerlink" href="#datetime.date.replace" title="Permalink to this definition">¶</a></dt> <dd><p>Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified. For example, if <tt class="docutils literal"><span class="pre">d</span> <span class="pre">==</span> <span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">31)</span></tt>, then <tt class="docutils literal"><span class="pre">d.replace(day=26)</span> <span class="pre">==</span> <span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">26)</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.timetuple"> <tt class="descclassname">date.</tt><tt class="descname">timetuple</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.timetuple" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><tt class="xref py py-class docutils literal"><span class="pre">time.struct_time</span></tt></a> such as returned by <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><tt class="xref py py-func docutils literal"><span class="pre">time.localtime()</span></tt></a>. The hours, minutes and seconds are 0, and the DST flag is -1. <tt class="docutils literal"><span class="pre">d.timetuple()</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">time.struct_time((d.year,</span> <span class="pre">d.month,</span> <span class="pre">d.day,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">d.weekday(),</span> <span class="pre">yday,</span> <span class="pre">-1))</span></tt>, where <tt class="docutils literal"><span class="pre">yday</span> <span class="pre">=</span> <span class="pre">d.toordinal()</span> <span class="pre">-</span> <span class="pre">date(d.year,</span> <span class="pre">1,</span> <span class="pre">1).toordinal()</span> <span class="pre">+</span> <span class="pre">1</span></tt> is the day number within the current year starting with <tt class="docutils literal"><span class="pre">1</span></tt> for January 1st.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.toordinal"> <tt class="descclassname">date.</tt><tt class="descname">toordinal</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.toordinal" title="Permalink to this definition">¶</a></dt> <dd><p>Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. For any <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object <em>d</em>, <tt class="docutils literal"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span> <span class="pre">d</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.weekday"> <tt class="descclassname">date.</tt><tt class="descname">weekday</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.weekday" title="Permalink to this definition">¶</a></dt> <dd><p>Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, <tt class="docutils literal"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).weekday()</span> <span class="pre">==</span> <span class="pre">2</span></tt>, a Wednesday. See also <a class="reference internal" href="#datetime.date.isoweekday" title="datetime.date.isoweekday"><tt class="xref py py-meth docutils literal"><span class="pre">isoweekday()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.isoweekday"> <tt class="descclassname">date.</tt><tt class="descname">isoweekday</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.isoweekday" title="Permalink to this definition">¶</a></dt> <dd><p>Return the day of the week as an integer, where Monday is 1 and Sunday is 7. For example, <tt class="docutils literal"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).isoweekday()</span> <span class="pre">==</span> <span class="pre">3</span></tt>, a Wednesday. See also <a class="reference internal" href="#datetime.date.weekday" title="datetime.date.weekday"><tt class="xref py py-meth docutils literal"><span class="pre">weekday()</span></tt></a>, <a class="reference internal" href="#datetime.date.isocalendar" title="datetime.date.isocalendar"><tt class="xref py py-meth docutils literal"><span class="pre">isocalendar()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.isocalendar"> <tt class="descclassname">date.</tt><tt class="descname">isocalendar</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.isocalendar" title="Permalink to this definition">¶</a></dt> <dd><p>Return a 3-tuple, (ISO year, ISO week number, ISO weekday).</p> <p>The ISO calendar is a widely used variant of the Gregorian calendar. See <a class="reference external" href="http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm">http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm</a> for a good explanation.</p> <p>The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.</p> <p>For example, 2004 begins on a Thursday, so the first week of ISO year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004, so that <tt class="docutils literal"><span class="pre">date(2003,</span> <span class="pre">12,</span> <span class="pre">29).isocalendar()</span> <span class="pre">==</span> <span class="pre">(2004,</span> <span class="pre">1,</span> <span class="pre">1)</span></tt> and <tt class="docutils literal"><span class="pre">date(2004,</span> <span class="pre">1,</span> <span class="pre">4).isocalendar()</span> <span class="pre">==</span> <span class="pre">(2004,</span> <span class="pre">1,</span> <span class="pre">7)</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.isoformat"> <tt class="descclassname">date.</tt><tt class="descname">isoformat</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.isoformat" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. For example, <tt class="docutils literal"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).isoformat()</span> <span class="pre">==</span> <span class="pre">'2002-12-04'</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.__str__"> <tt class="descclassname">date.</tt><tt class="descname">__str__</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.__str__" title="Permalink to this definition">¶</a></dt> <dd><p>For a date <em>d</em>, <tt class="docutils literal"><span class="pre">str(d)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">d.isoformat()</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.ctime"> <tt class="descclassname">date.</tt><tt class="descname">ctime</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.date.ctime" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the date, for example <tt class="docutils literal"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).ctime()</span> <span class="pre">==</span> <span class="pre">'Wed</span> <span class="pre">Dec</span> <span class="pre">4</span> <span class="pre">00:00:00</span> <span class="pre">2002'</span></tt>. <tt class="docutils literal"><span class="pre">d.ctime()</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></tt> on platforms where the native C <tt class="xref c c-func docutils literal"><span class="pre">ctime()</span></tt> function (which <a class="reference internal" href="time.html#time.ctime" title="time.ctime"><tt class="xref py py-func docutils literal"><span class="pre">time.ctime()</span></tt></a> invokes, but which <a class="reference internal" href="#datetime.date.ctime" title="datetime.date.ctime"><tt class="xref py py-meth docutils literal"><span class="pre">date.ctime()</span></tt></a> does not invoke) conforms to the C standard.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.strftime"> <tt class="descclassname">date.</tt><tt class="descname">strftime</tt><big>(</big><em>format</em><big>)</big><a class="headerlink" href="#datetime.date.strftime" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.date.__format__"> <tt class="descclassname">date.</tt><tt class="descname">__format__</tt><big>(</big><em>format</em><big>)</big><a class="headerlink" href="#datetime.date.__format__" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#datetime.date.strftime" title="datetime.date.strftime"><tt class="xref py py-meth docutils literal"><span class="pre">date.strftime()</span></tt></a>. This makes it possible to specify format string for a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a>. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <p>Example of counting days to an event:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">time</span> <span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span> <span class="gp">>>> </span><span class="n">today</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">today</span> <span class="go">datetime.date(2007, 12, 5)</span> <span class="gp">>>> </span><span class="n">today</span> <span class="o">==</span> <span class="n">date</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="go">True</span> <span class="gp">>>> </span><span class="n">my_birthday</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="n">today</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">24</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">if</span> <span class="n">my_birthday</span> <span class="o"><</span> <span class="n">today</span><span class="p">:</span> <span class="gp">... </span> <span class="n">my_birthday</span> <span class="o">=</span> <span class="n">my_birthday</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">today</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">my_birthday</span> <span class="go">datetime.date(2008, 6, 24)</span> <span class="gp">>>> </span><span class="n">time_to_birthday</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">my_birthday</span> <span class="o">-</span> <span class="n">today</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">time_to_birthday</span><span class="o">.</span><span class="n">days</span> <span class="go">202</span> </pre></div> </div> <p>Example of working with <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">fromordinal</span><span class="p">(</span><span class="mi">730920</span><span class="p">)</span> <span class="c"># 730920th day after 1. 1. 0001</span> <span class="gp">>>> </span><span class="n">d</span> <span class="go">datetime.date(2002, 3, 11)</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">t</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">i</span> <span class="go">2002 # year</span> <span class="go">3 # month</span> <span class="go">11 # day</span> <span class="go">0</span> <span class="go">0</span> <span class="go">0</span> <span class="go">0 # weekday (0 = Monday)</span> <span class="go">70 # 70th day in the year</span> <span class="go">-1</span> <span class="gp">>>> </span><span class="n">ic</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">i</span> <span class="go">2002 # ISO year</span> <span class="go">11 # ISO week number</span> <span class="go">1 # ISO day number ( 1 = Monday )</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span> <span class="go">'2002-03-11'</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">"</span><span class="si">%d</span><span class="s">/%m/%y"</span><span class="p">)</span> <span class="go">'11/03/02'</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">"%A </span><span class="si">%d</span><span class="s">. %B %Y"</span><span class="p">)</span> <span class="go">'Monday 11. March 2002'</span> <span class="gp">>>> </span><span class="s">'The {1} is {0:</span><span class="si">%d</span><span class="s">}, the {2} is {0:%B}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="s">"day"</span><span class="p">,</span> <span class="s">"month"</span><span class="p">)</span> <span class="go">'The day is 11, the month is March.'</span> </pre></div> </div> </div> <div class="section" id="datetime-objects"> <span id="datetime-datetime"></span><h2>8.1.4. <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> Objects<a class="headerlink" href="#datetime-objects" title="Permalink to this headline">¶</a></h2> <p>A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object is a single object containing all the information from a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object and a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object. Like a <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> assumes the current Gregorian calendar extended in both directions; like a time object, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> assumes there are exactly 3600*24 seconds in every day.</p> <p>Constructor:</p> <dl class="class"> <dt id="datetime.datetime"> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">datetime</tt><big>(</big><em>year</em>, <em>month</em>, <em>day</em><span class="optional">[</span>, <em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.datetime" title="Permalink to this definition">¶</a></dt> <dd><p>The year, month and day arguments are required. <em>tzinfo</em> may be <tt class="docutils literal"><span class="pre">None</span></tt>, or an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass. The remaining arguments may be ints or longs, in the following ranges:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">MINYEAR</span> <span class="pre"><=</span> <span class="pre">year</span> <span class="pre"><=</span> <span class="pre">MAXYEAR</span></tt></li> <li><tt class="docutils literal"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">month</span> <span class="pre"><=</span> <span class="pre">12</span></tt></li> <li><tt class="docutils literal"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">day</span> <span class="pre"><=</span> <span class="pre">number</span> <span class="pre">of</span> <span class="pre">days</span> <span class="pre">in</span> <span class="pre">the</span> <span class="pre">given</span> <span class="pre">month</span> <span class="pre">and</span> <span class="pre">year</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">hour</span> <span class="pre"><</span> <span class="pre">24</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">minute</span> <span class="pre"><</span> <span class="pre">60</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">second</span> <span class="pre"><</span> <span class="pre">60</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microsecond</span> <span class="pre"><</span> <span class="pre">1000000</span></tt></li> </ul> <p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</p> </dd></dl> <p>Other constructors, all class methods:</p> <dl class="classmethod"> <dt id="datetime.datetime.today"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">today</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.today" title="Permalink to this definition">¶</a></dt> <dd><p>Return the current local datetime, with <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> <tt class="docutils literal"><span class="pre">None</span></tt>. This is equivalent to <tt class="docutils literal"><span class="pre">datetime.fromtimestamp(time.time())</span></tt>. See also <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><tt class="xref py py-meth docutils literal"><span class="pre">now()</span></tt></a>, <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.now"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">now</tt><big>(</big><span class="optional">[</span><em>tz</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.datetime.now" title="Permalink to this definition">¶</a></dt> <dd><p>Return the current local date and time. If optional argument <em>tz</em> is <tt class="docutils literal"><span class="pre">None</span></tt> or not specified, this is like <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><tt class="xref py py-meth docutils literal"><span class="pre">today()</span></tt></a>, but, if possible, supplies more precision than can be gotten from going through a <a class="reference internal" href="time.html#time.time" title="time.time"><tt class="xref py py-func docutils literal"><span class="pre">time.time()</span></tt></a> timestamp (for example, this may be possible on platforms supplying the C <tt class="xref c c-func docutils literal"><span class="pre">gettimeofday()</span></tt> function).</p> <p>Else <em>tz</em> must be an instance of a class <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass, and the current date and time are converted to <em>tz</em>‘s time zone. In this case the result is equivalent to <tt class="docutils literal"><span class="pre">tz.fromutc(datetime.utcnow().replace(tzinfo=tz))</span></tt>. See also <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><tt class="xref py py-meth docutils literal"><span class="pre">today()</span></tt></a>, <a class="reference internal" href="#datetime.datetime.utcnow" title="datetime.datetime.utcnow"><tt class="xref py py-meth docutils literal"><span class="pre">utcnow()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.utcnow"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">utcnow</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.utcnow" title="Permalink to this definition">¶</a></dt> <dd><p>Return the current UTC date and time, with <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> <tt class="docutils literal"><span class="pre">None</span></tt>. This is like <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><tt class="xref py py-meth docutils literal"><span class="pre">now()</span></tt></a>, but returns the current UTC date and time, as a naive <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object. See also <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><tt class="xref py py-meth docutils literal"><span class="pre">now()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.fromtimestamp"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">fromtimestamp</tt><big>(</big><em>timestamp</em><span class="optional">[</span>, <em>tz</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.datetime.fromtimestamp" title="Permalink to this definition">¶</a></dt> <dd><p>Return the local date and time corresponding to the POSIX timestamp, such as is returned by <a class="reference internal" href="time.html#time.time" title="time.time"><tt class="xref py py-func docutils literal"><span class="pre">time.time()</span></tt></a>. If optional argument <em>tz</em> is <tt class="docutils literal"><span class="pre">None</span></tt> or not specified, the timestamp is converted to the platform’s local date and time, and the returned <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object is naive.</p> <p>Else <em>tz</em> must be an instance of a class <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass, and the timestamp is converted to <em>tz</em>‘s time zone. In this case the result is equivalent to <tt class="docutils literal"><span class="pre">tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))</span></tt>.</p> <p><a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a> may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>, if the timestamp is out of the range of values supported by the platform C <tt class="xref c c-func docutils literal"><span class="pre">localtime()</span></tt> or <tt class="xref c c-func docutils literal"><span class="pre">gmtime()</span></tt> functions. It’s common for this to be restricted to years in 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a>, and then it’s possible to have two timestamps differing by a second that yield identical <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects. See also <a class="reference internal" href="#datetime.datetime.utcfromtimestamp" title="datetime.datetime.utcfromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">utcfromtimestamp()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.utcfromtimestamp"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">utcfromtimestamp</tt><big>(</big><em>timestamp</em><big>)</big><a class="headerlink" href="#datetime.datetime.utcfromtimestamp" title="Permalink to this definition">¶</a></dt> <dd><p>Return the UTC <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> corresponding to the POSIX timestamp, with <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> <tt class="docutils literal"><span class="pre">None</span></tt>. This may raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>, if the timestamp is out of the range of values supported by the platform C <tt class="xref c c-func docutils literal"><span class="pre">gmtime()</span></tt> function. It’s common for this to be restricted to years in 1970 through 2038. See also <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><tt class="xref py py-meth docutils literal"><span class="pre">fromtimestamp()</span></tt></a>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.fromordinal"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">fromordinal</tt><big>(</big><em>ordinal</em><big>)</big><a class="headerlink" href="#datetime.datetime.fromordinal" title="Permalink to this definition">¶</a></dt> <dd><p>Return the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised unless <tt class="docutils literal"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">ordinal</span> <span class="pre"><=</span> <span class="pre">datetime.max.toordinal()</span></tt>. The hour, minute, second and microsecond of the result are all 0, and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.combine"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">combine</tt><big>(</big><em>date</em>, <em>time</em><big>)</big><a class="headerlink" href="#datetime.datetime.combine" title="Permalink to this definition">¶</a></dt> <dd><p>Return a new <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object whose date components are equal to the given <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object’s, and whose time components and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes are equal to the given <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object’s. For any <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object <em>d</em>, <tt class="docutils literal"><span class="pre">d</span> <span class="pre">==</span> <span class="pre">datetime.combine(d.date(),</span> <span class="pre">d.timetz())</span></tt>. If date is a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object, its time components and <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes are ignored.</p> </dd></dl> <dl class="classmethod"> <dt id="datetime.datetime.strptime"> <em class="property">classmethod </em><tt class="descclassname">datetime.</tt><tt class="descname">strptime</tt><big>(</big><em>date_string</em>, <em>format</em><big>)</big><a class="headerlink" href="#datetime.datetime.strptime" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> corresponding to <em>date_string</em>, parsed according to <em>format</em>. This is equivalent to <tt class="docutils literal"><span class="pre">datetime(*(time.strptime(date_string,</span> <span class="pre">format)[0:6]))</span></tt>. <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised if the date_string and format can’t be parsed by <a class="reference internal" href="time.html#time.strptime" title="time.strptime"><tt class="xref py py-func docutils literal"><span class="pre">time.strptime()</span></tt></a> or if it returns a value which isn’t a time tuple. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> </dd></dl> <p>Class attributes:</p> <dl class="attribute"> <dt id="datetime.datetime.min"> <tt class="descclassname">datetime.</tt><tt class="descname">min</tt><a class="headerlink" href="#datetime.datetime.min" title="Permalink to this definition">¶</a></dt> <dd><p>The earliest representable <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>, <tt class="docutils literal"><span class="pre">datetime(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">tzinfo=None)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.max"> <tt class="descclassname">datetime.</tt><tt class="descname">max</tt><a class="headerlink" href="#datetime.datetime.max" title="Permalink to this definition">¶</a></dt> <dd><p>The latest representable <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>, <tt class="docutils literal"><span class="pre">datetime(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31,</span> <span class="pre">23,</span> <span class="pre">59,</span> <span class="pre">59,</span> <span class="pre">999999,</span> <span class="pre">tzinfo=None)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.resolution"> <tt class="descclassname">datetime.</tt><tt class="descname">resolution</tt><a class="headerlink" href="#datetime.datetime.resolution" title="Permalink to this definition">¶</a></dt> <dd><p>The smallest possible difference between non-equal <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects, <tt class="docutils literal"><span class="pre">timedelta(microseconds=1)</span></tt>.</p> </dd></dl> <p>Instance attributes (read-only):</p> <dl class="attribute"> <dt id="datetime.datetime.year"> <tt class="descclassname">datetime.</tt><tt class="descname">year</tt><a class="headerlink" href="#datetime.datetime.year" title="Permalink to this definition">¶</a></dt> <dd><p>Between <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a> inclusive.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.month"> <tt class="descclassname">datetime.</tt><tt class="descname">month</tt><a class="headerlink" href="#datetime.datetime.month" title="Permalink to this definition">¶</a></dt> <dd><p>Between 1 and 12 inclusive.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.day"> <tt class="descclassname">datetime.</tt><tt class="descname">day</tt><a class="headerlink" href="#datetime.datetime.day" title="Permalink to this definition">¶</a></dt> <dd><p>Between 1 and the number of days in the given month of the given year.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.hour"> <tt class="descclassname">datetime.</tt><tt class="descname">hour</tt><a class="headerlink" href="#datetime.datetime.hour" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(24)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.minute"> <tt class="descclassname">datetime.</tt><tt class="descname">minute</tt><a class="headerlink" href="#datetime.datetime.minute" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(60)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.second"> <tt class="descclassname">datetime.</tt><tt class="descname">second</tt><a class="headerlink" href="#datetime.datetime.second" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(60)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.microsecond"> <tt class="descclassname">datetime.</tt><tt class="descname">microsecond</tt><a class="headerlink" href="#datetime.datetime.microsecond" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(1000000)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.datetime.tzinfo"> <tt class="descclassname">datetime.</tt><tt class="descname">tzinfo</tt><a class="headerlink" href="#datetime.datetime.tzinfo" title="Permalink to this definition">¶</a></dt> <dd><p>The object passed as the <em>tzinfo</em> argument to the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> constructor, or <tt class="docutils literal"><span class="pre">None</span></tt> if none was passed.</p> </dd></dl> <p>Supported operations:</p> <table border="1" class="docutils"> <colgroup> <col width="55%" /> <col width="45%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Operation</th> <th class="head">Result</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">datetime2</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">+</span> <span class="pre">timedelta</span></tt></td> <td>(1)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">datetime2</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">-</span> <span class="pre">timedelta</span></tt></td> <td>(2)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">datetime1</span> <span class="pre">-</span> <span class="pre">datetime2</span></tt></td> <td>(3)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">datetime1</span> <span class="pre"><</span> <span class="pre">datetime2</span></tt></td> <td>Compares <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> to <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>. (4)</td> </tr> </tbody> </table> <ol class="arabic"> <li><p class="first">datetime2 is a duration of timedelta removed from datetime1, moving forward in time if <tt class="docutils literal"><span class="pre">timedelta.days</span></tt> > 0, or backward if <tt class="docutils literal"><span class="pre">timedelta.days</span></tt> < 0. The result has the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute as the input datetime, and datetime2 - datetime1 == timedelta after. <a class="reference internal" href="exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> is raised if datetime2.year would be smaller than <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a> or larger than <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a>. Note that no time zone adjustments are done even if the input is an aware object.</p> </li> <li><p class="first">Computes the datetime2 such that datetime2 + timedelta == datetime1. As for addition, the result has the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute as the input datetime, and no time zone adjustments are done even if the input is aware. This isn’t quite equivalent to datetime1 + (-timedelta), because -timedelta in isolation can overflow in cases where datetime1 - timedelta does not.</p> </li> <li><p class="first">Subtraction of a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> from a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> is defined only if both operands are naive, or if both are aware. If one is aware and the other is naive, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised.</p> <p>If both are naive, or both are aware and have the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute, the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes are ignored, and the result is a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object <em>t</em> such that <tt class="docutils literal"><span class="pre">datetime2</span> <span class="pre">+</span> <span class="pre">t</span> <span class="pre">==</span> <span class="pre">datetime1</span></tt>. No time zone adjustments are done in this case.</p> <p>If both are aware and have different <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes, <tt class="docutils literal"><span class="pre">a-b</span></tt> acts as if <em>a</em> and <em>b</em> were first converted to naive UTC datetimes first. The result is <tt class="docutils literal"><span class="pre">(a.replace(tzinfo=None)</span> <span class="pre">-</span> <span class="pre">a.utcoffset())</span> <span class="pre">-</span> <span class="pre">(b.replace(tzinfo=None)</span> <span class="pre">-</span> <span class="pre">b.utcoffset())</span></tt> except that the implementation never overflows.</p> </li> <li><p class="first"><em>datetime1</em> is considered less than <em>datetime2</em> when <em>datetime1</em> precedes <em>datetime2</em> in time.</p> <p>If one comparand is naive and the other is aware, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised. If both comparands are aware, and have the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute, the common <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute is ignored and the base datetimes are compared. If both comparands are aware and have different <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from <tt class="docutils literal"><span class="pre">self.utcoffset()</span></tt>).</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">In order to stop comparison from falling back to the default scheme of comparing object addresses, datetime comparison normally raises <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> if the other comparand isn’t also a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object. However, <tt class="docutils literal"><span class="pre">NotImplemented</span></tt> is returned instead if the other comparand has a <tt class="xref py py-meth docutils literal"><span class="pre">timetuple()</span></tt> attribute. This hook gives other kinds of date objects a chance at implementing mixed-type comparison. If not, when a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised unless the comparison is <tt class="docutils literal"><span class="pre">==</span></tt> or <tt class="docutils literal"><span class="pre">!=</span></tt>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> or <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, respectively.</p> </div> </li> </ol> <p><a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects can be used as dictionary keys. In Boolean contexts, all <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects are considered to be true.</p> <p>Instance methods:</p> <dl class="method"> <dt id="datetime.datetime.date"> <tt class="descclassname">datetime.</tt><tt class="descname">date</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.date" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> object with same year, month and day.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.time"> <tt class="descclassname">datetime.</tt><tt class="descname">time</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.time" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object with same hour, minute, second and microsecond. <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>. See also method <a class="reference internal" href="#datetime.datetime.timetz" title="datetime.datetime.timetz"><tt class="xref py py-meth docutils literal"><span class="pre">timetz()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.timetz"> <tt class="descclassname">datetime.</tt><tt class="descname">timetz</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.timetz" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object with same hour, minute, second, microsecond, and tzinfo attributes. See also method <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-meth docutils literal"><span class="pre">time()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.replace"> <tt class="descclassname">datetime.</tt><tt class="descname">replace</tt><big>(</big><span class="optional">[</span><em>year</em><span class="optional">[</span>, <em>month</em><span class="optional">[</span>, <em>day</em><span class="optional">[</span>, <em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.datetime.replace" title="Permalink to this definition">¶</a></dt> <dd><p>Return a datetime with the same attributes, except for those attributes given new values by whichever keyword arguments are specified. Note that <tt class="docutils literal"><span class="pre">tzinfo=None</span></tt> can be specified to create a naive datetime from an aware datetime with no conversion of date and time data.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.astimezone"> <tt class="descclassname">datetime.</tt><tt class="descname">astimezone</tt><big>(</big><em>tz</em><big>)</big><a class="headerlink" href="#datetime.datetime.astimezone" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object with new <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute <em>tz</em>, adjusting the date and time data so the result is the same UTC time as <em>self</em>, but in <em>tz</em>‘s local time.</p> <p><em>tz</em> must be an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass, and its <a class="reference internal" href="#datetime.datetime.utcoffset" title="datetime.datetime.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> and <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> methods must not return <tt class="docutils literal"><span class="pre">None</span></tt>. <em>self</em> must be aware (<tt class="docutils literal"><span class="pre">self.tzinfo</span></tt> must not be <tt class="docutils literal"><span class="pre">None</span></tt>, and <tt class="docutils literal"><span class="pre">self.utcoffset()</span></tt> must not return <tt class="docutils literal"><span class="pre">None</span></tt>).</p> <p>If <tt class="docutils literal"><span class="pre">self.tzinfo</span></tt> is <em>tz</em>, <tt class="docutils literal"><span class="pre">self.astimezone(tz)</span></tt> is equal to <em>self</em>: no adjustment of date or time data is performed. Else the result is local time in time zone <em>tz</em>, representing the same UTC time as <em>self</em>: after <tt class="docutils literal"><span class="pre">astz</span> <span class="pre">=</span> <span class="pre">dt.astimezone(tz)</span></tt>, <tt class="docutils literal"><span class="pre">astz</span> <span class="pre">-</span> <span class="pre">astz.utcoffset()</span></tt> will usually have the same date and time data as <tt class="docutils literal"><span class="pre">dt</span> <span class="pre">-</span> <span class="pre">dt.utcoffset()</span></tt>. The discussion of class <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> explains the cases at Daylight Saving Time transition boundaries where this cannot be achieved (an issue only if <em>tz</em> models both standard and daylight time).</p> <p>If you merely want to attach a time zone object <em>tz</em> to a datetime <em>dt</em> without adjustment of date and time data, use <tt class="docutils literal"><span class="pre">dt.replace(tzinfo=tz)</span></tt>. If you merely want to remove the time zone object from an aware datetime <em>dt</em> without conversion of date and time data, use <tt class="docutils literal"><span class="pre">dt.replace(tzinfo=None)</span></tt>.</p> <p>Note that the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">tzinfo.fromutc()</span></tt></a> method can be overridden in a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass to affect the result returned by <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt></a>. Ignoring error cases, <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt></a> acts like:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">astimezone</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tz</span><span class="p">):</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="n">tz</span><span class="p">:</span> <span class="k">return</span> <span class="bp">self</span> <span class="c"># Convert self to UTC, and attach the new time zone object.</span> <span class="n">utc</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">())</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="n">tz</span><span class="p">)</span> <span class="c"># Convert from UTC to tz's local time.</span> <span class="k">return</span> <span class="n">tz</span><span class="o">.</span><span class="n">fromutc</span><span class="p">(</span><span class="n">utc</span><span class="p">)</span> </pre></div> </div> </dd></dl> <dl class="method"> <dt id="datetime.datetime.utcoffset"> <tt class="descclassname">datetime.</tt><tt class="descname">utcoffset</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.utcoffset" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.utcoffset(self)</span></tt>, and raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing a whole number of minutes with magnitude less than one day.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.dst"> <tt class="descclassname">datetime.</tt><tt class="descname">dst</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.dst" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.dst(self)</span></tt>, and raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing a whole number of minutes with magnitude less than one day.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.tzname"> <tt class="descclassname">datetime.</tt><tt class="descname">tzname</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.tzname" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.tzname(self)</span></tt>, raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt> or a string object,</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.timetuple"> <tt class="descclassname">datetime.</tt><tt class="descname">timetuple</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.timetuple" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><tt class="xref py py-class docutils literal"><span class="pre">time.struct_time</span></tt></a> such as returned by <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><tt class="xref py py-func docutils literal"><span class="pre">time.localtime()</span></tt></a>. <tt class="docutils literal"><span class="pre">d.timetuple()</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">time.struct_time((d.year,</span> <span class="pre">d.month,</span> <span class="pre">d.day,</span> <span class="pre">d.hour,</span> <span class="pre">d.minute,</span> <span class="pre">d.second,</span> <span class="pre">d.weekday(),</span> <span class="pre">yday,</span> <span class="pre">dst))</span></tt>, where <tt class="docutils literal"><span class="pre">yday</span> <span class="pre">=</span> <span class="pre">d.toordinal()</span> <span class="pre">-</span> <span class="pre">date(d.year,</span> <span class="pre">1,</span> <span class="pre">1).toordinal()</span> <span class="pre">+</span> <span class="pre">1</span></tt> is the day number within the current year starting with <tt class="docutils literal"><span class="pre">1</span></tt> for January 1st. The <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> flag of the result is set according to the <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> method: <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt> or <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> returns <tt class="docutils literal"><span class="pre">None</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is set to <tt class="docutils literal"><span class="pre">-1</span></tt>; else if <a class="reference internal" href="#datetime.datetime.dst" title="datetime.datetime.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> returns a non-zero value, <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is set to <tt class="docutils literal"><span class="pre">1</span></tt>; else <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is set to <tt class="docutils literal"><span class="pre">0</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.utctimetuple"> <tt class="descclassname">datetime.</tt><tt class="descname">utctimetuple</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.utctimetuple" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> instance <em>d</em> is naive, this is the same as <tt class="docutils literal"><span class="pre">d.timetuple()</span></tt> except that <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is forced to 0 regardless of what <tt class="docutils literal"><span class="pre">d.dst()</span></tt> returns. DST is never in effect for a UTC time.</p> <p>If <em>d</em> is aware, <em>d</em> is normalized to UTC time, by subtracting <tt class="docutils literal"><span class="pre">d.utcoffset()</span></tt>, and a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><tt class="xref py py-class docutils literal"><span class="pre">time.struct_time</span></tt></a> for the normalized time is returned. <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> is forced to 0. Note that the result’s <tt class="xref py py-attr docutils literal"><span class="pre">tm_year</span></tt> member may be <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MINYEAR</span></tt></a>-1 or <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><tt class="xref py py-const docutils literal"><span class="pre">MAXYEAR</span></tt></a>+1, if <em>d</em>.year was <tt class="docutils literal"><span class="pre">MINYEAR</span></tt> or <tt class="docutils literal"><span class="pre">MAXYEAR</span></tt> and UTC adjustment spills over a year boundary.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.toordinal"> <tt class="descclassname">datetime.</tt><tt class="descname">toordinal</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.toordinal" title="Permalink to this definition">¶</a></dt> <dd><p>Return the proleptic Gregorian ordinal of the date. The same as <tt class="docutils literal"><span class="pre">self.date().toordinal()</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.weekday"> <tt class="descclassname">datetime.</tt><tt class="descname">weekday</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.weekday" title="Permalink to this definition">¶</a></dt> <dd><p>Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The same as <tt class="docutils literal"><span class="pre">self.date().weekday()</span></tt>. See also <a class="reference internal" href="#datetime.datetime.isoweekday" title="datetime.datetime.isoweekday"><tt class="xref py py-meth docutils literal"><span class="pre">isoweekday()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.isoweekday"> <tt class="descclassname">datetime.</tt><tt class="descname">isoweekday</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.isoweekday" title="Permalink to this definition">¶</a></dt> <dd><p>Return the day of the week as an integer, where Monday is 1 and Sunday is 7. The same as <tt class="docutils literal"><span class="pre">self.date().isoweekday()</span></tt>. See also <a class="reference internal" href="#datetime.datetime.weekday" title="datetime.datetime.weekday"><tt class="xref py py-meth docutils literal"><span class="pre">weekday()</span></tt></a>, <a class="reference internal" href="#datetime.datetime.isocalendar" title="datetime.datetime.isocalendar"><tt class="xref py py-meth docutils literal"><span class="pre">isocalendar()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.isocalendar"> <tt class="descclassname">datetime.</tt><tt class="descname">isocalendar</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.isocalendar" title="Permalink to this definition">¶</a></dt> <dd><p>Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The same as <tt class="docutils literal"><span class="pre">self.date().isocalendar()</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.isoformat"> <tt class="descclassname">datetime.</tt><tt class="descname">isoformat</tt><big>(</big><span class="optional">[</span><em>sep</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.datetime.isoformat" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the date and time in ISO 8601 format, YYYY-MM-DDTHH:MM:SS.mmmmmm or, if <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><tt class="xref py py-attr docutils literal"><span class="pre">microsecond</span></tt></a> is 0, YYYY-MM-DDTHH:MM:SS</p> <p>If <a class="reference internal" href="#datetime.datetime.utcoffset" title="datetime.datetime.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> does not return <tt class="docutils literal"><span class="pre">None</span></tt>, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes: YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or, if <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><tt class="xref py py-attr docutils literal"><span class="pre">microsecond</span></tt></a> is 0 YYYY-MM-DDTHH:MM:SS+HH:MM</p> <p>The optional argument <em>sep</em> (default <tt class="docutils literal"><span class="pre">'T'</span></tt>) is a one-character separator, placed between the date and time portions of the result. For example,</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">TZ</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=-</span><span class="mi">399</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">datetime</span><span class="p">(</span><span class="mi">2002</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">TZ</span><span class="p">())</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(</span><span class="s">' '</span><span class="p">)</span> <span class="go">'2002-12-25 00:00:00-06:39'</span> </pre></div> </div> </dd></dl> <dl class="method"> <dt id="datetime.datetime.__str__"> <tt class="descclassname">datetime.</tt><tt class="descname">__str__</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.__str__" title="Permalink to this definition">¶</a></dt> <dd><p>For a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> instance <em>d</em>, <tt class="docutils literal"><span class="pre">str(d)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">d.isoformat('</span> <span class="pre">')</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.ctime"> <tt class="descclassname">datetime.</tt><tt class="descname">ctime</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.datetime.ctime" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the date and time, for example <tt class="docutils literal"><span class="pre">datetime(2002,</span> <span class="pre">12,</span> <span class="pre">4,</span> <span class="pre">20,</span> <span class="pre">30,</span> <span class="pre">40).ctime()</span> <span class="pre">==</span> <span class="pre">'Wed</span> <span class="pre">Dec</span> <span class="pre">4</span> <span class="pre">20:30:40</span> <span class="pre">2002'</span></tt>. <tt class="docutils literal"><span class="pre">d.ctime()</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></tt> on platforms where the native C <tt class="xref c c-func docutils literal"><span class="pre">ctime()</span></tt> function (which <a class="reference internal" href="time.html#time.ctime" title="time.ctime"><tt class="xref py py-func docutils literal"><span class="pre">time.ctime()</span></tt></a> invokes, but which <a class="reference internal" href="#datetime.datetime.ctime" title="datetime.datetime.ctime"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.ctime()</span></tt></a> does not invoke) conforms to the C standard.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.strftime"> <tt class="descclassname">datetime.</tt><tt class="descname">strftime</tt><big>(</big><em>format</em><big>)</big><a class="headerlink" href="#datetime.datetime.strftime" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the date and time, controlled by an explicit format string. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.datetime.__format__"> <tt class="descclassname">datetime.</tt><tt class="descname">__format__</tt><big>(</big><em>format</em><big>)</big><a class="headerlink" href="#datetime.datetime.__format__" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#datetime.datetime.strftime" title="datetime.datetime.strftime"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.strftime()</span></tt></a>. This makes it possible to specify format string for a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a>. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <p>Examples of working with datetime objects:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">time</span> <span class="gp">>>> </span><span class="c"># Using datetime.combine()</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="mi">2005</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">14</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">datetime</span><span class="o">.</span><span class="n">combine</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="go">datetime.datetime(2005, 7, 14, 12, 30)</span> <span class="gp">>>> </span><span class="c"># Using datetime.now() or datetime.utcnow()</span> <span class="gp">>>> </span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="go">datetime.datetime(2007, 12, 6, 16, 29, 43, 79043) # GMT +1</span> <span class="gp">>>> </span><span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span> <span class="go">datetime.datetime(2007, 12, 6, 15, 29, 43, 79060)</span> <span class="gp">>>> </span><span class="c"># Using datetime.strptime()</span> <span class="gp">>>> </span><span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s">"21/11/06 16:30"</span><span class="p">,</span> <span class="s">"</span><span class="si">%d</span><span class="s">/%m/%y %H:%M"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">dt</span> <span class="go">datetime.datetime(2006, 11, 21, 16, 30)</span> <span class="gp">>>> </span><span class="c"># Using datetime.timetuple() to get tuple of all attributes</span> <span class="gp">>>> </span><span class="n">tt</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">tt</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">it</span> <span class="gp">...</span> <span class="go">2006 # year</span> <span class="go">11 # month</span> <span class="go">21 # day</span> <span class="go">16 # hour</span> <span class="go">30 # minute</span> <span class="go">0 # second</span> <span class="go">1 # weekday (0 = Monday)</span> <span class="go">325 # number of days since 1st January</span> <span class="go">-1 # dst - method tzinfo.dst() returned None</span> <span class="gp">>>> </span><span class="c"># Date in ISO format</span> <span class="gp">>>> </span><span class="n">ic</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">it</span> <span class="gp">...</span> <span class="go">2006 # ISO year</span> <span class="go">47 # ISO week</span> <span class="go">2 # ISO weekday</span> <span class="gp">>>> </span><span class="c"># Formatting datetime</span> <span class="gp">>>> </span><span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">"%A, </span><span class="si">%d</span><span class="s">. %B %Y %I:%M%p"</span><span class="p">)</span> <span class="go">'Tuesday, 21. November 2006 04:30PM'</span> <span class="gp">>>> </span><span class="s">'The {1} is {0:</span><span class="si">%d</span><span class="s">}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="s">"day"</span><span class="p">,</span> <span class="s">"month"</span><span class="p">,</span> <span class="s">"time"</span><span class="p">)</span> <span class="go">'The day is 21, the month is November, the time is 04:30PM.'</span> </pre></div> </div> <p>Using datetime with tzinfo:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">tzinfo</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT1</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="c"># DST starts last Sunday in March</span> <span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># ends last Sunday in October</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span><span class="p">:</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">else</span><span class="p">:</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="s">"GMT +1"</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT2</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span> <span class="o">=</span> <span class="n">d</span> <span class="o">-</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">d</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dston</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstoff</span><span class="p">:</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">else</span><span class="p">:</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="s">"GMT +2"</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">gmt1</span> <span class="o">=</span> <span class="n">GMT1</span><span class="p">()</span> <span class="gp">>>> </span><span class="c"># Daylight Saving Time</span> <span class="gp">>>> </span><span class="n">dt1</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2006</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">gmt1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">dt1</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span> <span class="go">datetime.timedelta(0)</span> <span class="gp">>>> </span><span class="n">dt1</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span> <span class="go">datetime.timedelta(0, 3600)</span> <span class="gp">>>> </span><span class="n">dt2</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2006</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">gmt1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span> <span class="go">datetime.timedelta(0, 3600)</span> <span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span> <span class="go">datetime.timedelta(0, 7200)</span> <span class="gp">>>> </span><span class="c"># Convert datetime to another time zone</span> <span class="gp">>>> </span><span class="n">dt3</span> <span class="o">=</span> <span class="n">dt2</span><span class="o">.</span><span class="n">astimezone</span><span class="p">(</span><span class="n">GMT2</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">dt3</span> <span class="go">datetime.datetime(2006, 6, 14, 14, 0, tzinfo=<GMT2 object at 0x...>)</span> <span class="gp">>>> </span><span class="n">dt2</span> <span class="go">datetime.datetime(2006, 6, 14, 13, 0, tzinfo=<GMT1 object at 0x...>)</span> <span class="gp">>>> </span><span class="n">dt2</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">()</span> <span class="o">==</span> <span class="n">dt3</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">()</span> <span class="go">True</span> </pre></div> </div> </div> <div class="section" id="time-objects"> <span id="datetime-time"></span><h2>8.1.5. <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> Objects<a class="headerlink" href="#time-objects" title="Permalink to this headline">¶</a></h2> <p>A time object represents a (local) time of day, independent of any particular day, and subject to adjustment via a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> object.</p> <dl class="class"> <dt id="datetime.time"> <em class="property">class </em><tt class="descclassname">datetime.</tt><tt class="descname">time</tt><big>(</big><span class="optional">[</span><em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.time" title="Permalink to this definition">¶</a></dt> <dd><p>All arguments are optional. <em>tzinfo</em> may be <tt class="docutils literal"><span class="pre">None</span></tt>, or an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass. The remaining arguments may be ints or longs, in the following ranges:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">hour</span> <span class="pre"><</span> <span class="pre">24</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">minute</span> <span class="pre"><</span> <span class="pre">60</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">second</span> <span class="pre"><</span> <span class="pre">60</span></tt></li> <li><tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microsecond</span> <span class="pre"><</span> <span class="pre">1000000</span></tt>.</li> </ul> <p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised. All default to <tt class="docutils literal"><span class="pre">0</span></tt> except <em>tzinfo</em>, which defaults to <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>.</p> </dd></dl> <p>Class attributes:</p> <dl class="attribute"> <dt id="datetime.time.min"> <tt class="descclassname">time.</tt><tt class="descname">min</tt><a class="headerlink" href="#datetime.time.min" title="Permalink to this definition">¶</a></dt> <dd><p>The earliest representable <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, <tt class="docutils literal"><span class="pre">time(0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.max"> <tt class="descclassname">time.</tt><tt class="descname">max</tt><a class="headerlink" href="#datetime.time.max" title="Permalink to this definition">¶</a></dt> <dd><p>The latest representable <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, <tt class="docutils literal"><span class="pre">time(23,</span> <span class="pre">59,</span> <span class="pre">59,</span> <span class="pre">999999)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.resolution"> <tt class="descclassname">time.</tt><tt class="descname">resolution</tt><a class="headerlink" href="#datetime.time.resolution" title="Permalink to this definition">¶</a></dt> <dd><p>The smallest possible difference between non-equal <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects, <tt class="docutils literal"><span class="pre">timedelta(microseconds=1)</span></tt>, although note that arithmetic on <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects is not supported.</p> </dd></dl> <p>Instance attributes (read-only):</p> <dl class="attribute"> <dt id="datetime.time.hour"> <tt class="descclassname">time.</tt><tt class="descname">hour</tt><a class="headerlink" href="#datetime.time.hour" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(24)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.minute"> <tt class="descclassname">time.</tt><tt class="descname">minute</tt><a class="headerlink" href="#datetime.time.minute" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(60)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.second"> <tt class="descclassname">time.</tt><tt class="descname">second</tt><a class="headerlink" href="#datetime.time.second" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(60)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.microsecond"> <tt class="descclassname">time.</tt><tt class="descname">microsecond</tt><a class="headerlink" href="#datetime.time.microsecond" title="Permalink to this definition">¶</a></dt> <dd><p>In <tt class="docutils literal"><span class="pre">range(1000000)</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="datetime.time.tzinfo"> <tt class="descclassname">time.</tt><tt class="descname">tzinfo</tt><a class="headerlink" href="#datetime.time.tzinfo" title="Permalink to this definition">¶</a></dt> <dd><p>The object passed as the tzinfo argument to the <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> constructor, or <tt class="docutils literal"><span class="pre">None</span></tt> if none was passed.</p> </dd></dl> <p>Supported operations:</p> <ul class="simple"> <li>comparison of <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> to <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, where <em>a</em> is considered less than <em>b</em> when <em>a</em> precedes <em>b</em> in time. If one comparand is naive and the other is aware, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised. If both comparands are aware, and have the same <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute, the common <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute is ignored and the base times are compared. If both comparands are aware and have different <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from <tt class="docutils literal"><span class="pre">self.utcoffset()</span></tt>). In order to stop mixed-type comparisons from falling back to the default comparison by object address, when a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised unless the comparison is <tt class="docutils literal"><span class="pre">==</span></tt> or <tt class="docutils literal"><span class="pre">!=</span></tt>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> or <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>, respectively.</li> <li>hash, use as dict key</li> <li>efficient pickling</li> <li>in Boolean contexts, a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object is considered to be true if and only if, after converting it to minutes and subtracting <tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt> (or <tt class="docutils literal"><span class="pre">0</span></tt> if that’s <tt class="docutils literal"><span class="pre">None</span></tt>), the result is non-zero.</li> </ul> <p>Instance methods:</p> <dl class="method"> <dt id="datetime.time.replace"> <tt class="descclassname">time.</tt><tt class="descname">replace</tt><big>(</big><span class="optional">[</span><em>hour</em><span class="optional">[</span>, <em>minute</em><span class="optional">[</span>, <em>second</em><span class="optional">[</span>, <em>microsecond</em><span class="optional">[</span>, <em>tzinfo</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#datetime.time.replace" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> with the same value, except for those attributes given new values by whichever keyword arguments are specified. Note that <tt class="docutils literal"><span class="pre">tzinfo=None</span></tt> can be specified to create a naive <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> from an aware <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>, without conversion of the time data.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.isoformat"> <tt class="descclassname">time.</tt><tt class="descname">isoformat</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.isoformat" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the time in ISO 8601 format, HH:MM:SS.mmmmmm or, if self.microsecond is 0, HH:MM:SS If <a class="reference internal" href="#datetime.time.utcoffset" title="datetime.time.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> does not return <tt class="docutils literal"><span class="pre">None</span></tt>, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes: HH:MM:SS.mmmmmm+HH:MM or, if self.microsecond is 0, HH:MM:SS+HH:MM</p> </dd></dl> <dl class="method"> <dt id="datetime.time.__str__"> <tt class="descclassname">time.</tt><tt class="descname">__str__</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.__str__" title="Permalink to this definition">¶</a></dt> <dd><p>For a time <em>t</em>, <tt class="docutils literal"><span class="pre">str(t)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">t.isoformat()</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.strftime"> <tt class="descclassname">time.</tt><tt class="descname">strftime</tt><big>(</big><em>format</em><big>)</big><a class="headerlink" href="#datetime.time.strftime" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string representing the time, controlled by an explicit format string. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.__format__"> <tt class="descclassname">time.</tt><tt class="descname">__format__</tt><big>(</big><em>format</em><big>)</big><a class="headerlink" href="#datetime.time.__format__" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#datetime.time.strftime" title="datetime.time.strftime"><tt class="xref py py-meth docutils literal"><span class="pre">time.strftime()</span></tt></a>. This makes it possible to specify format string for a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a>. See section <a class="reference internal" href="#strftime-strptime-behavior"><em>strftime() and strptime() Behavior</em></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.utcoffset"> <tt class="descclassname">time.</tt><tt class="descname">utcoffset</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.utcoffset" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.utcoffset(None)</span></tt>, and raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt> or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing a whole number of minutes with magnitude less than one day.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.dst"> <tt class="descclassname">time.</tt><tt class="descname">dst</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.dst" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.dst(None)</span></tt>, and raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt>, or a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object representing a whole number of minutes with magnitude less than one day.</p> </dd></dl> <dl class="method"> <dt id="datetime.time.tzname"> <tt class="descclassname">time.</tt><tt class="descname">tzname</tt><big>(</big><big>)</big><a class="headerlink" href="#datetime.time.tzname" title="Permalink to this definition">¶</a></dt> <dd><p>If <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> is <tt class="docutils literal"><span class="pre">None</span></tt>, returns <tt class="docutils literal"><span class="pre">None</span></tt>, else returns <tt class="docutils literal"><span class="pre">self.tzinfo.tzname(None)</span></tt>, or raises an exception if the latter doesn’t return <tt class="docutils literal"><span class="pre">None</span></tt> or a string object.</p> </dd></dl> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">time</span><span class="p">,</span> <span class="n">tzinfo</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">GMT1</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="s">"Europe/Prague"</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">time</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">GMT1</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">t</span> <span class="go">datetime.time(12, 10, 30, tzinfo=<GMT1 object at 0x...>)</span> <span class="gp">>>> </span><span class="n">gmt</span> <span class="o">=</span> <span class="n">GMT1</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span> <span class="go">'12:10:30+01:00'</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span> <span class="go">datetime.timedelta(0)</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">tzname</span><span class="p">()</span> <span class="go">'Europe/Prague'</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">"%H:%M:%S %Z"</span><span class="p">)</span> <span class="go">'12:10:30 Europe/Prague'</span> <span class="gp">>>> </span><span class="s">'The {} is {:%H:%M}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">"time"</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="go">'The time is 12:10.'</span> </pre></div> </div> </div> <div class="section" id="tzinfo-objects"> <span id="datetime-tzinfo"></span><h2>8.1.6. <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> Objects<a class="headerlink" href="#tzinfo-objects" title="Permalink to this headline">¶</a></h2> <p><a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> is an abstract base class, meaning that this class should not be instantiated directly. You need to derive a concrete subclass, and (at least) supply implementations of the standard <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> methods needed by the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> methods you use. The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module does not supply any concrete subclasses of <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a>.</p> <p>An instance of (a concrete subclass of) <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> can be passed to the constructors for <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects. The latter objects view their attributes as being in local time, and the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> object supports methods revealing offset of local time from UTC, the name of the time zone, and DST offset, all relative to a date or time object passed to them.</p> <p>Special requirement for pickling: A <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass must have an <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> method that can be called with no arguments, else it can be pickled but possibly not unpickled again. This is a technical requirement that may be relaxed in the future.</p> <p>A concrete subclass of <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> may need to implement the following methods. Exactly which methods are needed depends on the uses made of aware <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> objects. If in doubt, simply implement all of them.</p> <dl class="method"> <dt id="datetime.tzinfo.utcoffset"> <tt class="descclassname">tzinfo.</tt><tt class="descname">utcoffset</tt><big>(</big><em>self</em>, <em>dt</em><big>)</big><a class="headerlink" href="#datetime.tzinfo.utcoffset" title="Permalink to this definition">¶</a></dt> <dd><p>Return offset of local time from UTC, in minutes east of UTC. If local time is west of UTC, this should be negative. Note that this is intended to be the total offset from UTC; for example, if a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> object represents both time zone and DST adjustments, <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> should return their sum. If the UTC offset isn’t known, return <tt class="docutils literal"><span class="pre">None</span></tt>. Else the value returned must be a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object specifying a whole number of minutes in the range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset must be less than one day). Most implementations of <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> will probably look like one of these two:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">return</span> <span class="n">CONSTANT</span> <span class="c"># fixed-offset class</span> <span class="k">return</span> <span class="n">CONSTANT</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="c"># daylight-aware class</span> </pre></div> </div> <p>If <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> does not return <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> should not return <tt class="docutils literal"><span class="pre">None</span></tt> either.</p> <p>The default implementation of <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> raises <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.tzinfo.dst"> <tt class="descclassname">tzinfo.</tt><tt class="descname">dst</tt><big>(</big><em>self</em>, <em>dt</em><big>)</big><a class="headerlink" href="#datetime.tzinfo.dst" title="Permalink to this definition">¶</a></dt> <dd><p>Return the daylight saving time (DST) adjustment, in minutes east of UTC, or <tt class="docutils literal"><span class="pre">None</span></tt> if DST information isn’t known. Return <tt class="docutils literal"><span class="pre">timedelta(0)</span></tt> if DST is not in effect. If DST is in effect, return the offset as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> object (see <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a> for details). Note that DST offset, if applicable, has already been added to the UTC offset returned by <a class="reference internal" href="#datetime.tzinfo.utcoffset" title="datetime.tzinfo.utcoffset"><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt></a>, so there’s no need to consult <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> unless you’re interested in obtaining DST info separately. For example, <a class="reference internal" href="#datetime.datetime.timetuple" title="datetime.datetime.timetuple"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.timetuple()</span></tt></a> calls its <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-attr docutils literal"><span class="pre">tzinfo</span></tt></a> attribute’s <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> method to determine how the <tt class="xref py py-attr docutils literal"><span class="pre">tm_isdst</span></tt> flag should be set, and <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">tzinfo.fromutc()</span></tt></a> calls <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> to account for DST changes when crossing time zones.</p> <p>An instance <em>tz</em> of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass that models both standard and daylight times must be consistent in this sense:</p> <p><tt class="docutils literal"><span class="pre">tz.utcoffset(dt)</span> <span class="pre">-</span> <span class="pre">tz.dst(dt)</span></tt></p> <p>must return the same result for every <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> <em>dt</em> with <tt class="docutils literal"><span class="pre">dt.tzinfo</span> <span class="pre">==</span> <span class="pre">tz</span></tt> For sane <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclasses, this expression yields the time zone’s “standard offset”, which should not depend on the date or the time, but only on geographic location. The implementation of <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.astimezone()</span></tt></a> relies on this, but cannot detect violations; it’s the programmer’s responsibility to ensure it. If a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass cannot guarantee this, it may be able to override the default implementation of <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">tzinfo.fromutc()</span></tt></a> to work correctly with <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> regardless.</p> <p>Most implementations of <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> will probably look like one of these two:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="c"># a fixed-offset class: doesn't account for DST</span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> </pre></div> </div> <p>or</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="c"># Code to set dston and dstoff to the time zone's DST</span> <span class="c"># transition times based on the input dt.year, and expressed</span> <span class="c"># in standard local time. Then</span> <span class="k">if</span> <span class="n">dston</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span> <span class="o"><</span> <span class="n">dstoff</span><span class="p">:</span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> </pre></div> </div> <p>The default implementation of <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">dst()</span></tt></a> raises <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="datetime.tzinfo.tzname"> <tt class="descclassname">tzinfo.</tt><tt class="descname">tzname</tt><big>(</big><em>self</em>, <em>dt</em><big>)</big><a class="headerlink" href="#datetime.tzinfo.tzname" title="Permalink to this definition">¶</a></dt> <dd><p>Return the time zone name corresponding to the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object <em>dt</em>, as a string. Nothing about string names is defined by the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module, and there’s no requirement that it mean anything in particular. For example, “GMT”, “UTC”, “-500”, “-5:00”, “EDT”, “US/Eastern”, “America/New York” are all valid replies. Return <tt class="docutils literal"><span class="pre">None</span></tt> if a string name isn’t known. Note that this is a method rather than a fixed string primarily because some <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclasses will wish to return different names depending on the specific value of <em>dt</em> passed, especially if the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> class is accounting for daylight time.</p> <p>The default implementation of <a class="reference internal" href="#datetime.tzinfo.tzname" title="datetime.tzinfo.tzname"><tt class="xref py py-meth docutils literal"><span class="pre">tzname()</span></tt></a> raises <a class="reference internal" href="exceptions.html#exceptions.NotImplementedError" title="exceptions.NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a>.</p> </dd></dl> <p>These methods are called by a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> or <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object, in response to their methods of the same names. A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object passes itself as the argument, and a <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> object passes <tt class="docutils literal"><span class="pre">None</span></tt> as the argument. A <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass’s methods should therefore be prepared to accept a <em>dt</em> argument of <tt class="docutils literal"><span class="pre">None</span></tt>, or of class <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>.</p> <p>When <tt class="docutils literal"><span class="pre">None</span></tt> is passed, it’s up to the class designer to decide the best response. For example, returning <tt class="docutils literal"><span class="pre">None</span></tt> is appropriate if the class wishes to say that time objects don’t participate in the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> protocols. It may be more useful for <tt class="docutils literal"><span class="pre">utcoffset(None)</span></tt> to return the standard UTC offset, as there is no other convention for discovering the standard offset.</p> <p>When a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object is passed in response to a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> method, <tt class="docutils literal"><span class="pre">dt.tzinfo</span></tt> is the same object as <em>self</em>. <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> methods can rely on this, unless user code calls <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> methods directly. The intent is that the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> methods interpret <em>dt</em> as being in local time, and not need worry about objects in other timezones.</p> <p>There is one more <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> method that a subclass may wish to override:</p> <dl class="method"> <dt id="datetime.tzinfo.fromutc"> <tt class="descclassname">tzinfo.</tt><tt class="descname">fromutc</tt><big>(</big><em>self</em>, <em>dt</em><big>)</big><a class="headerlink" href="#datetime.tzinfo.fromutc" title="Permalink to this definition">¶</a></dt> <dd><p>This is called from the default <a class="reference internal" href="#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><tt class="xref py py-class docutils literal"><span class="pre">datetime.astimezone()</span></tt></a> implementation. When called from that, <tt class="docutils literal"><span class="pre">dt.tzinfo</span></tt> is <em>self</em>, and <em>dt</em>‘s date and time data are to be viewed as expressing a UTC time. The purpose of <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></a> is to adjust the date and time data, returning an equivalent datetime in <em>self</em>‘s local time.</p> <p>Most <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclasses should be able to inherit the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></a> implementation without problems. It’s strong enough to handle fixed-offset time zones, and time zones accounting for both standard and daylight time, and the latter even if the DST transition times differ in different years. An example of a time zone the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></a> implementation may not handle correctly in all cases is one where the standard offset (from UTC) depends on the specific date and time passed, which can happen for political reasons. The default implementations of <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> and <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></a> may not produce the result you want if the result is one of the hours straddling the moment the standard offset changes.</p> <p>Skipping code for error cases, the default <a class="reference internal" href="#datetime.tzinfo.fromutc" title="datetime.tzinfo.fromutc"><tt class="xref py py-meth docutils literal"><span class="pre">fromutc()</span></tt></a> implementation acts like:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">fromutc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="c"># raise ValueError error if dt.tzinfo is not self</span> <span class="n">dtoff</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">utcoffset</span><span class="p">()</span> <span class="n">dtdst</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span> <span class="c"># raise ValueError if dtoff is None or dtdst is None</span> <span class="n">delta</span> <span class="o">=</span> <span class="n">dtoff</span> <span class="o">-</span> <span class="n">dtdst</span> <span class="c"># this is self's standard offset</span> <span class="k">if</span> <span class="n">delta</span><span class="p">:</span> <span class="n">dt</span> <span class="o">+=</span> <span class="n">delta</span> <span class="c"># convert to standard local time</span> <span class="n">dtdst</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">dst</span><span class="p">()</span> <span class="c"># raise ValueError if dtdst is None</span> <span class="k">if</span> <span class="n">dtdst</span><span class="p">:</span> <span class="k">return</span> <span class="n">dt</span> <span class="o">+</span> <span class="n">dtdst</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">dt</span> </pre></div> </div> </dd></dl> <p>Example <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> classes:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">tzinfo</span><span class="p">,</span> <span class="n">timedelta</span><span class="p">,</span> <span class="n">datetime</span> <span class="n">ZERO</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="n">HOUR</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="c"># A UTC class.</span> <span class="k">class</span> <span class="nc">UTC</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="sd">"""UTC"""</span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="s">"UTC"</span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="n">utc</span> <span class="o">=</span> <span class="n">UTC</span><span class="p">()</span> <span class="c"># A class building tzinfo objects for fixed-offset time zones.</span> <span class="c"># Note that FixedOffset(0, "UTC") is a different way to build a</span> <span class="c"># UTC tzinfo object.</span> <span class="k">class</span> <span class="nc">FixedOffset</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="sd">"""Fixed offset in minutes east from UTC."""</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">__offset</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span> <span class="o">=</span> <span class="n">offset</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">__name</span> <span class="o">=</span> <span class="n">name</span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__offset</span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__name</span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="c"># A class capturing the platform's idea of local time.</span> <span class="kn">import</span> <span class="nn">time</span> <span class="kn">as</span> <span class="nn">_time</span> <span class="n">STDOFFSET</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span> <span class="o">=</span> <span class="o">-</span><span class="n">_time</span><span class="o">.</span><span class="n">timezone</span><span class="p">)</span> <span class="k">if</span> <span class="n">_time</span><span class="o">.</span><span class="n">daylight</span><span class="p">:</span> <span class="n">DSTOFFSET</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span> <span class="o">=</span> <span class="o">-</span><span class="n">_time</span><span class="o">.</span><span class="n">altzone</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">DSTOFFSET</span> <span class="o">=</span> <span class="n">STDOFFSET</span> <span class="n">DSTDIFF</span> <span class="o">=</span> <span class="n">DSTOFFSET</span> <span class="o">-</span> <span class="n">STDOFFSET</span> <span class="k">class</span> <span class="nc">LocalTimezone</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">DSTOFFSET</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">STDOFFSET</span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">DSTDIFF</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="n">_time</span><span class="o">.</span><span class="n">tzname</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_isdst</span><span class="p">(</span><span class="n">dt</span><span class="p">)]</span> <span class="k">def</span> <span class="nf">_isdst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="n">tt</span> <span class="o">=</span> <span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">day</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">hour</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">minute</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">second</span><span class="p">,</span> <span class="n">dt</span><span class="o">.</span><span class="n">weekday</span><span class="p">(),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="n">stamp</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">tt</span><span class="p">)</span> <span class="n">tt</span> <span class="o">=</span> <span class="n">_time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">stamp</span><span class="p">)</span> <span class="k">return</span> <span class="n">tt</span><span class="o">.</span><span class="n">tm_isdst</span> <span class="o">></span> <span class="mi">0</span> <span class="n">Local</span> <span class="o">=</span> <span class="n">LocalTimezone</span><span class="p">()</span> <span class="c"># A complete implementation of current DST rules for major US time zones.</span> <span class="k">def</span> <span class="nf">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span> <span class="n">days_to_go</span> <span class="o">=</span> <span class="mi">6</span> <span class="o">-</span> <span class="n">dt</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="k">if</span> <span class="n">days_to_go</span><span class="p">:</span> <span class="n">dt</span> <span class="o">+=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days_to_go</span><span class="p">)</span> <span class="k">return</span> <span class="n">dt</span> <span class="c"># US DST Rules</span> <span class="c">#</span> <span class="c"># This is a simplified (i.e., wrong for a few cases) set of rules for US</span> <span class="c"># DST start and end times. For a complete and up-to-date set of DST rules</span> <span class="c"># and timezone definitions, visit the Olson Database (or try pytz):</span> <span class="c"># http://www.twinsun.com/tz/tz-link.htm</span> <span class="c"># http://sourceforge.net/projects/pytz/ (might not be up-to-date)</span> <span class="c">#</span> <span class="c"># In the US, since 2007, DST starts at 2am (standard time) on the second</span> <span class="c"># Sunday in March, which is the first Sunday on or after Mar 8.</span> <span class="n">DSTSTART_2007</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c"># and ends at 2am (DST time; 1am standard time) on the first Sunday of Nov.</span> <span class="n">DSTEND_2007</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># From 1987 to 2006, DST used to start at 2am (standard time) on the first</span> <span class="c"># Sunday in April and to end at 2am (DST time; 1am standard time) on the last</span> <span class="c"># Sunday of October, which is the first Sunday on or after Oct 25.</span> <span class="n">DSTSTART_1987_2006</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="n">DSTEND_1987_2006</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># From 1967 to 1986, DST used to start at 2am (standard time) on the last</span> <span class="c"># Sunday in April (the one on or after April 24) and to end at 2am (DST time;</span> <span class="c"># 1am standard time) on the last Sunday of October, which is the first Sunday</span> <span class="c"># on or after Oct 25.</span> <span class="n">DSTSTART_1967_1986</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="n">DSTEND_1967_1986</span> <span class="o">=</span> <span class="n">DSTEND_1987_2006</span> <span class="k">class</span> <span class="nc">USTimeZone</span><span class="p">(</span><span class="n">tzinfo</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hours</span><span class="p">,</span> <span class="n">reprname</span><span class="p">,</span> <span class="n">stdname</span><span class="p">,</span> <span class="n">dstname</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="n">hours</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">reprname</span> <span class="o">=</span> <span class="n">reprname</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdname</span> <span class="o">=</span> <span class="n">stdname</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstname</span> <span class="o">=</span> <span class="n">dstname</span> <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reprname</span> <span class="k">def</span> <span class="nf">tzname</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">dstname</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdname</span> <span class="k">def</span> <span class="nf">utcoffset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdoffset</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="k">def</span> <span class="nf">dst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dt</span><span class="p">):</span> <span class="k">if</span> <span class="n">dt</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="c"># An exception may be sensible here, in one or both cases.</span> <span class="c"># It depends on how you want to treat them. The default</span> <span class="c"># fromutc() implementation (called by the default astimezone()</span> <span class="c"># implementation) passes a datetime with dt.tzinfo is self.</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="k">assert</span> <span class="n">dt</span><span class="o">.</span><span class="n">tzinfo</span> <span class="ow">is</span> <span class="bp">self</span> <span class="c"># Find start and end times for US DST. For years before 1967, return</span> <span class="c"># ZERO for no DST.</span> <span class="k">if</span> <span class="mi">2006</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">:</span> <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_2007</span><span class="p">,</span> <span class="n">DSTEND_2007</span> <span class="k">elif</span> <span class="mi">1986</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span> <span class="o"><</span> <span class="mi">2007</span><span class="p">:</span> <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_1987_2006</span><span class="p">,</span> <span class="n">DSTEND_1987_2006</span> <span class="k">elif</span> <span class="mi">1966</span> <span class="o"><</span> <span class="n">dt</span><span class="o">.</span><span class="n">year</span> <span class="o"><</span> <span class="mi">1987</span><span class="p">:</span> <span class="n">dststart</span><span class="p">,</span> <span class="n">dstend</span> <span class="o">=</span> <span class="n">DSTSTART_1967_1986</span><span class="p">,</span> <span class="n">DSTEND_1967_1986</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="n">start</span> <span class="o">=</span> <span class="n">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dststart</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">))</span> <span class="n">end</span> <span class="o">=</span> <span class="n">first_sunday_on_or_after</span><span class="p">(</span><span class="n">dstend</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span><span class="p">))</span> <span class="c"># Can't compare naive to aware objects, so strip the timezone from</span> <span class="c"># dt first.</span> <span class="k">if</span> <span class="n">start</span> <span class="o"><=</span> <span class="n">dt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span> <span class="o"><</span> <span class="n">end</span><span class="p">:</span> <span class="k">return</span> <span class="n">HOUR</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">ZERO</span> <span class="n">Eastern</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="s">"Eastern"</span><span class="p">,</span> <span class="s">"EST"</span><span class="p">,</span> <span class="s">"EDT"</span><span class="p">)</span> <span class="n">Central</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">6</span><span class="p">,</span> <span class="s">"Central"</span><span class="p">,</span> <span class="s">"CST"</span><span class="p">,</span> <span class="s">"CDT"</span><span class="p">)</span> <span class="n">Mountain</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">,</span> <span class="s">"Mountain"</span><span class="p">,</span> <span class="s">"MST"</span><span class="p">,</span> <span class="s">"MDT"</span><span class="p">)</span> <span class="n">Pacific</span> <span class="o">=</span> <span class="n">USTimeZone</span><span class="p">(</span><span class="o">-</span><span class="mi">8</span><span class="p">,</span> <span class="s">"Pacific"</span><span class="p">,</span> <span class="s">"PST"</span><span class="p">,</span> <span class="s">"PDT"</span><span class="p">)</span> </pre></div> </div> <p>Note that there are unavoidable subtleties twice per year in a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass accounting for both standard and daylight time, at the DST transition points. For concreteness, consider US Eastern (UTC -0500), where EDT begins the minute after 1:59 (EST) on the second Sunday in March, and ends the minute after 1:59 (EDT) on the first Sunday in November:</p> <div class="highlight-python"><pre> UTC 3:MM 4:MM 5:MM 6:MM 7:MM 8:MM EST 22:MM 23:MM 0:MM 1:MM 2:MM 3:MM EDT 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM start 22:MM 23:MM 0:MM 1:MM 3:MM 4:MM end 23:MM 0:MM 1:MM 1:MM 2:MM 3:MM</pre> </div> <p>When DST starts (the “start” line), the local wall clock leaps from 1:59 to 3:00. A wall time of the form 2:MM doesn’t really make sense on that day, so <tt class="docutils literal"><span class="pre">astimezone(Eastern)</span></tt> won’t deliver a result with <tt class="docutils literal"><span class="pre">hour</span> <span class="pre">==</span> <span class="pre">2</span></tt> on the day DST begins. In order for <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> to make this guarantee, the <tt class="xref py py-meth docutils literal"><span class="pre">rzinfo.dst()</span></tt> method must consider times in the “missing hour” (2:MM for Eastern) to be in daylight time.</p> <p>When DST ends (the “end” line), there’s a potentially worse problem: there’s an hour that can’t be spelled unambiguously in local wall time: the last hour of daylight time. In Eastern, that’s times of the form 5:MM UTC on the day daylight time ends. The local wall clock leaps from 1:59 (daylight time) back to 1:00 (standard time) again. Local times of the form 1:MM are ambiguous. <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> mimics the local clock’s behavior by mapping two adjacent UTC hours into the same local hour then. In the Eastern example, UTC times of the form 5:MM and 6:MM both map to 1:MM when converted to Eastern. In order for <tt class="xref py py-meth docutils literal"><span class="pre">astimezone()</span></tt> to make this guarantee, the <a class="reference internal" href="#datetime.tzinfo.dst" title="datetime.tzinfo.dst"><tt class="xref py py-meth docutils literal"><span class="pre">tzinfo.dst()</span></tt></a> method must consider times in the “repeated hour” to be in standard time. This is easily arranged, as in the example, by expressing DST switch times in the time zone’s standard local time.</p> <p>Applications that can’t bear such ambiguities should avoid using hybrid <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclasses; there are no ambiguities when using UTC, or any other fixed-offset <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> subclass (such as a class representing only EST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)).</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><a class="reference external" href="http://pypi.python.org/pypi/pytz/">pytz</a></dt> <dd><p class="first">The standard library has no <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></a> instances, but there exists a third-party library which brings the <em>IANA timezone database</em> (also known as the Olson database) to Python: <em>pytz</em>.</p> <p class="last"><em>pytz</em> contains up-to-date information and its usage is recommended.</p> </dd> <dt><a class="reference external" href="http://www.iana.org/time-zones">IANA timezone database</a></dt> <dd>The Time Zone Database (often called tz or zoneinfo) contains code and data that represent the history of local time for many representative locations around the globe. It is updated periodically to reflect changes made by political bodies to time zone boundaries, UTC offsets, and daylight-saving rules.</dd> </dl> </div> </div> <div class="section" id="strftime-and-strptime-behavior"> <span id="strftime-strptime-behavior"></span><h2>8.1.7. <tt class="xref py py-meth docutils literal"><span class="pre">strftime()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> Behavior<a class="headerlink" href="#strftime-and-strptime-behavior" title="Permalink to this headline">¶</a></h2> <p><a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a>, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a>, and <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects all support a <tt class="docutils literal"><span class="pre">strftime(format)</span></tt> method, to create a string representing the time under the control of an explicit format string. Broadly speaking, <tt class="docutils literal"><span class="pre">d.strftime(fmt)</span></tt> acts like the <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></a> module’s <tt class="docutils literal"><span class="pre">time.strftime(fmt,</span> <span class="pre">d.timetuple())</span></tt> although not all objects support a <tt class="xref py py-meth docutils literal"><span class="pre">timetuple()</span></tt> method.</p> <p>Conversely, the <a class="reference internal" href="#datetime.datetime.strptime" title="datetime.datetime.strptime"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.strptime()</span></tt></a> class method creates a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> object from a string representing a date and time and a corresponding format string. <tt class="docutils literal"><span class="pre">datetime.strptime(date_string,</span> <span class="pre">format)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">datetime(*(time.strptime(date_string,</span> <span class="pre">format)[0:6]))</span></tt>.</p> <p>For <a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> objects, the format codes for year, month, and day should not be used, as time objects have no such values. If they’re used anyway, <tt class="docutils literal"><span class="pre">1900</span></tt> is substituted for the year, and <tt class="docutils literal"><span class="pre">1</span></tt> for the month and day.</p> <p>For <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> objects, the format codes for hours, minutes, seconds, and microseconds should not be used, as <a class="reference internal" href="#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">date</span></tt></a> objects have no such values. If they’re used anyway, <tt class="docutils literal"><span class="pre">0</span></tt> is substituted for them.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6: </span><a class="reference internal" href="#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a> and <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> objects support a <tt class="docutils literal"><span class="pre">%f</span></tt> format code which expands to the number of microseconds in the object, zero-padded on the left to six places.</p> <p>For a naive object, the <tt class="docutils literal"><span class="pre">%z</span></tt> and <tt class="docutils literal"><span class="pre">%Z</span></tt> format codes are replaced by empty strings.</p> <p>For an aware object:</p> <dl class="docutils"> <dt><tt class="docutils literal"><span class="pre">%z</span></tt></dt> <dd><tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt> is transformed into a 5-character string of the form +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM is a 2-digit string giving the number of UTC offset minutes. For example, if <tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt> returns <tt class="docutils literal"><span class="pre">timedelta(hours=-3,</span> <span class="pre">minutes=-30)</span></tt>, <tt class="docutils literal"><span class="pre">%z</span></tt> is replaced with the string <tt class="docutils literal"><span class="pre">'-0330'</span></tt>.</dd> <dt><tt class="docutils literal"><span class="pre">%Z</span></tt></dt> <dd>If <tt class="xref py py-meth docutils literal"><span class="pre">tzname()</span></tt> returns <tt class="docutils literal"><span class="pre">None</span></tt>, <tt class="docutils literal"><span class="pre">%Z</span></tt> is replaced by an empty string. Otherwise <tt class="docutils literal"><span class="pre">%Z</span></tt> is replaced by the returned value, which must be a string.</dd> </dl> <p>The full set of format codes supported varies across platforms, because Python calls the platform C library’s <tt class="xref py py-func docutils literal"><span class="pre">strftime()</span></tt> function, and platform variations are common.</p> <p>The following is a list of all the format codes that the C standard (1989 version) requires, and these work on all platforms with a standard C implementation. Note that the 1999 version of the C standard added additional format codes.</p> <p>The exact range of years for which <tt class="xref py py-meth docutils literal"><span class="pre">strftime()</span></tt> works also varies across platforms. Regardless of platform, years before 1900 cannot be used.</p> <table border="1" class="docutils"> <colgroup> <col width="22%" /> <col width="64%" /> <col width="14%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Directive</th> <th class="head">Meaning</th> <th class="head">Notes</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%a</span></tt></td> <td>Locale’s abbreviated weekday name.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%A</span></tt></td> <td>Locale’s full weekday name.</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%b</span></tt></td> <td>Locale’s abbreviated month name.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%B</span></tt></td> <td>Locale’s full month name.</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%c</span></tt></td> <td>Locale’s appropriate date and time representation.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%d</span></tt></td> <td>Day of the month as a decimal number [01,31].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%f</span></tt></td> <td>Microsecond as a decimal number [0,999999], zero-padded on the left</td> <td>(1)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%H</span></tt></td> <td>Hour (24-hour clock) as a decimal number [00,23].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%I</span></tt></td> <td>Hour (12-hour clock) as a decimal number [01,12].</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%j</span></tt></td> <td>Day of the year as a decimal number [001,366].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%m</span></tt></td> <td>Month as a decimal number [01,12].</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%M</span></tt></td> <td>Minute as a decimal number [00,59].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%p</span></tt></td> <td>Locale’s equivalent of either AM or PM.</td> <td>(2)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%S</span></tt></td> <td>Second as a decimal number [00,61].</td> <td>(3)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%U</span></tt></td> <td>Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.</td> <td>(4)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%w</span></tt></td> <td>Weekday as a decimal number [0(Sunday),6].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%W</span></tt></td> <td>Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.</td> <td>(4)</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%x</span></tt></td> <td>Locale’s appropriate date representation.</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%X</span></tt></td> <td>Locale’s appropriate time representation.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%y</span></tt></td> <td>Year without century as a decimal number [00,99].</td> <td> </td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%Y</span></tt></td> <td>Year with century as a decimal number.</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%z</span></tt></td> <td>UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).</td> <td>(5)</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">%Z</span></tt></td> <td>Time zone name (empty string if the object is naive).</td> <td> </td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">%%</span></tt></td> <td>A literal <tt class="docutils literal"><span class="pre">'%'</span></tt> character.</td> <td> </td> </tr> </tbody> </table> <p>Notes:</p> <ol class="arabic simple"> <li>When used with the <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> method, the <tt class="docutils literal"><span class="pre">%f</span></tt> directive accepts from one to six digits and zero pads on the right. <tt class="docutils literal"><span class="pre">%f</span></tt> is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available).</li> <li>When used with the <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> method, the <tt class="docutils literal"><span class="pre">%p</span></tt> directive only affects the output hour field if the <tt class="docutils literal"><span class="pre">%I</span></tt> directive is used to parse the hour.</li> <li>The range really is <tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">61</span></tt>; according to the Posix standard this accounts for leap seconds and the (very rare) double leap seconds. The <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-mod docutils literal"><span class="pre">time</span></tt></a> module may produce and does accept leap seconds since it is based on the Posix standard, but the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module does not accept leap seconds in <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> input nor will it produce them in <tt class="xref py py-func docutils literal"><span class="pre">strftime()</span></tt> output.</li> <li>When used with the <tt class="xref py py-meth docutils literal"><span class="pre">strptime()</span></tt> method, <tt class="docutils literal"><span class="pre">%U</span></tt> and <tt class="docutils literal"><span class="pre">%W</span></tt> are only used in calculations when the day of the week and the year are specified.</li> <li>For example, if <tt class="xref py py-meth docutils literal"><span class="pre">utcoffset()</span></tt> returns <tt class="docutils literal"><span class="pre">timedelta(hours=-3,</span> <span class="pre">minutes=-30)</span></tt>, <tt class="docutils literal"><span class="pre">%z</span></tt> is replaced with the string <tt class="docutils literal"><span class="pre">'-0330'</span></tt>.</li> </ol> <p class="rubric">Footnotes</p> <table class="docutils footnote" frame="void" id="id2" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>If, that is, we ignore the effects of Relativity</td></tr> </tbody> </table> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">8.1. <tt class="docutils literal"><span class="pre">datetime</span></tt> — Basic date and time types</a><ul> <li><a class="reference internal" href="#available-types">8.1.1. Available Types</a></li> <li><a class="reference internal" href="#timedelta-objects">8.1.2. <tt class="docutils literal"><span class="pre">timedelta</span></tt> Objects</a></li> <li><a class="reference internal" href="#date-objects">8.1.3. <tt class="docutils literal"><span class="pre">date</span></tt> Objects</a></li> <li><a class="reference internal" href="#datetime-objects">8.1.4. <tt class="docutils literal"><span class="pre">datetime</span></tt> Objects</a></li> <li><a class="reference internal" href="#time-objects">8.1.5. <tt class="docutils literal"><span class="pre">time</span></tt> Objects</a></li> <li><a class="reference internal" href="#tzinfo-objects">8.1.6. <tt class="docutils literal"><span class="pre">tzinfo</span></tt> Objects</a></li> <li><a class="reference internal" href="#strftime-and-strptime-behavior">8.1.7. <tt class="docutils literal"><span class="pre">strftime()</span></tt> and <tt class="docutils literal"><span class="pre">strptime()</span></tt> Behavior</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="datatypes.html" title="previous chapter">8. Data Types</a></p> <h4>Next topic</h4> <p class="topless"><a href="calendar.html" title="next chapter">8.2. <tt class="docutils literal"><span class="pre">calendar</span></tt> — General calendar-related functions</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/datetime.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="calendar.html" title="8.2. calendar — General calendar-related functions" >next</a> |</li> <li class="right" > <a href="datatypes.html" title="8. Data Types" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="datatypes.html" >8. Data Types</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close