MATLAB Answers

Using now() in different time zones (or with daylight saving time)

5 ビュー (過去 30 日間)
Neil Rogers
Neil Rogers 2019 年 12 月 6 日
Edited: Neil Rogers 2019 年 12 月 9 日
If two people in two different time zones simultaneously used the now() function, would they get different answers? Also, does the now() result depend on whether daylight saving time is in use?
I understand now() returns the number of days since 0 Jan 0000, but does that mean "0 Jan 0000 UTC", or "0 Jan 0000 in the local time zone"?
Many thanks,
Neil

  2 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2019 年 12 月 6 日
in the local time zone (i.e. the one the computer is set to).
Neil Rogers
Neil Rogers 2019 年 12 月 6 日
So does that mean that if Alice in London types
now()
at exactly the same time as Bob types
now()
in Delhi, they should get exactly the same result, even though their local times differ by several hours?
I'm in the London/GMT time zone, so I can't easily test this without calling Bob.

サインイン to comment.

採用された回答

Steven Lord
Steven Lord 2019 年 12 月 6 日
If you want control over the time zone, you probably want to use datetime instead of now.
N1 = datetime('now', 'TimeZone', 'local')
N2 = datetime('now', 'TimeZone', 'America/New_York')
N3 = datetime('now', 'TimeZone', 'Europe/London')
See this documentation page for more information about working with dates and times with time zones. If you want to know if a datetime occurs during Daylight Savings Time, use isdst. When called on datetime arrays without a time zone set, isdst return false.

  3 件のコメント

Neil Rogers
Neil Rogers 2019 年 12 月 6 日
Thanks. In future I think I shall use
t = datetime('now','TimeZone','Etc/UTC')
to get the current time in UTC, which I hope will be independent of any local time zones or daylight saving time offsets. UPDATE: This isn't right - see my comment below!
Previously I'd been using now() and datevec() functions, and I wanted to understand what problems these might cause when running the code in a different time zone.
Steven Lord
Steven Lord 2019 年 12 月 6 日
The timezones function output for the "Etc" Area shows that Etc/UTC has 0 UTCOffset and 0 DSTOffset, so I think you're fine.
t = timezones('Etc')
Neil Rogers
Neil Rogers 2019 年 12 月 9 日
After setting my computer's timezone to non-GMT, I tested this method and it doesn't work as expected. datetime('now','TimeZone','Etc/UTC') still returns the local time(!).
My best solution now is to use the java method
utc = java.lang.System.currentTimeMillis / 8.64e7 + datenum(1970, 1, 1);
This returns a serial date number for the time in UT, and seems to be consistent across time zones - at least for my system; I'm not sure if it is true for all systems.
If anyone knows of a simple Matlab alternative to this, please let me know.

サインイン to comment.

More Answers (1)

Jeremy Marcyoniak
Jeremy Marcyoniak 2019 年 12 月 6 日
From the documentation for
now
Limitations
  • MATLAB Online returns the current date and time in Coordinated Universal Time (UTC) rather than local time.

  1 件のコメント

Jeremy Marcyoniak
Jeremy Marcyoniak 2019 年 12 月 6 日
Additionally, you can use the syntax
[c tf] = clock
Where tf will be a logical that is 1 (true) if the current local date and time occur during Daylight Saving Time in your current time zone, and 0 (false) otherwise.

サインイン to comment.

サインイン してこの質問に回答します。

製品


リリース

R2018a

Translated by