FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
effectivepython/example_code/item_45.py at master · AISTLAB/effectivepython · GitHub
AISTLAB
/
effectivepython
Public
forked from
bslatkin/effectivepython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
effectivepython
/
example_code
/
item_45.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
95 lines (72 loc) · 2.26 KB
Breadcrumbs
effectivepython
/
example_code
/
item_45.py
Copy path
File metadata and controls
executable file
·
95 lines (72 loc) · 2.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Preamble to mimick book environment
import
logging
from
pprint
import
pprint
from
sys
import
stdout
as
STDOUT
# Example 1
from
time
import
localtime
,
strftime
now
=
1407694710
local_tuple
=
localtime
(
now
)
time_format
=
'%Y-%m-%d %H:%M:%S'
time_str
=
strftime
(
time_format
,
local_tuple
)
print
(
time_str
)
# Example 2
from
time
import
mktime
,
strptime
time_tuple
=
strptime
(
time_str
,
time_format
)
utc_now
=
mktime
(
time_tuple
)
print
(
utc_now
)
# Example 3
parse_format
=
'%Y-%m-%d %H:%M:%S %Z'
depart_sfo
=
'2014-05-01 15:45:16 PDT'
time_tuple
=
strptime
(
depart_sfo
,
parse_format
)
time_str
=
strftime
(
time_format
,
time_tuple
)
print
(
time_str
)
# Example 4
try
:
arrival_nyc
=
'2014-05-01 23:33:24 EDT'
time_tuple
=
strptime
(
arrival_nyc
,
time_format
)
except
:
logging
.
exception
(
'Expected'
)
else
:
assert
False
# Example 5
from
datetime
import
datetime
,
timezone
now
=
datetime
(
2014
,
8
,
10
,
18
,
18
,
30
)
now_utc
=
now
.
replace
(
tzinfo
=
timezone
.
utc
)
now_local
=
now_utc
.
astimezone
()
print
(
now_local
)
# Example 6
time_str
=
'2014-08-10 11:18:30'
now
=
datetime
.
strptime
(
time_str
,
time_format
)
time_tuple
=
now
.
timetuple
()
utc_now
=
mktime
(
time_tuple
)
print
(
utc_now
)
# Example 7
import
pytz
arrival_nyc
=
'2014-05-01 23:33:24'
nyc_dt_naive
=
datetime
.
strptime
(
arrival_nyc
,
time_format
)
eastern
=
pytz
.
timezone
(
'US/Eastern'
)
nyc_dt
=
eastern
.
localize
(
nyc_dt_naive
)
utc_dt
=
pytz
.
utc
.
normalize
(
nyc_dt
.
astimezone
(
pytz
.
utc
))
print
(
utc_dt
)
# Example 8
pacific
=
pytz
.
timezone
(
'US/Pacific'
)
sf_dt
=
pacific
.
normalize
(
utc_dt
.
astimezone
(
pacific
))
print
(
sf_dt
)
# Example 9
nepal
=
pytz
.
timezone
(
'Asia/Katmandu'
)
nepal_dt
=
nepal
.
normalize
(
utc_dt
.
astimezone
(
nepal
))
print
(
nepal_dt
)
Back
|
FazBrowse Home
|
New Git URL