-
Notifications
You must be signed in to change notification settings - Fork 17
/
Day_14_date_time.py
171 lines (106 loc) · 3.42 KB
/
Day_14_date_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <headingcell level=1>
# Overview
# <markdowncell>
# intro to understanding timestamps and datetime in context of `PfDA`
#
# * [datetime](http://docs.python.org/2/library/datetime.html) standard library -- nicely explained in [PyMOTW](http://pymotw.com/2/datetime/index.html)
# * [time](http://docs.python.org/2/library/time.html) standard library
#
# Also two modules available in EPD among other places:
#
# * [pytz](http://pytz.sourceforge.net/)
# * [python_dateutil](ttp://labix.org/python-dateutil)
#
# Wonderful tutorial on converting among various time formats: [Date and Time Representation in Python](http://www.seehuhn.de/pages/pdate)
# <codecell>
# http://docs.python.org/2/library/datetime.html
# http://docs.python.org/2/library/time.html
import datetime
import time
import pytz
import dateutil
# <headingcell level=1>
# datetime.datetime.now and utcnow
# <markdowncell>
# Basic task: get current local datetime -- notice no timezone info -- datetime.now() is "timezone naive"
# <codecell>
t = datetime.datetime.now()
print t
print 'hour :', t.hour
print 'minute:', t.minute
print 'second:', t.second
print 'microsecond:', t.microsecond
print 'tzinfo:', t.tzinfo
# <markdowncell>
# Similarly -- can get UTC http://en.wikipedia.org/wiki/Coordinated_Universal_Time
# <codecell>
t = datetime.datetime.utcnow()
print t
print 'hour :', t.hour
print 'minute:', t.minute
print 'second:', t.second
print 'microsecond:', t.microsecond
print 'tzinfo:', t.tzinfo
# <headingcell level=1>
# time zones
# <markdowncell>
# I've used pytz for time zones...I think dateutil has similar functionality but not sure
# <codecell>
pytz.utc
# <codecell>
# I'm assuming for our purposes here that we are in Pacific time
pacific_tz = pytz.timezone('US/Pacific')
# <codecell>
# make tz-aware datetimes
t = pacific_tz.localize(datetime.datetime.now())
t_utc = pytz.utc.localize(datetime.datetime.utcnow())
print t
print 'hour :', t.hour
print 'minute:', t.minute
print 'second:', t.second
print 'microsecond:', t.microsecond
print 'tzinfo:', t.tzinfo
# <codecell>
# example of using astimezone to represent in different tz
t.astimezone(pytz.utc)
# <codecell>
t, t_utc, t_utc-t, (t_utc-t).total_seconds()
# <headingcell level=1>
# time module
# <codecell>
# http://pymotw.com/2/time/index.html
time.ctime()
# <codecell>
# number of seconds since Jan 1, 1970 UTC
# http://www.epochconverter.com/
time.time()
# <codecell>
# use of fromtimestamp to convert Unix Epoch time to datetime.datetime
datetime.datetime.fromtimestamp(time.time())
# <codecell>
(datetime.datetime.now() - datetime.datetime.fromtimestamp(time.time())).total_seconds()
# <codecell>
datetime.datetime(1970,1,1,0,0,0)
# <codecell>
datetime.datetime.fromtimestamp(0.0)
# <codecell>
# show that if we are Pacific time as local time -- that 0 epoch time UTC
pacific_tz.localize(datetime.datetime.fromtimestamp(0.0)).astimezone(pytz.utc)
# <codecell>
# http://www.seehuhn.de/pages/pdate
# coverting datetime to epoch time
t=datetime.datetime.now()
timestamp_now = time.mktime(t.timetuple())+1e-6*t.microsecond
pacific_tz.localize(datetime.datetime.fromtimestamp(timestamp_now))
# <headingcell level=1>
# Any need to hardwire Pacific Time? No...use tzlocal perhaps
# <codecell>
import dateutil
# <codecell>
# does tzlocal work?
t = datetime.datetime.now(dateutil.tz.tzlocal())
t.tzname()
# <codecell>
t.astimezone(pytz.utc)