Created
April 26, 2018 14:04
-
-
Save BbsonLin/dd124cd9a65bcd633c392f31bf73e06f to your computer and use it in GitHub Desktop.
How time transfer is always being annoying, so I make this gist for record~
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from datetime import datetime, timedelta | |
print('------------------------------------') | |
# Get now timestamp & datetime | |
now_dt = datetime.today() | |
print('Now datetime is:', now_dt) | |
now_ts = time.time() | |
print('Now timestamp is:', now_ts) | |
print('------------------------------------') | |
# Get today's midnight datetime (put zero to hour/minute/second params) | |
midnight_dt = datetime(now_dt.year, now_dt.month, now_dt.day, 0, 0, 0) | |
print('Today\'s midnight datetime is:', midnight_dt) | |
# Get today's midnight timestamp (datetime -> timestamp) | |
midnight_ts = time.mktime(midnight_dt.timetuple()) | |
print('Today\'s midnight datetime is:', midnight_ts) | |
print('------------------------------------') | |
# Get one-day timedelta | |
one_day_td = timedelta(days=1) | |
print('One day timedelta is:', one_day_td) | |
print('------------------------------------') | |
# Get tomorrow's midnight datetime | |
next_midnight_dt = midnight_dt + one_day_td | |
print('Tomorrow\'s midnight datetime is:', midnight_dt) | |
# Get tomorrow's midnight timestamp | |
next_midnight_ts = time.mktime(next_midnight_dt.timetuple()) | |
print('Tomorrow\'s midnight timestamp is:', next_midnight_ts) | |
print('Timestamp\'s delta: ', next_midnight_ts - midnight_ts) | |
print('------------------------------------') | |
print('\nAnother way to get tomorrow\'s midnight timestamp ...') | |
# Add 86400.0 seconds to timestamp | |
next_midnight_ts = midnight_ts + 86400.0 | |
print('Tomorrow\'s midnight timestamp is:', next_midnight_ts) | |
# timestamp -> datetime | |
next_midnight_dt = datetime.fromtimestamp(next_midnight_ts) | |
print('Tomorrow\'s midnight datetime is:', midnight_dt) | |
""" | |
# Result: | |
$ python python3_time_snippet.py | |
------------------------------------ | |
Now datetime is: 2018-04-26 22:00:56.985617 | |
Now timestamp is: 1524751256.9856498 | |
------------------------------------ | |
Today's midnight datetime is: 2018-04-26 00:00:00 | |
Today's midnight datetime is: 1524672000.0 | |
------------------------------------ | |
One day timedelta is: 1 day, 0:00:00 | |
------------------------------------ | |
Tomorrow's midnight datetime is: 2018-04-26 00:00:00 | |
Tomorrow's midnight timestamp is: 1524758400.0 | |
Timestamp's delta 86400.0 | |
------------------------------------ | |
Another way to get tomorrow's midnight timestamp ... | |
Tomorrow's midnight timestamp is: 1524758400.0 | |
Tomorrow's midnight datetime is: 2018-04-26 00:00:00 | |
------------------------------------ | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment