Difference between pytz.UTC and pytz.timezone('GMT') - pytz

pytz's documentation says:
Note that this instance [pytz.timezone('UTC')] is not the same instance (or implementation)
as other timezones with the same meaning (GMT, Greenwich, Universal,
etc.).
and indeed:
>>> pytz.timezone('UTC') is pytz.timezone('GMT')
False
So... what's the difference?
When should I use pytz.timezone('UTC') and when should I use pytz.timezone('GMT')?

The UTC implementation is a tzinfo implementation that will at all times return 0 minutes offset.
The timezone you get when you do pytz.timezone('GMT') is the GMT timezone defined in the Olson database. It also will return 0 at all times, but in a more complicated manner.
Use UTC.

Related

How to get accurate UTC nanoseconds time stamp in Node.js

python3 has possibility running following construct:
from datetime import datetime
print({'timestamp':str(int(datetime.utcnow().timestamp() * (10**9)))})
That would output JSON-like structore:
{'timestamp': '1567658264143923968'}
Now I want to get similar result in Node.JS 12.
I need a string
I need UTC stamp since 1970, but not since machine boot
Error no more than last 4 digits (microseconds precision or better)
So 32-bitness of int cant be a limitation. (That cant be a problem in Q3 2019, since Node 10 has bigint support).
OS also cant pose such a limitation, since WinAPI has GetSystemTimePreciseAsFileTime and *nix has clock_gettime(CLOCK_MONOTONIC) both are better than 1us
How could I get nanoseconds UTC timestamp string in NodeJS?
Date.now()+"000000"
shows that it lacks last 6 digits (millis), but the task was 4 (micros or better)
This 7yo question seems to address general timestamp question, and hrtime will get you just machine uptime, not real UTC time. results will be irrelevant with that of python.
What are other options?
UPD
Now, I can reword question a bit. From very simple perspective, Nodejs can be seen as a OS-independent runtime library. You write one js code and it will work quite same on all platforms. So it MUST bind most viable functions to js interface; timer functions are among them. As we saw, monotonic clock QueryPerformanceCounter() call is bound through process.hrtime()
Which function in node.js would lead to a clock_gettime(3) call on linux, or GetSystemTimePreciseAsFileTime() call on windows, and return result to js, with microseconds precision or better?
If the resolution you are looking for is microseconds then process.hrtime() should be enough.
const hrTime = process.hrtime()
console.log(hrTime[0] * 1000000 + hrTime[1] / 1000)
Alternatively you could use timestamp-nano
If you need an even higher precision you could build yourself a native node module: https://medium.com/the-node-js-collection/n-api-next-generation-node-js-apis-for-native-modules-169af5235b06
Or if you really like Python, you could execute a python script from Node: How to call a Python function from Node.js
For recent versions of Node, process.hrtime.bigint() seems to provide this.

How to get the runTime of an active task according to its GMT lanuchTime in Spark?

I want to get the runtime of an active task. In the REST API page of /applications/[app-id]/stages/[stage-id], I can get tasks info in detail.
enter image description here
You can see, the executorRunTime is 0 when a task is not completed. I think I can get the runTime according to launchTime. Suppose the launchTime is 2017-12-21T03:15:31.106GMT. I use the following code to compute the runTime.
val format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'.'sss'GMT'", Locale.ENGLISH)
format.setTimeZone(TimeZone.getTimeZone("GMT"))
val launchTime = format.parse("2017-12-21T03:15:31.106GMT").getTime
val runTime = Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTimeInMillis -
launchTime
But I get a negative number. Is the time format wrong? Or what?
TL;DR
String launchTimeString = "2017-12-21T03:15:31.106GMT";
launchTimeString = launchTimeString.replaceFirst("GMT$", "Z");
long launchTime = Instant.parse(launchTimeString).toEpochMilli();
long runTime = System.currentTimeMillis() - launchTime;
(Sorry, I can write only Java 8 code, I will have to trust you to translate.)
java.time and ISO 8601
The date and time classes from Java 1.0 and 1.1 are long outdated, and SimpleDateFormat in particular notoriously troublesome. I recommend you stop using them and use java.time, the modern Java date and time API instead. It is so much nicer to work with.
Your launch time string is a bit funny. It resembles the ISO 8601 standard format with the characteristic T between the date and the time, but has a non-standard GMT in the end, where strings following the standard would usually have Z (or a positive or negative offset from UTC). Since the modern date and time classes parse ISO 8601 as their default, that is, without any explicit formatter, and since writing format pattern strings seems to be an endless source of bugs (it certainly isn’t just you), I found it tempting to modify your string to fit the standard and then parse it.
What was wrong in your format pattern?
There are two bugs in you format pattern string:
You want uppercase HH for hour of day. Lowercase hh is for hour within AM or PM, in the interval 1 through 12. With SimpleDateFormat this bug usually “just” means that an hour of 12 is understood as 00 (which would have given you a very long run time) (the modern DateTimeFormatter more eagerly tells you you have a bug if you try the same with that class).
While lowercase ss is correct for seconds, milliseconds are uppercase SSS. This must have been what hit you: 106 in you string was taken to be seconds rather than milliseconds, so if running your code before 03:16:46, you got a negative run time.
So both bugs boil down to: Format pattern strings are case sensitive, so you need to beware the correct case.
Links
Oracle tutorial: Date Time, explaining how to use java.time.
Wikipedia article on ISO 8601.

NodaTime supports relative time display?

