librelist archives

« back to archive

jinja question about timezone

jinja question about timezone

From:
Andreas Christoffersen
Date:
2012-07-30 @ 13:54
Hi Guys,

I'll jump right in:

in my app I define the local time zone:

dktid = pytz.timezone("Europe/Copenhagen")


In the function that calls test.html I return the timezone to jinja2 (with
a bunch of other variables).

return dktid = dktid


In the test.html template I have:

<span class=time>{{ i['time'].astimezone(dktid).strftime('%d. %B %Y, kl.
%H:%M') }}</span>$


i is an item from a mongodb cursorobject I am iterating over (so it would
be impractical to define the time string in the app).  The monog
connections is set to tz_aware = True.

It works without the astimezone(dktid) - but not as above? Whats wrong here?

(complete-n00b-code is on bitbucket:
https://bitbucket.org/achristoffersen/politikere-i-medierne )

Thanks in advance

Re: [flask] jinja question about timezone

From:
Audrius Kažukauskas
Date:
2012-07-30 @ 20:27
On Mon, 2012-07-30 at 15:54:21 +0200, Andreas Christoffersen wrote:
> in my app I define the local time zone:
> 
> dktid = pytz.timezone("Europe/Copenhagen")
> 
> 
> In the function that calls test.html I return the timezone to jinja2 (with
> a bunch of other variables).
> 
> return dktid = dktid
> 
> 
> In the test.html template I have:
> 
> <span class=time>{{ i['time'].astimezone(dktid).strftime('%d. %B %Y, kl.
> %H:%M') }}</span>$
> 
> 
> i is an item from a mongodb cursorobject I am iterating over (so it would
> be impractical to define the time string in the app).  The monog
> connections is set to tz_aware = True.
> 
> It works without the astimezone(dktid) - but not as above? Whats wrong here?

What is the timezone of datetime objects returned by MongoDB?  They
should either be in UTC or naive objects without any timezone at all.

According to pytz docs, the preferred way is to use UTC timezone
internally and convert to localtime only when generating output meant
for humans.

  >>> localtime = pytz.timezone('Europe/Copenhagen')
  >>> datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(localtime)

or

  >>> datetime(2012, 1, 1, tzinfo=pytz.utc).astimezone(localtime)

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] jinja question about timezone

From:
Andreas Christoffersen
Date:
2012-07-31 @ 05:29
Hi Audrius,

this is what I have:

dktid = pytz.timezone("Europe/Copenhagen")

# database configuration

MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017


# Flask start

app = Flask(__name__)
app.config.from_object(__name__)


# connect to the database

# tz_aware = true cf.

http://api.mongodb.org/python/2.0/faq.html#what-is-the-correct-way-to-handle-time-zones-with-pymongo
connection = Connection(app.config['MONGODB_HOST'],
                    app.config['MONGODB_PORT'], tz_aware= True)



This works (stamp_time is returned directly in the jinja template {{
stamp_time }} not through mongo)

now = datetime.utcnow()
now = now.replace(tzinfo=pytz.utc)
stamp_time = now.astimezone(dktid).strftime('%d. %B %Y, kl. %H:%M'),


So my problem is how to handle this when iterating over the mongodb object
in the jinja template.

While this works:

{{ i['time'].strftime('%d. %B %Y, kl. %H:%M') }}


This doesn't:

{{ i['time'].astimezone(dktid).strftime('%d. %B %Y, kl. %H:%M') }}


I hope this clears it up.



>
> What is the timezone of datetime objects returned by MongoDB?  They
> should either be in UTC or naive objects without any timezone at all.
>
> According to pytz docs, the preferred way is to use UTC timezone
> internally and convert to localtime only when generating output meant
> for humans.
>
>   >>> localtime = pytz.timezone('Europe/Copenhagen')
>   >>> datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(localtime)
>
> or
>
>   >>> datetime(2012, 1, 1, tzinfo=pytz.utc).astimezone(localtime)
>
> --
> Audrius Kažukauskas
> http://neutrino.lt/
>

Re: [flask] jinja question about timezone

From:
Audrius Kažukauskas
Date:
2012-07-31 @ 06:26
On Tue, 2012-07-31 at 07:29:04 +0200, Andreas Christoffersen wrote:
> # tz_aware = true cf.
> 
http://api.mongodb.org/python/2.0/faq.html#what-is-the-correct-way-to-handle-time-zones-with-pymongo
> connection = Connection(app.config['MONGODB_HOST'],
>                     app.config['MONGODB_PORT'], tz_aware= True)

If I understood it correctly, this configuration is OK (returned
datetime objects use UTC timezone).

> This works (stamp_time is returned directly in the jinja template {{
> stamp_time }} not through mongo)
> 
> now = datetime.utcnow()
> now = now.replace(tzinfo=pytz.utc)
> stamp_time = now.astimezone(dktid).strftime('%d. %B %Y, kl. %H:%M'),
> 
> 
> So my problem is how to handle this when iterating over the mongodb object
> in the jinja template.
> 
> While this works:
> 
> {{ i['time'].strftime('%d. %B %Y, kl. %H:%M') }}
> 
> This doesn't:
> 
> {{ i['time'].astimezone(dktid).strftime('%d. %B %Y, kl. %H:%M') }}

When you say it doesn't work, what exactly is happening?  Do you get an
error?

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] jinja question about timezone

From:
Andreas Christoffersen
Date:
2012-07-31 @ 07:21
Just ran the code to reproduce the error message. Amazing - it seems to
works now! - sorry for the trouble caused. I am embarrased. Don't know what
I did wrong the last time.


> When you say it doesn't work, what exactly is happening?  Do you get an
> error?
>
> --
> Audrius Kažukauskas
> http://neutrino.lt/
>