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/
>