Python pytz

From wikinotes
Revision as of 19:14, 6 May 2018 by Will (talk | contribs) (Created page with "<source lang="python"> def validate_datetime( mydt ): if mydt.tzinfo != pytz.utc: raise TypeError('Expected a UTC datetime object with tzinfo set to UTC') </source> <sour...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
def validate_datetime( mydt ):
	if mydt.tzinfo != pytz.utc:
		raise TypeError('Expected a UTC datetime object with tzinfo set to UTC')


## get utcnow localized to utc
dt = datetime.datetime.now( tz=pytz.utc )
dt = datetime.datetime.utcnow().replace( tzinfo=pytz.utc )

## display timezone in another timezone
dt.astimezone( pytz.utc )
dt.astimezone( pytz.timezone('US/Eastern') )

## display current timezone info
dt.tzinfo

## get current timezone (according to computer)
cur_tz = tzlocal.get_localzone()