Convert GMT time to Local time in python - python

I have a date(datetime) which is stored in the database as GMT time.
I need to convert this to local time.How can i achieve this using python
Please have your suggestions
Thanks in Advance!!

Have you read the Python documentation, 8.1 datetime — Basic date and time types
and especially 8.3 tzinfo objects?
They describe the library function datetime.astimezone(tz=None), which may be exactly what you want.
Return a datetime object with new tzinfo attribute tz, adjusting the date and time data so the result is the same UTC time as self, but in tz‘s local time.
They have example code there for defining a time zone in terms of an offset from UTC, ending with a declaration like:
Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")
Having read that, what is your question?
Do you have a date or a datetime? i.e. does the data have a time zone component? If no time component, then what is the meaning of changing the date to a different time zone?
Do you have a 'naive' or 'aware' datetime? From the docs,
An aware object has sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used to represent a specific moment in time that is not open to interpretation....
A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.
The Python documentation is really quite good. Perhaps it will answer your questions without you having to ask StackOverflow.

Related

Using datetime, latitude and longitude need of offset with/without DST using Zdump IANA

there are a lot of questions regarding this timezone, I am in need to get the offset such as +5.5, +6.0, etc., using the time, latitude, and longitude, I want to use the zdump of IANA databases, with the script of python, don't want to use the pytz or timezone finder modules. Just want to parse the time, lat and lon then get the offset with/without DST. I have extracted the tz2020a databases.
Sorry, but what you ask is not possible because the IANA time zone database does not include geographic boundaries of the time zones.
The only location data in the IANA time zone database are the coordinates of the reference locations in zone.tab and zone1970.tab. However, this tells you nothing about the boundaries for the zone. One cannot just pick the closest city, as illustrated in the graphic towards the end of this answer.
The correct solution is to use a library that provides geographic boundaries of time zones. For Python, the best option is currently timezonefinder.
Once you have identified the time zone, then you could use zdump for that zone to get the raw offset information. However, you'll still have some challenges in figuring out which offset or sets of offsets to use. Not all time zones are as simple as having a single standard offset or a standard offset + daylight saving time. Many have changed standard offsets multiple times throughout their history - so you'll still need some reference to "now" (ish).
Libraries like pytz, dateutil, or arrow make that part much easier, by exposing APIs that interpret the data correctly for you. If you don't want to use a library for this - you can use the built-in time zone support from PEP 615 in the upcoming Python 3.9 release.

How to find out if DST was effective for some date in Python?

I have following data: geographical coordinates of the place and local date and time, not necessarily current, may point to some moment many years ago. I also may have non-DST time zone effective currently, at present time (but not necessarily correct for the date and time of interest).
I want to find out whether DST was in effect in that location at that local time using Python. Any ideas how to do that? I was reading about package pytz, but could not find anything suitable in it to complete that.
You'll need one of the solutions in How to get a time zone from a location using latitude and longitude coordinates? to identify the timezone that the given coordinates belongs to.
Once you have the timezone you can then use the solution at Is a specific timezone using DST right now? to determine if DST is effective given the timezone and the date/time.
I'm assuming you've already resolved the time zone itself - if not, there are really two problems in this one question:
Finding a time zone ID from coordinates
Determining if DST was in effect in a time zone at an arbitrary date/time
I'm not very familiar with Python, but given this documentation I believe you want the tzinfo.dst(dt) function, which will return timedelta(0) in standard time, and the difference between the standard offset and the wall offset in DST. You pass in the datetime you want to find the offset for.
Note that in the pytz library, there's an additional is_dst parameter which you can specify to resolve ambiguities, where a given local time occurs twice due to a "fall back" transition.

Python - Convert UTC datetime.timetz() to local time

Having a hard time searching for this since it's mostly datetime or similar-but-different results and still seeming to struggle with getting suggestions for slightly different problems to work with this one.
I have some UTC datetime object: timestamp. What I'm doing is dealing with many timestamps and corresponding values and bucketing them hour by hour, so I'm bucketing them by timestamp.timetz().
Now I want to loop through these buckets, take this UTC time object, and convert it to Eastern time, e.g. I want 00:00 to become 20:00, keeping daylight savings in mind.
How do I accomplish this?
You can use the "pytz" package to get this done. Below is a example to get current time using pytz in eastern timezone.
import pytz
from datetime import datetime
est_tz = pytz.timezone('US/Eastern')
datetime.now(est_tz)
Hope it helps.
Happy Coding !! :)

Datetime iteration for KML placemarks

All,
I am writing an app that produces 64800 polygons and I want each of them to have a unique time stamp. The problem is that my script runs in about 3 seconds which means that there are exactly 3 unique dates spread across all 64k polygons. I've tried going back in time using timedelta() which does actually go back in time from utcnow() but the problem is that it still iterates for 3 seconds with the 3 unique date stamps. I have been working on variations of this...
def time():
now = datetime.datetime.utcnow()
start_day = datetime.timedelta(days=-2000)
timer = now + start_day
Can anyone lend a hand to get me to the 64800 unique time stamps?
Thanks,
Adam
You could add a polygon counter to the generated timestamp to differentiate each polygon from the previous one. As a matter of fact, if you do not need accurate wall-clock timestamps - and your messing with the timestamps with timedelta() indicates that you do not - you could even use an abritrary counter as your clock, as long as it's atomically increased.
If you do need accurate timestamps, you could try for sub-second resolution, as mentioned here. Keep in mind, however, that many systems will only provide timestamps in increments of several milli-seconds, which is still plenty of time for more than one polygon to be created, so you will still have to use an additional counter or something similar to create unique timestamps.
We might be able to help you more if you mentioned what these timestamps will be used for...

How to handle time zones in a CMS?

I'm storing MySQL DateTimes in UTC, and let the user select their time zone, storing that information.
However, I want to to some queries that uses group by a date. Is it better to store that datetime information in UTC (and do the calculation every time) or is it better to save it in the timezone given? Since time zones for users can change, I wonder.
Thanks
Generally always store in UTC and convert for display, it's the only sane way to do time differences etc. Or when somebody next year decides to change the summer time dates.
It's almost always better to save the time information in UTC, and convert it to local time when needed for presentation and display.
Otherwise, you will go stark raving mad trying to manipulate and compare dates and times in your system because you will have to convert each time to UTC time for comparison and manipulation.

Categories