Why is the conversion from EST to UTC +5 hours and not +4? [duplicate] - datetime

This question already has answers here:
How to make a timezone aware datetime object
(15 answers)
Closed 6 years ago.
I've got a datetime which has no timezone information. I'm now getting the timezone info and would like to add the timezone into the existed datetime instance, how can I do?
d = datetime.datetime.now()
tz = pytz.timezone('Asia/Taipei')
How to add the timezone info tz into datetime a

Use tz.localize(d) to localize the instance. From the documentation:
The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information):
>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
>>> print(loc_dt.strftime(fmt))
2002-10-27 06:00:00 EST-0500
If you don't use tz.localize(), but use datetime.replace(), chances are that a historical offset is used instead; tz.localize() will pick the right offset in effect for the given date. The US Eastern timezone DST start and end dates have changed over time, for example.
When you try to localize a datetime value that is ambiguous because it straddles the transition period from summer to winter time or vice-versa, the timezone will be consulted to see if the resulting datetime object should have .dst() return True or False. You can override the default for the timezone with the is_dst keyword argument for .localize():
dt = tz.localize(naive, is_dst=True)
or even switch off the choice altogether by setting is_dst=None. In that case, or in the rare cases there is no default set for a timezone, an ambiguous datetime value would lead to a AmbiguousTimeError exception being raised. The is_dst flag is only consulted for datetime values that are ambiguous and is ignored otherwise.
To go back the other way, turn a timezone-aware object back to a naive object, use .replace(tzinfo=None):
naivedt = awaredt.replace(tzinfo=None)

