1

I have just started using the Swift 3.0 Calendar method startOfDay. It works great for the most part, but sometimes it has been returning a time of 3am in the returned date. I am getting a productionDate from a web service, which I am converting from a Unix style string using the following method

static let dateFormatString = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"

static func dateForDateString(dateString: String) -> Date?
{
    let formatter = DateFormatter()
    formatter.dateFormat = dateFormatString

    return formatter.date(from: dateString)
}

Example date/time string

2017-03-01T09:38:05-05:00

I am then saving an integer value (timeIntervalSince1970) to Firebase, where I can see the incorrect date as 3am. Here's a snippet of code I am using to generate the Int value I am saving to Firebase

let startofDay = Calendar.current.startOfDay(for: productionDate)
let dateValue = Int(startofDay.timeIntervalSince1970)

Like I said, most of the time, the time ends up being midnight of the correct date, but when the app is fetching from the web service in the background, I occasionally see 3am pop into the Firebase database.

I know I haven't given a ton of information here. I am really hoping someone has had the same problem and figured out a way to fix it or someone can look at my code and tell me how I am being a bonehead.

Thanks!

Moebius
  • 700
  • 7
  • 25
  • 2
    You're likely mixing UTC with your current timezone. – Robert Gummesson Mar 01 '17 at 15:56
  • @Robert I am getting a time zone string value like `America/New_York` in the web service payload, so I will use this to initialize the DateFormatter. Thanks! – Moebius Mar 01 '17 at 16:09
  • Why are you trying to get the startOfDay? Better to use noon (12pm) if you need a date for calendrical calculations. Everyday has 12pm but not every day has 12am. – Leo Dabus Mar 01 '17 at 16:36
  • @LeoDabus I don't care about the time, just the date, so it really doesn't matter what I use. – Moebius Mar 01 '17 at 16:37
  • So just use 12pm https://developer.apple.com/videos/play/wwdc2013/227/ – Leo Dabus Mar 01 '17 at 16:37
  • @LeoDabus I am not doing any calculations on the date, so it doesn't matter what I use. I am simply failing to see how this will help my problem. – Moebius Mar 01 '17 at 16:38
  • @Moebius Your problem is unclear. As I said not every day starts at 12am. What's your problem if the day starts at 3am ? check the video at 44 minutes – Leo Dabus Mar 01 '17 at 16:55
  • @LeoDabus The problem is that if one entry is added to the database for 12am and another is added for 3am, I will have more than one entry for a single day in the Firebase database. Each entry is the day's production value, so it is expected that there is only one entry for a single day in the DB. – Moebius Mar 01 '17 at 17:00
  • If you add all of them using 12pm as I said you won't have any problems. http://stackoverflow.com/a/37759264/2303865 you can use the extension from this answer to get noon time for any date – Leo Dabus Mar 01 '17 at 17:03
  • @LeoDabus All I want to know here is why startOfDay could be returning 3am. I appreciate your comments, but they are not helping me get to that answer. – Moebius Mar 01 '17 at 17:05
  • Daylight savings time transition – Leo Dabus Mar 01 '17 at 17:06

1 Answers1

-3

I finally was able to reproduce this issue and it has nothing to do with startOfDay returning a 3am time. I am posting another question to try to figure out the answer to the real issue.

Moebius
  • 700
  • 7
  • 25