lkiesow / python-feedgen

Python module to generate ATOM feeds, RSS feeds and Podcasts.
https://feedgen.kiesow.be/
BSD 2-Clause "Simplified" License
723 stars 123 forks source link

Is it possible to set custom timezone? #40

Closed MasterGroosha closed 8 years ago

MasterGroosha commented 8 years ago

Hello, is it possible to config feedgen so it will set time with different timezone? F.ex. I need time to be formatted as GMT+3 Or could you please give an example, how to properly set published argument for entry object?

gkiko commented 8 years ago

I believe that it's not feedgen's job to provide date/time utilities.

This is my code snippet to set current time to feed entry

fe.updated(datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%dT%H:%M:%SZ'))

the result is

<updated>2016-03-27T03:12:08+00:00</updated>

You can easily apply timezone conversion before setting the value in a feed entry

lkiesow commented 8 years ago

You can also just pass a datetime object with a specific timezone like this:

>>> from feedgen.feed import FeedGenerator
>>> from datetime import datetime
>>> from dateutil import tz
>>> fg = FeedGenerator()
>>> fg.id('http://lernfunk.de/media/654321')
'http://lernfunk.de/media/654321'
>>> fg.title('Some Testfeed')
'Some Testfeed'
>>> fg.updated(datetime.now(tz.tzlocal()))
datetime.datetime(2016, 9, 4, 20, 8, 7, 820300, tzinfo=tzlocal())
>>> print(fg.atom_str(pretty=True))
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://lernfunk.de/media/654321</id>
  <title>Some Testfeed</title>
  <updated>2016-09-04T20:08:07.820300+02:00</updated>
  <generator version="0.3.2">python-feedgen</generator>
</feed>

As you can see, feedgen will preserve the timezone you set. In this case UTC+2.