I live in the US eastern time zone (EST), and a couple weeks ago the daylight savings began. I was testing some of my earlier code, which was working(supposedly) fine and noticed that the day second calculations are running 1 hour earlier.
here's the code that I use for testing:
int gmtOffset = [[NSTimeZone localTimeZone] secondsFromGMT];
int daylightOffset = [[NSTimeZone localTimeZone] daylightSavingTimeOffset];
int daySeconds = ((int)([date timeIntervalSinceReferenceDate]+gmtOffset+daylightOffset))%(86400+1);
NSLog(@"Day seconds: %i", daySeconds);
The result is 3600 seconds less than the expected value. The daylightOffset is 0. How can I correctly get the daylight offset for the timezone that the user is in? the NSDateFormatter parses the date correctly, so it must know something that NSTimeZone is not telling me.
Thank you!
If you want to determine the number of seconds since midnight, you can use date components. This code adjusts appropriately for daylight savings.