NSDate Daylight time saving - ios

I need to know if we can determine that a particular date had Daylighttime saving ON or OFF.
For eg. In Germany , Daylighttime saving was not there from 1950-1979. Can I determine this programmatically in iOS if these days did not have Daylighttime saving?

You can pick a date and test it. The NSDate accounts for daylight savings.

isDaylightSavingTimeForDate: returns YES if daylight savings time was in effect at that moment in time. If you want to check whether it was in effect in a certain country in a certain year, create an NSDate on 1st of July of that year, and an NSDate on 1st of January of that year, and if neither date had daylight savings time on, then the country didn't have it at all.
(You need to check a date in January as well because Australia, New Zealand etc. have daylight savings time in Summer = January).
iOS is quite good with that kind of historical knowledge. Actually, there are quite a few posts from time to time where iOS has some bizarre but correct information about a timezone and people think it's a bug.

Related

Best way to store the day of the week using NSDate and NSDateFormatter

I'm trying to reproduce some of the functionality of the default Clock where I let users select a repeat frequency for an alarm. The problem is that different NSCalendar settings will give you different names for the days of the week. How do I store the selected days of the week in such a way that if the user changes their calendar the frequency always falls on the right day of the week?
One solution would be to check if the calendar has changed every time the app comes into foreground, and if so, make appropriate changes from there. Probably not the most elegant solution, nor the best practice, but it could get the job done.
Take (long) [dateYouWantToRemember timeIntervalSince1970] and store that in a property file or some such. (You could use timeIntervalSinceReferenceDate, but the 1970 number is the "UNIX epoch date" and more standardized -- you can find converters online to check it.)
Later, under the (potentially) different calendar, do [NSDate dateWithTimeIntervalSince1970:theLongYouStored] and you will reconstruct the date, in the new calendar.
Now use NSCalendar/NSDateComponents to find out the new ordinal day of week of that date, and use [NSDateFormatter weekdaySymbols] to fetch a list of the weekday names. Index the list with the ordinal to fetch the new day name. Use NSDateComponents to do arithmetic on you dates to select the next date that is that day of the week, as needed for your app.

Can any one justify this two NSDate?

Why are these two fireDate different?
The date displayed in the log is wrong.
The date logged to console is correct, considering that there is no time zone offset (i.e., displayed for UTC). The date displayed in the quick look popover is adjusted for India Standard Time (which is offset +5:30 from UTC).
You are from INDIA I guess and india Time zone is +5.30.
but in Log it showing +000 GMT right.
So if you add +5.30 in your Log Time Then You will get correct time which you can see during debugging.

Timezone in the future

I'm working on an app that can book services in later dates in many timezones.
My problem is that when i'm getting timezone for one country, i got this timezone for now.
But this week-end we will change our timezone, and Paris (Actually +1) we'll become +2.
How can I get this timezone for a date like tuesday programmatically ?
You can use various methods.
One would be to use
- (NSInteger)secondsFromGMTForDate:(NSDate *)aDate
of NSTimeZone and get the GMT offset for a certain date and a certain timezone.
Or you can use
– daylightSavingTimeOffsetForDate:
to check wether there needs a DST offset to be accounted for.

Compare iOS Time Zone Rules

If the user is in Eastern time, I don't want to display "EST" or "EDT" but if they are not, I do want to display it.
Is there an easy way to compare TimeZone rules in iOS?
[NSTimeZone localTimeZone] returns America/Indianapolis, but I just want to know if it is eastern.
Comparing data does not work, do I just compare secondsFromGMT?
EDIT
Comparing offsets does seem to work, but I don't know if it is going to cause problems later?
You can use [[NSTimeZone localTimeZone] abbreviation]; to get the abbreviation of whatever time zone the user is in.
This gives the abbreviation of the current date (EDT if not daylight savings and EST if daylight savings, in your case). If you want the abbreviation of a specific date, you can use abbreviationForDate: and insert any date.
Here is some more information about NSTimeZone
Edit:
If you want to actually compare multiple time zones (as in to check if it is the current time zone or not), you can use isEqualToTimeZone:. If that does not fit your needs, look at some of the other NSTimeZone methods in that link.
I've had lots of pain from dealing with time zone issues on our tools app, that includes event scheduling across time zones. The time zone names have not been friendly to use. Also, Daylight savings time is a great big pain.
What has worked consistently for me is calls like this using timeZoneForSecondsFromGMT
I also convert my stored dates to GMT, then I can set them to the local time on display.
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger timeZoneOffset = [zone secondsFromGMT];
[self.dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:timeZoneOffset]];

Rails timezone and Daylight saving time

for a while I´m trying to understand how this timezone times will work, and I had a question:
Today in my country, we are in Daylight saving time (GMT-2).
So the user of my application enter a time, like 11:00AM and post the form.
Far as I know, rails will convert this date to UTC and save in the database (mysql in my case), like: 01:00PM UTC.
When I recover that record, I had to convert to local time to display. Ok?
My question is, lets suppose that this date/time represents a date/time in future, when my country gets out from summer time (GMT-3). Rails will save 01:00PM UTC? Today, in Daylight saving time, how will be the local time? And in the future, how will be this local time?
Basically, I always need to display to user 11:00AM.
thanks.
There are several places where timezone can come into play: the operating system (or probably user account) default setting, the database server, Rails environment.rb.
The key is to make sure all dates are stored with UTC time zone, then displayed in whatever your local timezone is. It sounds like you're doing that.
So your question seems to boil down to "if it's Daylight time, I want to offset by -3 hours, else offset by -2 hours". The Rails time extensions let you determine your current offset like Time.zone.now.utc_offset, and Time#dst? tells you if it's Daylight Savings Time with those two you can conditionally subtract the extra hour (3600 hundred seconds).
7 months after you asked, but perhaps skip_time_zone_conversion_for_attributes= will help - it tells AcitveRecord not to convert timezones on storage or retrieval. See ActiveRecord Timestamp which shows the example:
class Topic < ActiveRecord::Base
self.skip_time_zone_conversion_for_attributes = [:written_on]
end

Resources