I printed out a <class 'pandas._libs.tslibs.timestamps.Timestamp'>
time
variable, and it displays as
2006-04-03 08:00:00 01:00
I am wondering what is the 01:00
at the end? Seems this is related to daylight savings?
When I did
print(time.timestamp())
I am seeing
1144047600.0
which is off by 3600 (an hour).
CodePudding user response:
01:00
specifies an offset from UTC. Positive means ahead of UTC, so 8 am 01:00 would be 7 am UTC. Negative means behind UTC, so 8 am -01:00 would be 9 am UTC.
In that context, "timezone" is often mentioned. However it is important to note that many time zones in a geographical sense / as you can find them in the IANA database can share the same UTC offset at a given point in time.
Also, a UTC offset alone doesn't tell you anything about DST. For that you need to know the timezone. The combination of date/time, timezone and the rules associated with that timezone allow you to determine if DST is active or not.
For your example date try the following:
from datetime import datetime
import zoneinfo
dt = datetime.fromisoformat("2006-04-03")
# let's assume a time zone and see if DST is active:
dt = dt.replace(tzinfo=zoneinfo.ZoneInfo("Europe/London"))
print(dt, dt.dst())
# 2006-04-03 00:00:00 01:00 1:00:00
#--> so yes, DST is active, offset of one hour
# let's see which time zones share UTC 1 on that date:
for z in zoneinfo.available_timezones():
if dt.replace(tzinfo=zoneinfo.ZoneInfo(z)).strftime("%z") == " 0100":
print(z)
>>>
Europe/Guernsey
Europe/Jersey
GB
Africa/Ndjamena
GB-Eire
Europe/London
Europe/Belfast
Eire
Europe/Dublin
Africa/Algiers
Atlantic/Canary
Atlantic/Faroe
Europe/Lisbon
Atlantic/Faeroe
Etc/GMT-1
Africa/Bangui
Africa/Porto-Novo
Africa/Libreville
Africa/Malabo
Africa/Kinshasa
Africa/Lagos
Africa/Luanda
Atlantic/Madeira
Africa/Windhoek
Europe/Isle_of_Man
Africa/Niamey
Portugal
WET
Africa/Douala
Africa/Brazzaville
CodePudding user response:
What is the " 01:00" in "2006-04-03 08:00:00 01:00" ...?
This is the timezone offset in the format specified by ISO 8601 standard. In this example, it tells that 2006-04-03 08:00:00 01:00
is offset by one hour from UTC i.e. the equivalent date-time at UTC will be 2006-04-03T07:00Z
where Z
is the abbreviation for Zulu
and represents the timezone offset of 00:00
.
As you can see, the calculation is simple: To get the UTC date-time from a date-time with a different offset, subtract the excess offset or add the less offset, as applicable.
My area of expertise is Java and therefore, I am presenting a demo using Java language.
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String args[]) {
DateTimeFormatter parser = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssXXX");
OffsetDateTime odt = OffsetDateTime.parse("2006-04-03 08:00:00 01:00", parser);
System.out.println(odt);
System.out.println(odt.withOffsetSameInstant(ZoneOffset.UTC));
odt = OffsetDateTime.parse("2006-04-03 08:00:00-01:00", parser);
System.out.println(odt);
System.out.println(odt.withOffsetSameInstant(ZoneOffset.UTC));
}
}
Output:
2006-04-03T08:00 01:00
2006-04-03T07:00Z
2006-04-03T08:00-01:00
2006-04-03T09:00Z
You may be interested to know the difference between timezone and timezone offset. A timezone ID is typically specified as Region/City e.g. America/New_York
, Europe/London
etc. You can check the tz database here. A timezone can have one or more timezone offsets e.g. Asia/Kolkata
has a single timezone offset of 05:30
. In comparison Europe/London
has two timezone offsets: 01:00
in summer while 00:00
in winter as per its DST schedules. Because of this reason, an object representing a date-time with timezone is designed to adjust its timezone offset automatically. On the other hand, an object representing a date-time with timezone offset contains a fixed offset value. The following demo shows how a ZonedDateTime
in Java automatically adjusts its offset.
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String args[]) {
ZoneId zoneId = ZoneId.of("Europe/London");
ZonedDateTime zdtSummer = LocalDate.of(2022, 5, 20).atStartOfDay(zoneId);
System.out.println(zdtSummer);
ZonedDateTime zdtWinter = LocalDate.of(2022, 11, 20).atStartOfDay(zoneId);
System.out.println(zdtWinter);
}
}
Output:
2022-05-20T00:00 01:00[Europe/London]
2022-11-20T00:00Z[Europe/London]
Learn more about the modern Date-Time API from Trail: Date Time.