The right time of day with the right time zone

I use feedparser to get RSS feeds. Here is my code:

 >>> import datetime >>> import time >>> import feedparser >>> d=feedparser.parse("http://.../rss.xml") >>> datetimee_rss = d.entries[0].published_parsed >>> datetimee_rss time.struct_time(tm_year=2015, tm_mon=5, tm_mday=8, tm_hour=16, tm_min=57, tm_sec=39, tm_wday=4, tm_yday=128, tm_isdst=0) >>> datetime.datetime.fromtimestamp(time.mktime(datetimee_rss)) datetime.datetime(2015, 5, 8, 17, 57, 39) 

In my time zone (FR), the actual date is May, 8th, 2015 18:57 .

In RSS XML format, the value is <pubDate>Fri, 08 May 2015 18:57:39 +0200</pubDate>

When I parse it into datetime, I got 2015, 5, 8, 17, 57, 39 .

How to have 2015, 5, 8, 18, 57, 39 without dirty hacking, but simply by setting the correct time zone?

EDIT:

Performing:

 >>> from pytz import timezone >>> datetime.datetime.fromtimestamp(time.mktime(datetimee_rss),tz=timezone('Euro pe/Paris')) datetime.datetime(2015, 5, 8, 17, 57, 39, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>) 

I have something nicer, however it doesn't seem to work in the rest of the script, I got a lot of TypeError: can't compare offset-naive and offset-aware datetimes errors TypeError: can't compare offset-naive and offset-aware datetimes .

+3
source share
3 answers

feedparser provides the source datetime string (just remove the _parsed suffix from the attribute name), so if you know the format of the string, you can analyze it yourself in a datetime object that supports tz.

For example, with your code, you can get a tz-aware object as such:

 datetime.datetime.strptime(d.entries[0].published, '%a, %d %b %Y %H:%M:%S %z') 

for more information on strptime() , see https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

EDIT: since Python 2.x does not support the %z directive, use python-dateutil instead

 pip install python-dateutil 

then

 from dateutil import parser datetime_rss = parser.parse(d.entries[0].published) 

at https://dateutil.readthedocs.org/en/latest/

+1
source

feedparser returns time in the UTC time zone. It is wrong to apply time.mktime() to it (if your time zone is not UTC, it is not). Instead, use calendar.timegm() :

 import calendar from datetime import datetime utc_tuple = d.entries[0].published_parsed posix_timestamp = calendar.timegm(utc_tuple) local_time_as_naive_datetime_object = datetime.frometimestamp(posix_timestamp) # assume non-"right" timezone 

RSS feeds can use different date formats ; I would leave the date parsing to feedparser .

If you want local time to be known as a datetime object:

 from tzlocal import get_localzone # $ pip install tzlocal local_timezone = get_localzone() local_time = datetime.frometimestamp(posix_timestamp, local_timezone) # assume non-"right" timezone 
+1
source

Try the following:

 >>> import os >>> os.environ['TZ'] = 'Europe/Paris' >>> time.tzset() >>> time.tzname ('CET', 'CEST') 
0
source

Source: https://habr.com/ru/post/1238169/


All Articles