How to get current offset of any timezone in Python - python

I need to get the current offset on any timezone in Python. At the moment I am using the pytz library and I am getting the offset with this code:
import datetime
import pytz
timezone = 'America/Punta_Arenas'
datetime.datetime.now(pytz.timezone(timezone)).utcoffset().total_seconds()/60/60
//prints -3
timezone = 'America/Santiago'
datetime.datetime.now(pytz.timezone(timezone)).utcoffset().total_seconds()/60/60
//prints -4
Taking as example 'America/Santiago': this year (2018), DST was set the 13 of May, while in 2015 DST was observed the whole year and in 2014 was set the 27 of April. So, sepending on the date and the year the offset is '-3' or '-4'.
I assume the previous code is not aware of the political decisions of each country regarding DST and probably gets you the offset according to fixed DST dates or something like that. Since I do not know how the nuts and bolts of datetime and pytz I'd rather ask, is this assumption right?
If it is not aware, how can I get the real current offset for any timezone?

I assume the previous code is not aware of the political decisions of each country regarding DST and probably gets you the offset according to fixed DST dates or something like that.
Your assumption is incorrect. Political changes of time zones are recorded and distributed via the TZ Database. Those changes are then implemented in hundreds of different libraries, platforms, and operating systems - such as the pytz library you mention.
The change to America/Santiago that you describe was reflected in TZDB 2016c, which is implemented in pytz 2016.3. If you are using pytz 2016.3 or greater, then your code is aware of this change.
Pytz is fine, but you may want to consider using dateutil instead. Version 2.5.2 or higher has this data.

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 !! :)

Get the timestamp of the start of a day (local time) [duplicate]

This question already has an answer here:
How to get the first datetime of a day?
(1 answer)
Closed 7 years ago.
I wonder if there's any way to get the UNIX timestamp at the beginning of a certain day, i.e the midnight timestamp of each day given its timezone.
Assuming you do not care about daylight savings time and you know the UTC offset which was in effect at that time (as opposed to the UTC offset which is in effect now), you can just do this:
import datetime as dt
return dt.datetime(year, month, day, tzinfo=dt.timezone(utc_offset)).timestamp()
The hour, minute, and second default to zero, so you can skip them. The timezone class does not handle daylight savings time, historical changes in timezone definitions (e.g. British Double Summer Time), or any other temporal anomalies (e.g. there was no December 30, 2011 in Samoa); it is a "dumb" offset. It is equivalent (in this case) to adding or subtracting the offset directly onto the timestamp and then working in UTC. You must ensure this is correct for your use case. If you need better timekeeping, you should install and make use of pytz.

Convert GMT time to Local time in 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.

Categories