I have a situation where the relative time is more important to a user than an absolute time. So it's more important to be able to quickly say "event happened 5 days and 5 hours ago" than "event happened at 1 PM CDT and it's 5 PM CST 5 days later now."
We store our dates in UTC and convert to display for the user:
pDateTime = DateTime.SpecifyKind(pDateTime, DateTimeKind.Utc);
DateTimeZone dateTimeZone = DateTimeZoneProviders.Tzdb[pCurrentUser.PreferredTimezone];
return Instant.FromDateTimeUtc(pDateTime).InZone(dateTimeZone).ToString("HH':'mm':'ss' 'MM'/'dd'/'yy' 'x", CultureInfo.InvariantCulture);
We'll be using NodaTime 1.2 when it's fully out and just used vanilla ToString before.
However, times using this pattern end up using the daylight status of the time as opposed to the current daylight status. This means that times look something like: 16:15:32 10/25/13 CDT even though we have now transitioned to CST.
It is an absolute measure of the time. This forces the user to do the logic: "How long ago was that? Is it daylight saving time now? If so, the difference is x. If not, I have to add or subtract an hour? That makes the difference y."
Meanwhile, a relative measure of the time would display 15:15:32 10/25/13 CST in the absence of DST. This forces the user to do no conversions and allows them to compute what that time means in context much easier.
In a display that has numerous dates, it can get tricky to do the absolute time logic over the entire set. Doing it once is tricky to get right. However, a friendly relative string like "posted 5 hours ago" also forces them to resolve both the date and time themselves - that information is still important.
A compromise might be to do the posted blank hours/minutes ago for the first 24 hours or to include both the friendly string and absolute time - these are both patterns I've seen done.
But ignoring those, is there a way in NodaTime to imbue a time with a specific daylight status in order to get times displaying in a relative context?
However, times using this pattern end up using the daylight status of the time as opposed to the current daylight status. This means that times look something like: 16:15:32 10/25/13 CDT even though we have now transitioned to CST.
Yes, and it should. Displaying a date/time with CST despite that date/time occurring in CDT would be very odd, IMO.
So it's more important to be able to quickly say "event happened 5 days and 5 hours ago" than "event happened at 1 PM CDT and it's 5 PM CST 5 days later now."
In that case you shouldn't be displaying a date/time at all, in my view. Convert both ZonedDateTime values to Instant, take the Duration between them, and then you can see that it's 5 days and 5 hours ago. (I can't remember how much help we provide with that - you may need to manually take the number of ticks and divide by NodaConstants.TicksPerStandardDay etc. Look at DurationPattern to see if it helps though.)
Alternatively, if you really want to display a date and time, but still easily be able to extract the difference between them mentally, two options suggest themselves:
Use OffsetDateTime instead; there you could force the offsets to be the same, although I still think it would be odd to display an offset which wasn't actually the current offset in the zone you were observing the time in. Or you could just display the relevant offset at the time, so -5 for CST and -4 for CDT.
Just display everything in UTC, so that daylight saving transitions are irrelevant.
Note that you can't get months between the two ZonedDateTime values, as we're dealing with an elapsed time (a duration) rather than calendar-logical arithmetic (a period).

Do NodaTime constructors accept 24 in the Hours parameter

I am hoping to get a quick answer to one question about NodaTime before downloading it. So far, I am reading about NodaTime and its API and it seems very carefully thought out.
I hope it might help eliminate some of the chaos I've been encountering in an application that has a database back-end, a desktop client with a database provider, and a web client that must run on the major browsers. The support for ISO 8601 on DateTime and Time varies greatly on the various database, database provider, and web platforms. Internet Explorer, for example, follows ISO 8601 but SQL Server does not; web UI timepickers do not because Chrome does not.
QUESTION: In NodaTime, is 24:00 a valid Time value? Is 24 a valid argument for the hours parameter of its Time constructors?
BACKGROUND: ISO 8601 allows for two representations of midnight: 00:00 for "midnight this morning" and 24:00 for "midnight tonight". When the DateTime object is on the time-line, a date whose time element has 24:00 coincides with the next day at 00:00. They are the same time-line instant with two different representations, both representations valid per ISO.
A Time-only value is detached from the time-line. A time of 00:00 occurs at the beginning of the detached 24-hour day and a Time-only value of 24:00 is 24 hours after 00:00. A Time type should accept 24 in the hour. When 24 is the hour the maximum value for seconds and milliseconds and ticks is 0 (unless modulo arithmetic is involved and the time rolls over, so that 24:01 is 00:01 -- but ISO has nothing to say about this implementation detail, IIRC).
We accept 24:00 when parsing a LocalDateTime, but not 24:01.
This was issue 153, implemented in revision f7ac0365d8ca.
Unfortunately this was after the 1.0 release, so you'll either need to grab the current code, or wait for 1.1 to be released (hopefully soon).
We don't currently accept it when parsing just a LocalTime. If you want that, please log a feature request - we'd probably look at it for 1.2 (which will have a lot of text features), although I'm not sure what the representation would look like. (LocalTime itself doesn't support the idea of "end-of-day midnight".)

Are struct stat times GMT?

One of the fields in struct stat is st_mtime. I assume that is seconds since jan 1, 1970. Is that GMT or local time?
The time_t type represents the number of seconds that have passed since 1 January 1970 00:00 UTC (that moment in time is called the "epoch" and happened at the same moment everywhere around the world). You can consider "UTC" to mean the same thing as "GMT" (see Leap Second for detail about the very small differences).
Be aware that instead of adding or subtracting values from the time_t type, you should always use the localtime() and mktime() functions to convert to and from a local time zone representation.

Resources