Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the NSDate to Matter epoch conversion code. #29963

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}