5

In a Python application I need to know the current day. datetime.date.today() works well. The problem is that when I deploy the program to my server located in the USA, my user base (that is Italian) is presented a date that may not be the correct one (since the server is 6 time zones away).

How can I construct a date object in python according to a specific time zone? Thanks

1
  • pytz is your friend. Commented Feb 15, 2012 at 18:54

3 Answers 3

5

Normally, a server application will do all its time storage and calculations in UTC. Times would also be transmitted to the client in UTC. Then, you do the conversion to local time at the client side (in Javascript, if it's a web application). It's very difficult for a server to get the time zone right for every client, because the server simply doesn't have all the information.

Even if your application is targeted specifically at Italian users, they have been known to travel to other time zones.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 It's a much better idea to use a good standard (like UTC) and convert it client-side.
4

Everyone agrees that UTC is wonderful but that doesn't help much if datetime.date.today() is being run in the US without a timezone. If the user base is in Italy and that's the date you want to show...

import datetime import pytz IT = pytz.timezone('Europe/Rome') oggi = datetime.now(IT).date() 

Comments

-2

access the class, create an instance, set your own time, access that object everytime you want the time.

2 Comments

I have done it this way before and it works! It is cool because you can set anytime you want and the program will keep track of it. I suppse pytz works too.
I don't understand what you are saying. Can you elaborate?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.