Why gmt and utc results are different when running following code.
import time
import datetime
print( time.mktime(time.gmtime()))
print(datetime.datetime.utcnow().strftime("%s"))
Output:
1571984607.0
1571981007
I read many articles gmt and utc both will give same result. Why I am getting different result here.
gmt is 1 hour advance in time module when I converted it to IST.
I wrote this python code but I am facing problems to make it work properly at the level of time zone. So basically the datetime.now() is not working as expected as it is not compliant to my local time (Paris local time) . Any idea how to sovle this please.
from datetime import datetime, timedelta
from airflow.operators import
GoogleCloudStorageToGoogleCloudStorageOperator
copy_trm = GoogleCloudStorageToGoogleCloudStorageOperator(
task_id='copy_file',
source_bucket="source",
source_object="input-files/*.orc",
destination_bucket="destination",
destination_object="output-files/recent",
last_modified_time=datetime.now() - timedelta(days=1)
Best Regards
use pytz module
import pytz
tz = pytz.timezone('Pacific/Johnston') #change it to your suitable timezone
ct = datetime.now(tz=tz)
If you want to know your current timezone name. It is possible with below code:
import datetime
dt = datetime.datetime.now()
dt.astimezone().tzname()
Output:
'IST'
You can send time in UTC format as:
dt.astimezone().utcnow()
With the standard library, requires the numerical offset
If you have the numerical offset of your timezone (compared to UTC) then you use the standard library:
>>> from datetime import datetime, timezone
>>> datetime.now().strftime("%c (%Z)")
'Wed Feb 13 23:00:06 2019 ()'
>>> datetime.now(tz=timezone.utc).strftime("%c (%Z)")
'Wed Feb 13 22:00:11 2019 (UTC)'
>>> datetime.now(tz=timezone(timedelta(hours=2))).strftime("%c (%Z)")
'Thu Feb 14 00:00:20 2019 (UTC+02:00)'
Though since the offset can change throughout the year (e.g. with daylight saving time), it is recommended to use a named timezone.
With dateutils, using the host timezones database
The Python documentation on timezone points to the dateutil.tz package which will use the timezone database from the machine it runs on. The pytz package on the other hand comes with a bundled timezone database.
I would personally recommend the dateutil package since the database already exists on the running machine. As long as the machine is kept up-to-date (as any server and PC should) you are good to go.
>>> from dateutil.tz import gettz # GNU/Linux and macOS
>>> # Or, on Windows: from dateutil.tz.win import gettz
>>> datetime.now(tz=gettz("Europe/Paris")).strftime("%c (%Z)")
'Wed Feb 13 23:00:58 2019 (CET)'
>>> datetime.now(tz=gettz("Asia/Taipei")).strftime("%c (%Z)")
'Thu Feb 14 06:01:27 2019 (CST)'
I have a date here which is Fri Jun 19 02:27:25 PDT 2015 which I get from the DB and I am trying to convert it from PDT to UTC
For which first I am converting it to a datetime object like this:
date_time = datetime.datetime.strptime(date, '%a %b %d %H:%M:%S %Z %Y');
When I run the python file directly, it works, but when this code executes thru the Django framework, I get this error.
ValueError: time data 'Fri Jun 19 02:27:25 PDT 2015' does not match format '%a %b %d %H:%M:%S %Z %Y'
I have a feeling this is because of the timezone, because I have many more date formats which don’t contain timezone and conversion for them works fine. Could you suggest a workaround for this.
This error raised because datetime module not recognize all time-zones, use dateutil module instead of datetime similar below:
from dateutil.parser import parse
parse('Fri Jun 19 02:27:25 PDT 2015')
I download RSS content from different countries with Python, but each of them use their own datetime format or time zone. For instance,
Wed, 23 Oct 2013 17:44:13 GMT
23 Oct 2013 18:21:04 +0100
23 Oct 2013 13:12:41 EDT
10-23-2013 00:12:24
At the moment, my solution is to create a different function for each RSS source and change the date to a format I will decide. But is there any way to do this automatically?
Not really. But take a look at the feedparser lib.
Different feed types and versions use wildly different date formats.
Universal Feed Parser will attempt to auto-detect the date format used
in any date element, and parse it into a standard Python 9-tuple, as
documented in the Python time module.
From the list of Recognized Date Formats it seems to me, that the library could help you out some of the way :)
Best of luck
You can try using the dateutil module to parse the datetime.
It povides the functionality to parse most of the known datetime format. Here is an example from the docs:
>>> from dateutil.parser import *
>>> parse("Thu Sep 25 10:36:28 2003")
datetime.datetime(2003, 9, 25, 10, 36, 28)
It returns a datetime object which can be directly used for manipulation. You can then also use strftime to convert it to the required format string.
I have a date of the form specified by RFC 2822 -- say Fri, 15 May 2009 17:58:28 +0000, as a string. Is there a quick and/or standard way to get it as a datetime object in Python 2.5? I tried to produce a strptime format string, but the +0000 timezone specifier confuses the parser.
The problem is that parsedate will ignore the offset.
Do this instead:
from email.utils import parsedate_tz
print parsedate_tz('Fri, 15 May 2009 17:58:28 +0700')
I'd like to elaborate on previous answers. email.utils.parsedate and email.utils.parsedate_tz both return tuples, since the OP needs a datetime.datetime object, I'm adding these examples for completeness:
from email.utils import parsedate
from datetime import datetime
import time
t = parsedate('Sun, 14 Jul 2013 20:14:30 -0000')
d1 = datetime.fromtimestamp(time.mktime(t))
Or:
d2 = datetime.datetime(*t[:6])
Note that d1 and d2 are both naive datetime objects, there's no timezone information stored. If you need aware datetime objects, check the tzinfo datetime() arg.
Alternatively you could use the dateutil module
from email.utils import parsedate
print parsedate('Fri, 15 May 2009 17:58:28 +0000')
Documentation.
It looks like Python 3.3 going forward has a new method parsedate_to_datetime in email.utils that takes care of the intermediate steps:
email.utils.parsedate_to_datetime(date)
The inverse of format_datetime(). Performs the same function as parsedate(), but on
success returns a datetime. If the input date has a timezone of -0000,
the datetime will be a naive datetime, and if the date is conforming
to the RFCs it will represent a time in UTC but with no indication of
the actual source timezone of the message the date comes from. If the
input date has any other valid timezone offset, the datetime will be
an aware datetime with the corresponding a timezone tzinfo.
New in version 3.3.
http://python.readthedocs.org/en/latest/library/email.util.html#email.utils.parsedate_to_datetime
There is a parsedate function in email.util.
It parses all valid RFC 2822 dates and some special cases.
email.utils.parsedate_tz(date) is the function to use. Following are some variations.
Email date/time string (RFC 5322, RFC 2822, RFC 1123) to unix timestamp in float seconds:
import email.utils
import calendar
def email_time_to_timestamp(s):
tt = email.utils.parsedate_tz(s)
if tt is None: return None
return calendar.timegm(tt) - tt[9]
import time
print(time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(email_time_to_timestamp("Wed, 04 Jan 2017 09:55:45 -0800"))))
# 2017-01-04T17:55:45Z
Make sure you do not use mktime (which interprets the time_struct in your computer’s local time, not UTC); use timegm or mktime_tz instead (but beware caveat for mktime_tz in the next paragraph).
If you are sure that you have python version 2.7.4, 3.2.4, 3.3, or newer, then you can use email.utils.mktime_tz(tt) instead of calendar.timegm(tt) - tt[9]. Before that, mktime_tz gave incorrect times when invoked during the local time zone’s fall daylight savings transition (bug 14653).
Thanks to #j-f-sebastian for caveats about mktime and mktime_tz.
Email date/time string (RFC 5322, RFC 2822, RFC 1123) to “aware” datetime on python 3.3:
On python 3.3 and above, use email.utils.parsedate_to_datetime, which returns an aware datetime with the original zone offset:
import email.utils
email.utils.parsedate_to_datetime(s)
print(email.utils.parsedate_to_datetime("Wed, 04 Jan 2017 09:55:45 -0800").isoformat())
# 2017-01-04T09:55:45-08:00
Caveat: this will throw ValueError if the time falls on a leap second e.g. email.utils.parsedate_to_datetime("Sat, 31 Dec 2016 15:59:60 -0800").
Email date/time string (RFC 5322, RFC 2822, RFC 1123) to “aware” datetime in UTC zone:
This just converts to timestamp and then to UTC datetime:
import email.utils
import calendar
import datetime
def email_time_to_utc_datetime(s):
tt = email.utils.parsedate_tz(s)
if tt is None: return None
timestamp = calendar.timegm(tt) - tt[9]
return datetime.datetime.utcfromtimestamp(timestamp)
print(email_time_to_utc_datetime("Wed, 04 Jan 2017 09:55:45 -0800").isoformat())
# 2017-01-04T17:55:45
Email date/time string (RFC 5322, RFC 2822, RFC 1123) to python “aware” datetime with original offset:
Prior to python 3.2, python did not come with tzinfo implementations, so here an example using dateutil.tz.tzoffset (pip install dateutil):
import email.utils
import datetime
import dateutil.tz
def email_time_to_datetime(s):
tt = email.utils.parsedate_tz(s)
if tt is None: return None
tz = dateutil.tz.tzoffset("UTC%+02d%02d"%(tt[9]//60//60, tt[9]//60%60), tt[9])
return datetime.datetime(*tt[:5]+(min(tt[5], 59),), tzinfo=tz)
print(email_time_to_datetime("Wed, 04 Jan 2017 09:55:45 -0800").isoformat())
# 2017-01-04T09:55:45-08:00
If you are using python 3.2, you can use the builtin tzinfo implementation datetime.timezone: tz = datetime.timezone(datetime.timedelta(seconds=tt[9])) instead of the third-party dateutil.tz.tzoffset.
Thanks to #j-f-sebastian again for note on clamping the leap second.