What is the module/method used to get the current time?
>>> import datetime >>> datetime.datetime.now() datetime.datetime(2009, 1, 6, 15, 8, 24, 78915) >>> print(datetime.datetime.now()) 2009-01-06 15:08:24.789150
And just the time:
>>> datetime.datetime.now().time() datetime.time(15, 8, 24, 78915) >>> print(datetime.datetime.now().time()) 15:08:24.789150
See the documentation for more information.
To save typing, you can import the
datetime object from the
>>> from datetime import datetime
Then remove the leading
datetime. from all of the above.
New in version 2.6.
For a naive object, the %z and %Z format codes are replaced by empty strings.
It looks like this is implemented only in >= 2.6, and I think you have to manually parse it.
I can't see another solution than to remove the time zone data:
from datetime import timedelta,datetime try: offset = int("Tue, 22 Jul 2008 08:17:41 +0300"[-5:]) except: print "Error" delta = timedelta(hours = offset / 100) fmt = "%a, %d %b %Y %H:%M:%S" time = datetime.strptime("Tue, 22 Jul 2008 08:17:41 +0200"[:-6], fmt) time -= delta
The short answer: you can't.
The long answer: There is no such thing as "proper time zone for a locale". That's just because there are a few countries that have more than one time zone (for example United States). Time zone is a different concept.
Anyway, you are looking to solve your problem. I am guessing that you are writing a web application and you see that the time zone is reverting to the server default. That's a typical situation. Both
TimeZone.getDefault() will return server-related information. The JVM has no way of knowing the "proper" time zone. So what can you do about it?
- You can add time zone information to user profile (if you have one), or create a time zone combo box (so that the user can switch at runtime). Then you can assign an appropriate object to
DateFormatinstance and it will convert time zone automatically.
- You can send the unformatted time to the client (for example written as ISO 8601 date-time format or as a Unix time of the epoch in relation to UTC) and have Globalize or Dojo format date and time for you.
Out of these three possible choices, I always opt for number 1. By putting time zone information to user profile, you know for sure what his/her preferred time zone is, regardless of their current web browser, etc. Please keep in mind that some users might want to use your application while visiting other countries...
There is a macro on that page with a LocalTimeToUTC method. Looks like it would do the trick. Also some formula examples if you wanted to go that route.
Edit - Another link. http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx This page has several methods for date/time. Pick your poison. Either should do the trick, but I feel like the second is prettier. ;)
use the following to convert to a timestamp in python 2