Date Time Conversions

I have a datafile with a date in STRING format, and I need to convert it to a DATE object

let dateFormatter        = DateFormatter()
dateFormatter.locale     = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX dateFormatter.dateFormat = "MM-dd-yyyy HH:mm a"
let date                 = dateFormatter.date(from:v[2])!
let timeZoneOffset       = Double(TimeZone.current.secondsFromGMT(for: date))
let localDate            = Calendar.current.date(byAdding: .second, value: Int(timeZoneOffset), to: date)!

print("Input: \(v[2]) Date: \(date) Local: \(localDate) = \(timeZoneOffset)")

here is what I get

Input:  08-22-2024 11:51 AM 
 Date:  2024-08-22 07:51:00 +0000 
Local:  2024-08-22 00:51:00 +0000
  TZ : -25200.0 [7 hours]

the first conversion is off by 4 hours, the seond is off by 11 hours
even though the timezone offset is only 7 hours

I need the date object to reflect EXACTLY what the string is, regardless of TZ

the original has no TZ info ?
I’m assuming it’s “local time” ? (PST / PDT ?) which is a pain in the rear since you have to look at the original date to know if its PST / PDT and adjust accordingly (6 or 7 hours)

Not sure why the first one is off by 4 hours … that one is confusing
Almost like en_US_POSIX specifies a particular eastern tz offset (like EDT ?)

local offset from that makes sense
Do subtraction, not addition, since your behind GMT

let dateFormatter        = DateFormatter()
dateFormatter.locale     = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX dateFormatter.dateFormat = "MM-dd-yyyy HH:mm a"
dateFormatter.dateFormat = "MM-dd-yyyy hh:mm a"
dateFormatter.timeZone   = TimeZone.current
let date                 = dateFormatter.date(from:v[2])!
let timeZoneOffset       = Double(TimeZone.current.secondsFromGMT(for: date))
let localDate            = Calendar.current.date(byAdding: .second, value: Int(timeZoneOffset), to: date)!