dateutil seems to give wrong timezone - timezone

I use datetime and dateutil to get my current UTC offset and time zone like this:
>>> from datetime import datetime
>>> from dateutil.tz import tzlocal
>>>
>>> current_time = datetime.now(tzlocal())
>>> print('Your UTC offset is {:+g}'.format(current_time.utcoffset().total_seconds()/3600))
Your UTC offset is +2
>>> print('Your time zone is {}'.format(current_time.tzname()))
Your time zone is W. Europe Daylight Time
The UTC offset is correct, but not the time zone. I have Central European Daylight Time; besides, Western European Daylight Time is UTC+1 so it doesn't even match with the UTC offset that is printed. So why does this code print "W. Europe Daylight Time" and not "C. Europe Daylight Time"?

Related

How to display timezones abbreviations?

I'm looking at the timezones and trying to figure out abbreviations. I'm using list of timezones and offsets to display them on my website.
Client asked to add abbreviation for each timezone. Once I listed abbreviations I realised that there are the same timezone names for different abbreviations such as:
'edt'
'offset' = -14400
'timezone_id' = 'America/New_York'
'ept'
'offset' = -14400
'timezone_id' = 'America/New_York'
'est'
'offset' = -18000
'timezone_id' = 'America/New_York'
'ewt'
'offset' = -14400
'timezone_id' = 'America/New_York'
Can someone please explain what is that exactly and how do I know which abbreviation to display?
US Timezones
If you're working with US Timezones they are broken down into 4 Timezones that cover the Mainland US (Except Alaska and Hawaii)
EST EASTERN STANDARD TIME UTC - 5
EDT EASTERN DAYLIGHT TIME UTC - 4
CST CENTRAL STANDARD TIME UTC - 6
CDT CENTRAL DAYLIGHT TIME UTC - 5
MST MOUNTAIN STANDARD TIME UTC - 7
MDT MOUNTAIN DAYLIGHT TIME UTC - 6
PST PACIFIC STANDARD TIME UTC - 8
PDT PACIFIC DAYLIGHT TIME UTC - 7
So you'd be using EST,CST,MST and PST for the Mainland US anyway. Hawaii and Alaska have different Timezones and off the top of my head am not sure exactly what they are

Why is SQL Server 2017 not recognizing AT Time zone?

I'm running the following query on a SQL server 2017 Database (developer edition). the query executes but it only shows the getdate value without actually converting. I also see that "AT TIME ZONE" is no fully highlighted in blue by the SQL editor, which means it is probably not recognized. Any idea please?
SELECT
GETDATE() AT TIME ZONE 'Central Europe Standard Time' as CET,
GETDATE() AT TIME ZONE 'Cuba Standard Time' as 'Cuba Time'
P.S: I checked that time zones are defined and spelled correctly
SELECT * FROM sys.time_zone_info
It seems the problem is your misunderstanding of how AT TIME ZONE works with a datetime datatype (which is what GETDATE() returns). Take the following:
DECLARE #DT datetime = '2020-01-17T16:39:01.123';
SELECT #DT AT TIME ZONE 'Central Europe Standard Time' as CET,
#DT AT TIME ZONE 'Cuba Standard Time' as [Cuba Time];
This returns the below:
CET Cuba Time
---------------------------------- ----------------------------------
2020-01-17 16:39:01.123 +01:00 2020-01-17 16:39:01.123 -05:00
Notice the times are the same as what is was in #DT apart from the timezone. This is because datetime is timezone agnostic; it doesn't know that exist. As a result the timezone is simply "added on" to the value.
If you use a datetimeoffset, however, you get the result you expect:
DECLARE #DT datetimeoffset(0) = '2020-01-17T16:39:01.123';
SELECT #DT AT TIME ZONE 'Central Europe Standard Time' as CET,
#DT AT TIME ZONE 'Cuba Standard Time' as [Cuba Time];
Returns:
CET Cuba Time
---------------------------------- ----------------------------------
2020-01-17 17:39:01 +01:00 2020-01-17 11:39:01 -05:00
In your case, that means instead you need to use SYSDATETIMEOFFSET:
SELECT SYSDATETIMEOFFSET() AT TIME ZONE 'Central Europe Standard Time' as CET,
SYSDATETIMEOFFSET() AT TIME ZONE 'Cuba Standard Time' as [Cuba Time];
For me, as I write this answer, that returns the below:
CET Cuba Time
---------------------------------- ----------------------------------
2020-01-17 17:43:55.5596303 +01:00 2020-01-17 11:43:55.5596303 -05:00

Will convert_timezone 'America/New_York' work regardless of my timezone's offset depending on time of year?

