0

Suppose someone travels from a country with UTC to +/- X hr timezone. Their device such as a computer or a mobile, is completely switched off during this journey.

Upon reaching the destination, once the device is switched ON without internet, Will the system_clock::now() return a correct value of the UTC time?

Related: How datetime.datetime.now() works without internet connection?

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 1
    Depends how fast they travel. At large enough velocities, relativistic effects will come into play and the device's on-board clock will show a lag compared to external timers. :-) – Adrian Mole Jul 08 '22 at 06:05
  • 1
    ... but, more seriously, how would the device know what time zone it is now in without some sort of connection (internet or phone signal)? – Adrian Mole Jul 08 '22 at 06:11
  • Or are you just asking if `std::chrono::system_clock` can still work without an internet connexion? – Adrian Mole Jul 08 '22 at 06:22
  • 3
    UTC doesn't change with geographic location, so yes? – gre_gor Jul 08 '22 at 06:24

2 Answers2

2

Yes, as long as you don't change any settings.

If you change your computer's location without letting it be aware, it will continue to function as though it was in the previous location. The local time will be wrong, but the UTC will correctly calculate based on the previous settings.

Alan
  • 97
  • 2
1

That is possibly an operating system issue, but normally I would expect the system clock to be universally set to UTC and the TZ offset to be stored separately and applied to higher level time functions such as std::time() for example.

If you specifically want UTC, perhaps std::gmtime() would be more appropriate, since it is explicitly documented as UTC regardless of local time or system clock.

Additionally, regardless of all that, you have to consider how this computer will acquire a local timezone - it needs information to determine that. It might be manually entered, or determined by GNSS position if it has one, or it might "guess" from IP address (which can easily be wrong in territories with multiple timezones). Either way it is not magic.

You can in any event easily test the behaviour of system_clock by temporarily manually modifying your system's TZ.

Clifford
  • 88,407
  • 13
  • 85
  • 165