#1 2020-08-28 12:37:36

George
Member
Registered: 2016-04-05
Posts: 140

DateTimeToIso8601 as UTC

As i understand, Iso8601ToDateTime and DateTimeToIso8601 just ignores timezones (and utc marker "z").
I can ensure that all incoming date strings will be in UTC timezone.
Then, after DateTimeToIso8601 i can just add "Z" at the end.

Maybe exists a better way?

Last edited by George (2020-08-28 12:53:55)

Offline

#2 2020-08-28 16:45:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,207
Website

Re: DateTimeToIso8601 as UTC

I am not sure I understand what is your question... sad

Offline

#3 2020-08-28 17:16:51

George
Member
Registered: 2016-04-05
Posts: 140

Re: DateTimeToIso8601 as UTC

iso8601 datetime string may include timezone.
For example: 2020-08-28T15:55:56+05:00
Datetime in UTC timezone: 2020-08-28T15:55:56Z

Iso8601ToDateTime() method converts "2020-08-28T15:55:56+05:00" to 2020-08-28 15:55:56. It ignores timezone part.
Theoretically, Iso8601ToDateTime may respect timezone part and return date converted to local timezone with proper timezone offset.
If you live in France (GMT+2), then for "2020-08-28T15:55:56+05:00" correct date will be: 2020-08-28T12:55:56.
When value contain "Z" (which mean utc+0) value may be converted like now (2020-08-28T15:55:56Z = 2020-08-28 15:55:56).

Second part is DateTimeToIso8601() method.
Currently it does not allow to specify timezone for the TDateTime value.
For example, method signature may be changed to support timezones:

// - Use UTC to specify that D in UTC timezone (will add Z at the end of string). When true, TimeZoneOffset will be ignored.
// - Use TimeZoneOffset to specify timezone offset in minutes, for example for +03:30 set 210
function DateTimeToIso8601(D: TDateTime; Expanded: boolean; FirstChar: AnsiChar='T'; WithMS: boolean=false; QuotedChar: AnsiChar=#0; UTC: boolean = false; TimeZoneOffset: integer = 0): RawUTF8; overload;

And maybe also would be usefull to add GetTimezoneOffset():integer method.
Similar to javascript.

Last edited by George (2020-08-28 17:28:19)

Offline

#4 2020-08-28 18:31:28

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,539
Website

Re: DateTimeToIso8601 as UTC

In my application  on the server-side I consider DateTime is in UTC0, during writing Date/DateTime to JSON I add 'Z' in the end (manually).
In case client send me a Date/DateTime I expect client convert it to UTC0 (JavaScript from browser doing it by default).

This works perfectly for mutli-timezoes applications.

Offline

#5 2020-08-28 19:40:33

George
Member
Registered: 2016-04-05
Posts: 140

Re: DateTimeToIso8601 as UTC

In my application  on the server-side I consider DateTime is in UTC0, during writing Date/DateTime to JSON I add 'Z' in the end (manually).

I used the same approach.

In another project written on another language, i implemented similar methods (for iso8601 conversions) that also supports timezone part.
Even if DateTimeToIso8601 and Iso8601ToDateTime will be updated (like i posted above), current approach will continue to work if date string contain "Z" at the end, but also will work correctly with timezone.
In general it more accurate iso8601 handling which gives more options.

Last edited by George (2020-08-28 19:43:50)

Offline

#6 2020-08-29 10:37:45

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,539
Website

Re: DateTimeToIso8601 as UTC

IMHO - there are many hidden problems in timezones. For example GetTimezoneOffset should depends on current user timezone and a date itself (because of Daylight saving time what changes from year to year. For example in Ukraine it changed  may be 4 time since 1970).
The same with TimeZoneOffset you propose - it's depends on Date passed to DateTimeToIso8601. And so on... So here better not to implement it than to implement it incorrectly.

But adding a UTC: boolean = false to DateTimeToIso8601 (it true = add a Z to the end of string) and parsing a +XXX in Iso8601ToDateTime is a good ideas and easy to implement.

P.S.
In JavaScript ZoneOffset calculation actually delegated to ICU library. The same in Postgres and many other products. So even such big companies do not want to do it itself.

Last edited by mpv (2020-08-29 10:43:06)

Offline

Board footer

Powered by FluxBB