Sdk: DateTime(...).toUtc() and DateTime.utc(…) give different results

Created on 2 Apr 2019  Â·  3Comments  Â·  Source: dart-lang/sdk

  • Dart VM version: 2.1.1-dev.1.0 (Wed Jan 9 14:55:46 2019 +0100) on "macos_x64"

    * Using Flutter

The issue is rather straightforward. I would expectDateTime(...).toUtc() to give the same output as DateTime.utc(…) but they don't.

Given the following example:

DateTime now = DateTime.now();
DateTime toUtc = DateTime(now.year, now.month, now.day).toUtc();
DateTime utc = DateTime.utc(now.year, now.month, now.day);
print("now $now");
print("toUtc $toUtc");
print("utc $utc");

the output will be:

now 2019-04-02 16:28:23.223
toUtc 2019-04-01 22:00:00.000Z
utc 2019-04-02 00:00:00.000Z

whereas I would expect it to be:

now 2019-04-02 16:28:23.223
toUtc 2019-04-02 00:00:00.000Z
utc 2019-04-02 00:00:00.000Z

area-library closed-as-intended library-core

Most helpful comment

I think this behavior makes sense.

The following creates a new DateTime object when the current timestamp are in you own timezone and converts it to a new datetime object where the time is represented as UTC:
DateTime toUtc = DateTime(now.year, now.month, now.day).toUtc();

And here you create a DateTime object where you tell Dart that the input are already UTC.
DateTime utc = DateTime.utc(now.year, now.month, now.day);

Please see the documentation for the two constructors:
https://api.dartlang.org/stable/2.2.0/dart-core/DateTime/DateTime.html
https://api.dartlang.org/stable/2.2.0/dart-core/DateTime/DateTime.utc.html

And the toUtc() method:
https://api.dartlang.org/stable/2.2.0/dart-core/DateTime/toUtc.html

All 3 comments

I think this behavior makes sense.

The following creates a new DateTime object when the current timestamp are in you own timezone and converts it to a new datetime object where the time is represented as UTC:
DateTime toUtc = DateTime(now.year, now.month, now.day).toUtc();

And here you create a DateTime object where you tell Dart that the input are already UTC.
DateTime utc = DateTime.utc(now.year, now.month, now.day);

Please see the documentation for the two constructors:
https://api.dartlang.org/stable/2.2.0/dart-core/DateTime/DateTime.html
https://api.dartlang.org/stable/2.2.0/dart-core/DateTime/DateTime.utc.html

And the toUtc() method:
https://api.dartlang.org/stable/2.2.0/dart-core/DateTime/toUtc.html

CC @lrhn

This is working as intended.

DateTime(y, m, d).toUtc() finds the y-m-d time in the local time-zone and then creates a UTC date at the same point in time. So, if I'm in the UTC+2 time zone, and I do DateTime(2019, 04, 02).toUtc(), I start with the local time 2019-04-02T00:00:00+02:00, and then find that the corresponding UTC-time is 2019-04-01T22:00:00Z. That's the same point in time in a different time zone, it answers the question "when the local time is 2019-04-02T00:00:00, what is the time in the UTC time zone".

When you do DateTime.utc(2019, 04, 02), you get the point in time where the UTC clock showed 2019-04-02T00:00:00Z. If I do toLocal() on that, I'd get the local time 2019-04-02T02:00:00+02:00.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brooth picture brooth  Â·  3Comments

ranquild picture ranquild  Â·  3Comments

nex3 picture nex3  Â·  3Comments

DartBot picture DartBot  Â·  3Comments

DartBot picture DartBot  Â·  3Comments