current time

I used the Date class such as "currentTime = Date()", but I get a date that is off by 5 hours exactly. How do I get the current time? Is there a time zone setting I have to set?

Accepted Reply

I think you are trying to do too much work.


Imagine that you have an old fashioned watch on your wrist, set to the correct time in the CMT time zone.

You are attacked by a ninja, who knocks you unconcious and puts you on a plane to Siberia.

You wake up, with no idea what the time difference is between CMT and whatever timezone this part of Siberia is in.

The ninja tells you that you will freeze to death in one hour.


If you look at the current time in CMT on your watch, can you figure out later exactly how much time you have left until that one hour is up? Or do you need to know the correct timezone you are in?



Instances of Date or NSTimer don't really care about the timezone, they only care about how much time passes.


So the current Date() doesn't need to know or care what a timezone is, it only cares how many seconds have passed since a specific point in time that it uses for reference. NSTimer doesn't need to know or care what a timezone is, it just needs to trigger the code when the right number of seconds have passed since that same specific point in time that it uses for reference.


The description property of a Date uses GMT when it converts itself to a string, which doesn't work so well for people who want to compare that date to their own timezone. That's why a DateFormatter can give you a localized string with the "correct" time, to display for users. But the Date itself isn't in GMT, it is just a reference to a number of seconds, which pass in the same amount of time no matter what the timezone is.

Replies

I think you are trying to do too much work.


Imagine that you have an old fashioned watch on your wrist, set to the correct time in the CMT time zone.

You are attacked by a ninja, who knocks you unconcious and puts you on a plane to Siberia.

You wake up, with no idea what the time difference is between CMT and whatever timezone this part of Siberia is in.

The ninja tells you that you will freeze to death in one hour.


If you look at the current time in CMT on your watch, can you figure out later exactly how much time you have left until that one hour is up? Or do you need to know the correct timezone you are in?



Instances of Date or NSTimer don't really care about the timezone, they only care about how much time passes.


So the current Date() doesn't need to know or care what a timezone is, it only cares how many seconds have passed since a specific point in time that it uses for reference. NSTimer doesn't need to know or care what a timezone is, it just needs to trigger the code when the right number of seconds have passed since that same specific point in time that it uses for reference.


The description property of a Date uses GMT when it converts itself to a string, which doesn't work so well for people who want to compare that date to their own timezone. That's why a DateFormatter can give you a localized string with the "correct" time, to display for users. But the Date itself isn't in GMT, it is just a reference to a number of seconds, which pass in the same amount of time no matter what the timezone is.

>> The value I get from Date(timeIntervalSinceNow: delayInSeconds) is still in absolute time or Greenwich Mean Time.


No, you're just plain wrong about this. The value in a Date is a number of type Double (well, type TimeInterval, which is an alias for Double), representing a number of seconds.


You think it's a date, because anything that displays a Date (such as "print") will automatically apply a date formatter to produce a string representation. If you want to see what a Date contains, try "print (date.timeIntervalSinceReferenceDate)". That's what's actually inside.

Would this be correct in all cases ?

Yes. However, the code is not correct (-: Lines 2 through 4 are redundant and can be removed.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"