Skip to content

Commit

Permalink
Merge pull request #348 from paulplant/develop
Browse files Browse the repository at this point in the history
fix 12hr/timezone clock error
  • Loading branch information
JohanDegraeve authored May 6, 2022
2 parents 9cbbe35 + 342e9d6 commit c51b3a1
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions xdrip/Treatments/TreatmentNSResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,29 @@ public struct TreatmentNSResponse {

var treatmentNSResponses: [TreatmentNSResponse] = []

// we need this to be optional in case created_at cannot be transformed successfully into a date
// we need this to be optional in case the created_at string cannot be transformed successfully into a valid date
var nightscoutDate: Date?

if let createdAt = dictionary["created_at"] as? String {

let dateFormatter = DateFormatter()

// let's check which date format is stored in Nightscout and deal with it accordingly
// add the locale and timeZone properties as per dateFromISOString()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")

// let's check which date format is return by Nightscout and deal with it accordingly
// if the date string contains a decimal point, then it must contain milliseconds
// if we don't take this into account, .date(from: string) could be returned as nil
// if we don't take this into account, .date(from: string) will be returned as nil if the milliseconds (.SSS) are missing
if createdAt.contains(".") {

// this is the way Loop, FreeAPS (Loop), OpenAPS and FreeAPS X store the created_at date/time
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SZ"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

} else {

// and AndroidAPS stores it without milliseconds
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"

}

Expand Down

0 comments on commit c51b3a1

Please sign in to comment.