utc

How to set timestamps on GMT/UTC on Python logging?

How to set timestamps on GMT/UTC on Python logging? Question: Is it possible and how to set the logging timezone to GMT? (i.e. the %(asctime)s parameter in the format) Asked By: Jonathan Livni || Source Answers: logging.Formatter.converter = time.gmtime (documented in the docstring of logging.Formatter.formatTime) Answered By: Sven Marnach Just setting logging.Formatter.converter = time.gmtime is …

Total answers: 6

get UTC timestamp in python with datetime

get UTC timestamp in python with datetime Question: Is there a way to get the UTC timestamp by specifying the date? What I would expect: datetime(2008, 1, 1, 0, 0, 0, 0) should result in 1199145600 Creating a naive datetime object means that there is no time zone information. If I look at the documentation …

Total answers: 10

Convert UTC datetime string to local datetime

Convert UTC datetime string to local datetime Question: I’ve never had to convert time to and from UTC. Recently had a request to have my app be timezone aware, and I’ve been running myself in circles. Lots of information on converting local time to UTC, which I found fairly elementary (maybe I’m doing that wrong …

Total answers: 16

How can I get the current time (now) in UTC?

How can I get the current time (now) in UTC? Question: I have a python datetime object (representing five minutes from now) which I would like to convert to UTC. I am planning to output it in RFC 2822 format to put in an HTTP header, but I am not sure if that matters for …

Total answers: 9

Getting computer's UTC offset in Python

Getting computer's UTC offset in Python Question: In Python, how do you find what UTC time offset the computer is set to? Asked By: Paul || Source Answers: gmtime() will return the UTC time and localtime() will return the local time so subtracting the two should give you the utc offset. From https://pubs.opengroup.org/onlinepubs/009695399/functions/gmtime.html The gmtime() …

Total answers: 9

Python: Figure out local timezone

Python: Figure out local timezone Question: I want to compare UTC timestamps from a log file with local timestamps. When creating the local datetime object, I use something like: >>> local_time=datetime.datetime(2010, 4, 27, 12, 0, 0, 0, tzinfo=pytz.timezone(‘Israel’)) I want to find an automatic tool that would replace thetzinfo=pytz.timezone(‘Israel’) with the current local time zone. …

Total answers: 19

Convert to UTC Timestamp

Convert to UTC Timestamp Question: # parses some string into that format. datetime1 = datetime.strptime(somestring, "%Y-%m-%dT%H:%M:%S") # gets the seconds from the above date. timestamp1 = time.mktime(datetime1.timetuple()) # adds milliseconds to the above seconds. timeInMillis = int(timestamp1) * 1000 How do I (at any point in that code) turn the date into UTC format? I’ve …

Total answers: 4

pytz localize vs datetime replace

pytz localize vs datetime replace Question: I’m having some weird issues with pytz’s .localize() function. Sometimes it wouldn’t make adjustments to the localized datetime: .localize behaviour: >>> tz <DstTzInfo ‘Africa/Abidjan’ LMT-1 day, 23:44:00 STD> >>> d datetime.datetime(2009, 9, 2, 14, 45, 42, 91421) >>> tz.localize(d) datetime.datetime(2009, 9, 2, 14, 45, 42, 91421, tzinfo=<DstTzInfo ‘Africa/Abidjan’ GMT0:00:00 …

Total answers: 4

pytz utc conversion

pytz utc conversion Question: What is the right way to convert a naive time and a tzinfo into an UTC time? Say I have: d = datetime(2009, 8, 31, 22, 30, 30) tz = timezone(‘US/Pacific’) First way, pytz inspired: d_tz = tz.normalize(tz.localize(d)) utc = pytz.timezone(‘UTC’) d_utc = d_tz.astimezone(utc) Second way, from UTCDateTimeField def utc_from_localtime(dt, tz): …

Total answers: 4

How do I get the UTC time of "midnight" for a given timezone?

How do I get the UTC time of "midnight" for a given timezone? Question: The best I can come up with for now is this monstrosity: >>> datetime.utcnow() … .replace(tzinfo=pytz.UTC) … .astimezone(pytz.timezone(“Australia/Melbourne”)) … .replace(hour=0,minute=0,second=0,microsecond=0) … .astimezone(pytz.UTC) … .replace(tzinfo=None) datetime.datetime(2008, 12, 16, 13, 0) I.e., in English, get the current time (in UTC), convert it to …

Total answers: 6