1

2014/03/30 02:00 - > 02:59

I am parsing a CSV to train a basic neural net. Every row corresponds to one minute. The code has no trouble parsing the rest of 2014 but that one hour results in a nil value. There is no difference in notation.

I also tried in a playground. Same problem. Just paste the code below in a playground and change the hour or the day.

var dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"

let dateString : String = "2014/03/30 02:53"


if dateFormatter.dateFromString(dateString) == nil {

    print("bad date \(dateString)")


} else {
    let date : NSDate = dateFormatter.dateFromString(dateString)!
    let newDateString = dateFormatter.stringFromDate(date)


}

2014/03/30 02:55,1017,0,1.917,70.8,5.074,99,0,13.84

2014/03/30 02:56,1017,0,2.146,65.26,5.036,99,0,13.84

2014/03/30 02:57,1017,0,1.313,71.2,5.033,99,0,13.84

2014/03/30 02:58,1017,0,1.292,74.9,5.047,99,0,13.84

2014/03/30 02:59,1017,0,2.125,68.76,5.039,99,0,13.83

2014/03/30 03:00,1017,0,1.438,61.02,5.011,99,0,13.84

2014/03/30 03:01,1017,0,2.333,66.73,4.981,99,0,13.84

Working Code:

UTC timezone has no Daylight Savings

var dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")

dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"

let dateString : String = "2014/03/30 02:53"


if dateFormatter.dateFromString(dateString) == nil {

    print("bad date \(dateString)")


} else {
    let date : NSDate = dateFormatter.dateFromString(dateString)!
    let newDateString = dateFormatter.stringFromDate(date)


}
R Menke
  • 8,183
  • 4
  • 35
  • 63
  • 1
    Which timezone are you in? My guess is that this is where the clocks are adjusted one hour forward for Daylight Saving time, which means that there is no "02:00" on that day. – Martin R Aug 09 '15 at 11:17
  • I was wondering about that too. I did't know how it handled Daylight Saving time. Now I do.... I'll change the local and try again. Also your comment reads more like an answer than the first answer. If you post it as such I can accept it! – R Menke Aug 09 '15 at 11:19
  • Similar: http://stackoverflow.com/questions/22272089/ios-nsdateformatter-datefromstring-returns-nil-only-in-one-instance, http://stackoverflow.com/questions/12922645/why-does-nsdateformatter-return-nil-date-for-these-4-time-zones, http://stackoverflow.com/questions/23833038/a-bug-nsdateformatter-datefromstring-returns-null-only-for-this-date-string. – Martin R Aug 09 '15 at 11:44

1 Answers1

3

You can't get dateFromString because this specific hour has never exist. On 2014/03/30 due to changing daylight saving time the time 2:00 - 2:59 has just been skipped in your time zone.

vadian
  • 274,689
  • 30
  • 353
  • 361