If you know that your original datetime was "measured" in the time zone you are trying to add to it, you could (but probably shouldn't) use replace rather than localize.
# d = datetime.datetime.now()
# tz = pytz.timezone('Asia/Taipei')
d = d.replace(tzinfo=tz)
I can imagine 2 times when this might make sense (the second one happened to me):
Your server locale is set to the incorrect time zone and you are trying to correct a datetime instance by making it aware of this incorrect timezone (and presumably later localizing it to the "correct" time zone so the values of now() match up to other times you are comparing it to (your watch, perhaps)
You want to "tag" a time instance (NOT a datetime) with a time zone (tzinfo) attribute so that attribute can be used later to form a full datetime instance.

Related

Timestamp field in Avro source connector without time zone

I have a SQL DATETIME field DT that I want to feed into Kafka using Avro. DT is in local time but has no time zone; that's stored in another column TZ as an offset from UTC in minutes. It seems that an Avro timestamp-millis would be appropriate for DT, but I'm confused about conversion, given the lacking time zone. From looking at the connector source code, I think it will just default to UTC.
So that raises the questions:
Will all visibly similar datetimes convert to the same number of milliseconds since the/an epoch, regardless of their nominal time zone? For example, does (2018-01-01T00:00Z).to_timestamp_ms() == (2018-01-01T00:00).to_timestamp_ms() == (2018-01-01T00:00+05).to_timestamp_ms()?
More importantly, is it possible to convert to true UTC after ingestion by subtracting the time zone offset TZ from the datetime field DT (which is now in milliseconds since some epoch)? In other words, if the connector incorrectly assumes UTC, and falsely interprets the datetime as UTC, can the true datetime be recovered after that by subtracting the offset?
Details on transformation steps
I think the order of operations is something like this on the Connector (largely out of my control):
tz = read_field_as_int('tz')
dt = read_field_as_string('dt')
parsed_datetime = datetime(dt, timezone=UTC)
message = {
'dt': parsed_datetime.to_timestamp_ms(),
'tz': tz
}
producer.produce(message)
And then later, in the consumer, maybe this would work?
ms_per_min = 60 * 1000
message = consumer.poll()
true_timestamp = message['dt'] - message['tz'] * ms_per_min
true_dt = datetime.from_timestamp(true_timestamp, timezone=UTC)

how to find predefined timezone IDs of ballerina time package?

In ballerina time packages createTime function, it asks for zoneID as a function parameter, when I provide a custom zoneID, program keeps running without any output. Is there a way to find those predefined time zoneIDs with relevant timezones?
The underlying implementation of ballerina time package is Java and the zone ID processing rules are similar to the Java rules. Basically rules are as follows.
If the zone ID equals 'Z', the result is UTC. If the zone ID equals
'GMT', 'UTC' or 'UT', it is equivalent to UTC.
If the zone ID starts with '+' or '-', the ID is parsed as an offset.
Offset can be specified in one of the following ways.
+h,
+hh,
+hh:mm,
-hh:mm,
+hhmm,
-hhmm,
+hh:mm:ss,
-hh:mm:ss,
+hhmmss,
-hhmmss
Also zone id can be a region-based zone ID. The format is '{area}/{city}' eg: "America/Panama". The zones are based on IANA Time Zone Database (TZDB) supplied data.
Ex:
Using UTC
time:Timezone zoneValue = {zoneId:"Z"};
time:Time time = new(1456876583555, zoneValue);
Using offset
time:Timezone zoneValue = {zoneId:"-05:00"};
time:Time time = new(1456876583555, zoneValue);
Using region-based zone ID
time:Timezone zoneValue = {zoneId:"America/Panama"};
time:Time time = new(1456876583555, zoneValue);
We hope to improve this API in a future version of Ballerina with proper representation for the zone ids etc.
You can refer to the Ballerina date time example for more details.

How to convert a local datetime to UTC with moment.js

I've found a couple of existing StackoverFlow questions on this but nothing very definite.
I have a local datetime. I want this in UTC. my local datetime does not have a 'Z' at the end or any offset information.
I first tried:
moment(mylocaldatetime).toISOString() #works fine because this method always returns time in UTC
But for consistency with other code I didn't want to use to ISOString() so I did this:
moment(mylocaldatetime).utc().format()
This seems to work fine. If the browser running this code is in UTC + 1 I get a datetime one hour less than mylocaldatetime (with an offset string if I specify that in the format). I.e. it has treated mylocaldatetime as a local time, taken account of my current time zone, and given me my local time as UTC.
However. This appears to contradict the moment.js docs which are pretty clear that:
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment(). - Notice the 'parse'.
and
Moment normally interprets input times as local times (or UTC times if moment.utc() is used).
If these doc comments were true this line:
moment(mylocaldatetime).utc().format()
should treat mylocaldatetime as if it were utc and then output this datetime in utc - no difference. No conversion. But that is not what I get.
Maybe what this line moment(mylocaldatetime).utc().format() is saying is:
create a moment object in local mode with mylocaldatetime. Then put the moment object into utc mode. So now when we format for display we output as utc. IF this is the case I think the docs could be made clearer.

Converting Date to CurrentCompany timeZone in Dynamics ax x++

I have a scenario where i need to convert a Datefield(joindate) to currentcompany timezone date. And then i need to compare this with anotherdate(startdate). If the difference is more than 365 days i need to give an warning. Can someone help me in this.
Thanks in advance.
You can apply a timezone to an utcdatetime via DateTimeUtil::applyTimeZoneOffset
The company timezone can be retrieved by calling DateTimeUtil::getCompanyTimeZone
Afterwards calculate the difference by calling DateTimeUtil::getDifference, which returns the difference in seconds so you have to compare that with the seconds per year.
To avoid inserting a 'magic' number, use the constants in the macro library TimeConstants.
If Datefield(joindate) is of type date and not utcDateTime then DateTimeUtil::newDateTime() should be used to convert it to utcDateTime:
utcDateTime joinDateTime = DateTimeUtil::newDateTime(joindate, 0,
DateTimeUtil::getCompanyTimeZone());
DateTimeUtil::getDifference() can be used to get the number of seconds between the utcDateTime values.
If both Datefield(joindate) and anotherdate(startdate) are of type date and not utcDateType then no conversion is required at all, and you can check whether the difference is more than 365 as follows:
if (joindate - startdate > 365) {}
If the above assumptions are wrong, see DAXaholic's answer.

Conversion from DateTime (Joda Time) to Date (java.util)

I parse this date "22/11/11" into a DateTime object called s. When I do s.getDayOfMonth() it gives me 22 which is right. However, when I convert the DateTime object to a Date object and try to get the date using s.toDate().getDate() it returns 24 which is not right. Does anyone have an idea why is this happening ?
The Joda-Time DateTime.toDate() method converts to a java.util.Date with the same millisecond instant. The java.util.Date.getDate() method uses the local default time-zone to return its value, whereas DateTime.getDayOfMonth() uses the time-zone stored in the DateTime object. If the two time-zones are different, you may see a difference, explaining the 22/24 you observe.
To understand further, print the time-zone of the DateTime, and the default Java zone TimeZone.getDefault() used by java.util.Date.

Resources