Skip to content

Commit

Permalink
Simplify the NSDate to Matter epoch conversion code. (#29963)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Mar 8, 2024
1 parent 7be693a commit 1188992
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/darwin/Framework/CHIP/MTRConversion.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ CHIP_ERROR SetToCATValues(NSSet<NSNumber *> * catSet, chip::CATValues & values)

bool DateToMatterEpochSeconds(NSDate * date, uint32_t & matterEpochSeconds)
{
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents * components = [calendar componentsInTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0] fromDate:date];
auto timeSinceUnixEpoch = date.timeIntervalSince1970;
if (timeSinceUnixEpoch < static_cast<NSTimeInterval>(chip::kChipEpochSecondsSinceUnixEpoch)) {
// This is a pre-Matter-epoch time, and cannot be represented in epoch-s.
return false;
}

if (!chip::CanCastTo<uint16_t>(components.year)) {
auto timeSinceMatterEpoch = timeSinceUnixEpoch - chip::kChipEpochSecondsSinceUnixEpoch;
if (timeSinceMatterEpoch > UINT32_MAX) {
// Too far into the future.
return false;
}

uint16_t year = static_cast<uint16_t>([components year]);
uint8_t month = static_cast<uint8_t>([components month]);
uint8_t day = static_cast<uint8_t>([components day]);
uint8_t hour = static_cast<uint8_t>([components hour]);
uint8_t minute = static_cast<uint8_t>([components minute]);
uint8_t second = static_cast<uint8_t>([components second]);
return chip::CalendarToChipEpochTime(year, month, day, hour, minute, second, matterEpochSeconds);
matterEpochSeconds = static_cast<uint32_t>(timeSinceMatterEpoch);
return true;
}

0 comments on commit 1188992

Please sign in to comment.