Skip to content

Instantly share code, notes, and snippets.

@evaldosantos
Created October 25, 2017 20:25
Show Gist options
  • Save evaldosantos/57d6058054b087a7188f84314b6782c3 to your computer and use it in GitHub Desktop.
Save evaldosantos/57d6058054b087a7188f84314b6782c3 to your computer and use it in GitHub Desktop.
# working with date
>>> from datetime import datetime
>>> d = datetime.now()
>>> d = d.replace(hour=9,minute=0, second=0)
>>> e = datetime.now()
>>> time = e-s
>>> time.days
0
>>> time.seconds
8868
>>> time.microseconds
520171
>>> round(time.seconds / 3600)
2.0
#the way we manipulate datetime is using timedeltas
>>> from datetime import datetime
>>> from datetime import timedelta
>>> now = datetime.now()
>>> now + timedelta(days=3)
datetime.datetime(2017, 10, 26, 11, 33, 42, 657978)
>>> now + timedelta(days=-5)
datetime.datetime(2017, 10, 18, 11, 33, 42, 657978)
>>> now - timedelta(days=5)
datetime.datetime(2017, 10, 18, 11, 33, 42, 657978)
# what if I just want a date, or time
>>> now.date()
datetime.date(2017, 10, 23)
>>> now.time()
datetime.time(11, 33, 42, 657978)
# now x today
datetime.now() and datetime.today() do almost the same things, with one difference:
- datetime.now can take a timezone as argument
# also, we can combine a date and time to get a datetime object
_date = datetime.now().date()
_time = datetime.now().time()
datetime.combine(_date, _time)
# timezones
>>> import datetime
>>> eastern - datetime.timezone(datetime.timedelta(hours=-5))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'eastern' is not defined
>>> eastern = datetime.timezone(datetime.timedelta(hours=-5))
>>> pacific = datetime.timezone(datetime.timedelta(hours=-8))
>>> naive = datetime.datetime(2014, 4, 21, 9, 0)
>>> aware = datetime.datetime(2014, 4, 21, 9, 0, tzinfo=pacific)
>>> aware
datetime.datetime(2014, 4, 21, 9, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600)))
>>> aware.astimezone(eastern)
datetime.datetime(2014, 4, 21, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
>>> import pytz
>>> pacific = pytz.timezone('US/Pacific')
>>> eastern = pytz.timezone('US/Eastern')
>>> fmt = '%Y-%m%d %H:%M:%S %Z%z'
>>> utc = pytz.utc
>>> utc
<UTC>
>>> start = pacific.localize(datetime.datetime(2014, 4, 21, 9))
>>> start.strftime(fmt)
'2014-0421 09:00:00 PDT-0700'
>>> start_eastern = start.astimezone(eastern)
>>> start_eastern
datetime.datetime(2014, 4, 21, 12, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
>>> start
datetime.datetime(2014, 4, 21, 9, 0, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)
>>> start_utc = datetime.datetime(2014, 4, 21, 1, tzinfo=utc)
>>> start_utc.strftime(fmt)
'2014-0421 01:00:00 UTC+0000'
>>> start_pacific = start_utc.astimezone(pacific)
>>> start_pacific
datetime.datetime(2014, 4, 20, 18, 0, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)
>>> auckland = pytz.timezone('Pacific/Auckland')
>>> mumbai = pytz.timezone('Asia/Calcutta')
>>> apollo_13_naive = datetime.datetime(1970, 4, 11, 14, 13)
>>> apollo_13_eastern = eastern.localize(apollo_13_naive)
>>> apollo_13_eastern = eastern.localize(apollo_13_naive)
>>> pytz.all_timezones
>>> pytz.country_timezones['br']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment