Why is the time before 1892 printed incorrectly in Swift? - 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.

Related

millisecondSinceEpoch returning weird value when parsing date around 1969

I don't know if this is a bug or if I'm using this method incorrectly.
Can someone explain to me why I got a weird value when parsing this date?
This is the minimal script:
void main(){
DateTime zero = DateTime.fromMillisecondsSinceEpoch(0);
print(zero.millisecondsSinceEpoch);
DateTime errorDate2 = DateTime(1969,1,1);
print(errorDate2.millisecondsSinceEpoch);
DateTime errorDate = DateTime(1969,10,2);
print(errorDate.millisecondsSinceEpoch);
}
Based on my sample, the result seems fine when I parse dates bigger than 0 milliseconds since the epoch.
But it gets weird when I parse some date less than 0 milliseconds since the epoch.
From that script, I got this output
0
-31536000000
-7862400000
I parse that output to this website.
In the first and second results, the results are fine, but in the third result, I got a date around 1720.
Can someone explain to me what happens to that function? Did I use it wrong?
What should I do when I want to parse a date less than 0 milliseconds since the epoch?
In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system. Most versions of Unix, for example, use January 1, 1970 as the epoch date; Windows uses January 1, 1601; Macintosh systems use January 1, 1904, and Digital Equipment Corporation's Virtual Memory System (VMS) uses November 17, 1858.
So here epoch time starts on January 1, 1970, so it will give you a minus value for milliseconds.
You can still parse the date. it will gave just minus value nothing else.
void main() {
DateTime zero = DateTime.fromMillisecondsSinceEpoch(0);
print(zero.millisecondsSinceEpoch);
DateTime errorDate2 = DateTime(1969,1,1);
print(errorDate2.millisecondsSinceEpoch);
DateTime errorDate = DateTime(1969,10,2);
print(errorDate.millisecondsSinceEpoch);
DateTime error3 = DateTime.fromMillisecondsSinceEpoch(errorDate.millisecondsSinceEpoch);
print(error3);
}
Result is
0
-31555800000
-7882200000
1969-10-02 00:00:00.000
It seems that the bug comes from the website that I refer to it.
Thanks! #jamesdlin

Bootstrap datetime picker does not validate hour & minute together when incrementing hours or minutes

I am using the bootstrap datetime picker.
I have set enabledHours between 8 am to 5 pm and stepping to 30. When i pick the current hour to be 5 pm and increment the minute by one step,the result is a invalid date(5:30 pm). The expected result is not to allow incrementing the time, as it produces invalid date.
Same goes for hours also. E.g. if I pick the time as 4:30 pm and try to increment the hour by one step, it produces 5:30 pm which is not valid according to the enabled hours.
Any workaround for this issue?
I found my answer here: https://stackoverflow.com/a/31950948/495000
Turns out the trick is to use the disabledTimeIntervals option instead of EnabledHours.
Note that disabledTimeIntervals takes an array of arrays - representing a list of disabled ranges.
For example, I needed the following, which disables the times between 12:00 AM to 06:59 AM, and between 6:01 PM and 11:59 PM (technically 12:AM the next day the way it's written...). If you consider the inverse, this means I'm enabling from exactly 7:00 AM to exactly 6:00 PM.
.datetimepicker({
format: 'hh:mm A',
stepping: 15,
disabledTimeIntervals: [
[moment().hour(0).minutes(0), moment().hour(6).minutes(59)],
[moment().hour(18).minutes(1), moment().hour(24).minutes(0)]
]

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

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.

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]