I tried like this:
// Format string for date parsing
const Char* dateFormatString = "%Y%m%d";
// Current date
maxon::LocalDateTime currentDate(maxon::LocalDateTime::GetNow());
// Get expiry date
expirationDateString = String("20191025");
maxon::LocalDateTime expirationDate = maxon::LocalDateTime::FromString(expirationDateString, dateFormatString) iferr_ignore();
expirationDate._hour = 0;
expirationDate._minute = 0;
expirationDate._second = 0;
expirationDate._daylightSavingTime = maxon::DST::AUTOMATIC;
// Convert local to universal datetime
maxon::UniversalDateTime universalCurrentDate = currentDate.ConvertToUniversalDateTime();
maxon::UniversalDateTime universalExpirationDate = expirationDate.ConvertToUniversalDateTime();
// Convert universal datetime to Unix timestamp
UInt64 currentDateStamp = universalCurrentDate.GetUnixTimestamp();
UInt64 expirationDateStamp = universalExpirationDate.GetUnixTimestamp();
// Subtract timestamps
UInt64 remainingDaysStamp = expirationDateStamp - currentDateStamp;
// Convert seconds to days
Int64 daysDifference = remainingDaysStamp / (60 * 60 * 24);
But it gives me an incredibly high value for daysDifference, even though the dates are just 4 days apart.
Printing the values to the console, these are the surprising results:
Current date LOCAL: 2019-10-29 10:51:20 (that is correct!)
Expiration date LOCAL: 2019-10-25 00:00:00 (that is what I expected, too)
Current date UNIVERSAL: 2019-10-29 09:51:20 (that is also correct)
Expiration date UNIVERSAL: 2019-10-24 22:00:00 (why 22:00 ??)
Current date UNIX: 1572342680
Expiration date UNIX: 1571954400
Days difference: 213503982334596 (why??)
How cumbersome can it be to just subtract two dates from each other?
If I just knew how to use ConvertLocalDateTimeToTM()
, the problem would've been long solved.
And why is daysDifference so large? If I calculate it myself with a pocket calculator, I get this:
(1571954400 - 1572342680) / (60 * 60 * 24) = -4,4939814815
And that is exactly why I would expect: About four and a half days.