iPhone Datepicker.date is 4 hours and 56 minutes fast, help? - iphone

Hey all so as the title suggest I just have a date picker and it appears to be 4 hours and 56 minutes fast.. which is very strange. Code is very straight foreword:
NSLog(#"%#",datePicker.date);
In the view did load
datePicker.timeZone = [NSTimeZone localTimeZone];
Any ideas/suggestions?
ETA: for example if I set the time as 4 00 PM I get this in the NSLog
0001-01-01 20:56:02 +0000

Set minimumDate and maximumDate on your date picker to something sane.
Dates before October 1582 tend to have numerous issues in iOS, due to some things recognizing the Julian/Gregorian calendar transition and other things not. There also seems to be accuracy issues in the times when you deal NSDates near the year 1.

Related

Why is the time before 1892 printed incorrectly in Swift?

I came across a weird date behaviour in Swift.
If I try to print any date before 1891, the minutes and seconds will be printed incorrectly with offset by 2 minutes and 16 seconds like this:
Code Sample:
// incorrectly printed date
let year1981 = Calendar.current.date(from: .init(year: 1891, hour: 9))
print(year1981!) // will print 1891-01-01 08:02:16 +0000
// correctly printed date
let year1982 = Calendar.current.date(from: .init(year: 1892, hour: 9))
print(year1982!) // will print 1892-01-01 08:00:00 +0000
Is there any rational reason for this behaviour or it is a bug? Thanks for any reply!
Tested in Xcode Playground 14.1
Based on the comments you are in the "Europe/Prague" timezone.
According to this website information:
When local standard time was about to reach
Thursday, October 1, 1891, 12:00:00 midnight clocks were turned forward 0:02:16 hours to
Thursday, October 1, 1891, 12:02:16 am local standard time instead.
This change would explain the results you are seeing when converting such a date.
So it is not a bug. It's just one of many examples of how complicated Date and Calendar code is due to all of the obscure details of time zones and day light saving time can be.

iPhone iOS 4 UITimePicker force 24 hour view

I got a UITimePicker in my app, and I want to use it to select a certain number of hours and minutes after a user event. (for example 3 hours 15 minutes after 7PM) This is why I want to show the date in 24 hour format, otherwise the user might get confused.
Is there a way to force the timepicker to show 24 hour view, independent of the clock format for the locale? Or do I just pick another locale?
UIDatePicker *timePicker;
NSDateFormatter *timePickerFormatter;
[self.view addSubview:timePicker];
float tempHeight = timePicker.frame.size.height;
timePicker.frame = CGRectMake(0,150 ,self.view.frame.size.width , tempHeight);
timePicker.hidden = YES;
timePickerFormatter= [[NSDateFormatter alloc]init ];
[timePickerFormatter setDateFormat:#"HH:mm"]; //formats date for processing
Thank you!
Update: I found the answer like this:
timepicker.datePickerMode = UIDatePickerModeCountDownTimer;
This effectively accomplishes what I'm trying to do by displaying 0 to 23 hours and 0 to 59 minutes, no further customization required! Sorry for asking about the 24 hour format, this is what the Android app that I'm porting over was using!
Did you try to use the calendar and locale properties of UIDatePicker?
That should be the solution for your problem, because as the documentation explains (read the UIDatePicker class reference):
UIDatePickerModeTime
The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].
[EDIT] after further investigation, it seems the NSLocale can't override the AM/PM setting… (I had that information by… simply searching here in Stackoverflow! And your question has already been asked and answered many times before)
For visualization I use:
[self.datePickerView setLocale:[NSLocale localeWithLocaleIdentifier:#"ru_RU"]];
You may use your own locale.
And for NSDateFormatter I Use:
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
It is no longer breaks my applications.

NSDateFormatter giving the wrong Hour (+8 hours)

I don't undersatnd why I get 8:00AM instead of 00. This also happens if I set it to other hours.
Ok, I now tryed to present in a label the value of
[dateFormatter stringFromDate:dateTest];
And in a wierd way it presented the time I formatted... But if I check the NSDate object on a breakpoint then it says 08:00:00 and not 00:00:00;
My best guess that you live in the States, which uses GMT -8 time zone at the date specified (maybe PST?). So the date has been parse correctly already, because it said it was GMT.
The dateFormat string is wrong. It should be
"yyyy-MM-dd-HH-mm-ss"
See http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns.
The main problems are
DD is the day of year, not day of month. So 07 will override the month "November" set previously.
hh can only recognize numbers in the range [1, 12], which 0 is out of range.

Convert UnixTimeStamp to NSDate with Day light saving time

I have a GMT timestamp "1273051632" where GMT offset is -5.I want to convert this into local time with day light saving time in my iPhone application. Converting it using NSdateFormatter gives me
1273051632 translates to Wednesday, May 5th 2010, 04:27:12 (GMT -5)
This is not adding day light saving time...
(The correct result should be Wednesday, May 5th 2010, 05:27:12 (GMT -5)) , right ?
Please help me how to convert above Unixtimestamp to NSString accounting day light saving time .Thanks in advance.
how about [NSDate dateWithTimeIntervalSince1970:seconds]

Doing comparison on NSDates in different timezones?

I have an array of NSDates which I build from strings using [NSDate dateFromString]
In the xml I parsed to get the string there was also a timezone string. As far as I can see in the manual NSDate does not in it self deal with timezones. Do I need to always store this timezone value somewhere and pair it with the belonging NSDate each time I need it?
I also need to figure out that if an event starts in London at 10:00, but I am in Denmark having my iPhone set to danish time my "event started in London" should display at 09:00 o'clock.
Again if an event starts in London at 10:00 o'clock and ends in Denmark at 12:00 o'clock, If I were to compare start time and end time using an iPhone with danish settings I would get that the duration of the event was 02:00 event though 10:00 o'clock in UK and 12:00 o'clock in Denmark is only 1 hour apart.
NSdate works really well for these things in the scope of one timezone, but introducing the timezone part just made everything complicated to me. Is there a way to abstract/hide all these calculations, as I see potential for making a lot of mistakes.
I have been through the NSDateformatter and NSDate guides from Apple, but they are really vague and sports a substantial amount of deprecated code :/
Thanks for any help given.
You should take one standard timezone like UTC/GMT format for all calculation.
According to the NSDate reference, dateWithString: takes an offset to GMT as last component; while it is not a time zone, it is sufficient to perform computation or comparison).
Looking at the NSTimeZone reference, you can use the abbreviationForDate: and the timeZoneWithAbbreviation: to get a NSTimeZone object from a NSDate instance. Once you get the time zone, you have everything you need.
I convert the present date and the date I would like to know if is close, to GMT and then returning the difference. So I changed every thing to deal with differences instead of actual times and dates. A bit like a music score transposed to a different key:)
+ (NSInteger) minutesUntilDate:(NSDate*) date withTimezoneOffset:(NSInteger) GMTOffset
{
NSDate *now = [NSDate date];
NSTimeInterval localTimeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
now = [now addTimeInterval:(localTimeZoneOffset * -1)];
date = [date addTimeInterval:(GMTOffset * 60 * 60) * −1];
return ((NSInteger)[now timeIntervalSinceDate:date] / 60 ) * -1;
}
As soon as you have allocated an NSDate, these do not have timezone information any longer. NSDate is "timezone-less" and is always in GMT. You should make sure that NSDate understand your format correctly when allocating it.
Once you have an NSDate you can make normal calculations and ignore the timezones.
You only need to take care of timezones when reading strings into NSDates and when printing them out.