My timezone is -4 or -5 from GMT depending on time of year. Currently it is -4.
If I use this code below, will it automatically switch when we change to -5?
select convert_timezone('America/New_York', CAST(json_extract_path_text('{"timestamp":"2019-04-16T03:30:00.704Z"}', 'timestamp') as TIMESTAMP));
Currently the line above returns a date of 2019-04-15.
But if I make this call in December (note the time has +1 hour),
will it still return a date of 2019-04-15.
select convert_timezone('America/New_York', CAST(json_extract_path_text('{"timestamp":"2019-04-16T04:30:00.704Z"}', 'timestamp') as TIMESTAMP));
In other words, will it switch between EDT and EST without intervention? Or is there a better way?
Thanks
Consider:
2019-04-16T03:30:00.704Z (UTC) in America/New_York is 2019-04-15T23:30:00.704-04:00
2019-04-16T04:30:00.704Z (UTC) in America/New_York is 2019-04-16T00:30:00.704-04:00
2019-12-16T04:30:00.704Z (UTC) in America/New_York is 2019-12-15T23:30:00.704-05:00
For US Eastern Time, a date in April will be in -4 while a date in December will be in -5. In other words, the timestamp being converted is what determines which offset to apply.
The date and time that the code executes has no relevance.

Convert US Central Time to different time zones using moment JS

I have a Date Time(Friday, 27 October 2017 4:00:00 AM) in US Central Time zone (CDT). I want to convert this Date Time into different time zones. These are time zones i wanted to convert.
Eastern Time (EDT)
Pacific Time (PDT)
New Delhi, India (IST)
Central Europian Time (CET)
Riyadh, Saudi Arabia (AST)
Pakistan Standard Time (PKT)
Lagos, Nigeria (WAT)
Australian Standard Time (AET)
Greenwich Mean Time (GMT)
Moscow, Russia (MSK)
China Standard Time (CST)
This is how i am doing
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "America/Chicago");
var IST = dateTime.tz('Asia/Calcutta').format('MMMM Do YYYY, h:mm:ss a');
console.log(IST) // October 27th 2017, 9:30:00 am
The returned Date Time is wrong. Because Indian Standard Time is 10 hours and 30 minutes ahead of Central Time.
It should be Friday, 27 October 2017 2:30 PM (IST)
Thanks!
The problem isn't with the conversion to the Indian time zone - it's the original parsing of the Chicago time.
This:
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "America/Chicago");
... is treated as 4am UTC, and then converted to America/Chicago, so it ends up representing 11pm local time (on October 26th) in Chicago. You can see that by just logging the value of dateTime.
If you change the code to:
var dateTime = moment.tz("2017-10-27 04:00:00", "America/Chicago");
... then it's treated as 4am local time on the 27th, which is what I believe you expected. The result of the conversion to Asia/Calcutta is then 2:30pm as you expected.
So either change the format of your input, or specify that format. For example, this works fine too:
var dateTime = moment.tz("2017-10-27 4:00:00 AM", "YYYY-MM-DD h:mm:ss a", "America/Chicago");

moment-timezone format doesn't return the expected result

Using the tz() function from moment-timezone as follow:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format()
//returns '2017-10-15T13:53:43+08:00'
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format('h:m A')
//I expect to return '9:53 PM' but it returns '1:53 PM'
Ultimately, I want to apply the fromNow() function to format the result. But when I apply it, it uses the initial timestamp and ignore the timezone applied.
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').fromNow()
//I expect to return '1 min ago' when actual time is 13:54 UTC (21:54 in HK) but it returns '8 hours ago'
What am I doing wrong here?
When you do:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong');
You're creating a date/time that corresponds to October 15th 2017, at 1:53 PM in Hong Kong - which, in turn, corresponds to 2017-10-15T05:53:43Z (5:53 AM in UTC).
When you call the format() function:
moment.tz('2017-10-15 13:53:43','Asia/Hong_Kong').format();
It returns:
2017-10-15T13:53:43+08:00
The +08:00 part is just the UTC offset - it just tells that Hong Kong is 8 hours ahead UTC. But 2017-10-15T13:53:43+08:00 (1:53 PM in Hong Kong) is exactly the same instant as 2017-10-15T05:53:43Z (5:53 AM in UTC). That's why fromNow(), when the current time is 13:54 UTC, returns 8 hours.
If you want the date/time that corresponds to 1:53 PM in UTC, you should use the utc() function:
// October 15th 2017, 1:53 PM in UTC
moment.utc('2017-10-15 13:53:43');
Now, when the current time is 13:54 UTC, fromNow() will return 1 minute (because the date/time represents 1:53 PM in UTC).
To convert this to Hong Kong timezone, just use the tz() function:
// convert 1:53 PM UTC to Hong Kong timezone (9:53 PM)
moment.utc('2017-10-15 13:53:43').tz('Asia/Hong_Kong').format('h:m A');
This will convert 1:53 PM UTC to Hong Kong timezone (resulting in 9:53 PM):

Resources