Hi,
I am still trying to wrap my head around LocalDateTime and UniversalDateTime.
LocalDateTime -> UniversalDateTime : time zone offset
I have this test code:
// Format string for date parsing
const Char* dateFormatString = "%Y%m%d";
// Date string to parse
String activationDateString("20191105"); // November 5th, 2019
// Parse date from string
maxon::LocalDateTime activationDateLocal = maxon::LocalDateTime::FromString(activationDateString, dateFormatString) iferr_ignore();
// Zero out time values, as the parser gives me obscure times otherwise
activationDateLocal._hour = activationDateLocal._minute = activationDateLocal._second = 0;
// Convert activation date to UTC
maxon::UniversalDateTime activationDate = activationDateLocal.ConvertToUniversalDateTime();
// DEBUG: Print times
DiagnosticOutput("DEBUG: @", maxon::String("Activation date LOCAL: ") + activationDateLocal.ToString(nullptr));
DiagnosticOutput("DEBUG: @", maxon::String("Activation date UTC (converted from local): ") + activationDate.ToString(nullptr));
This is the readout:
DEBUG: Activation date LOCAL: 2019-11-05 00:00:00
DEBUG: Activation date UTC (converted from local): 2019-11-04 22:00:00
So far so good. I'm in Germany, so my time zone is +2 hours. But WAIT a second... we're on winter time currently, so shouldn't it rather be 1 hour difference? Let's see what the time zone difference is:
DiagnosticOutput("DEBUG: @", maxon::String("Activation date LOCAL: ") + activationDateLocal.ToString(nullptr));
DiagnosticOutput("DEBUG: @", maxon::String("Local time zone offset: ") + maxon::String::FloatToString(activationDateLocal.GetTimezoneOffset().GetHours()));
DiagnosticOutput("DEBUG: @", maxon::String("Activation date UTC (converted from local): ") + activationDate.ToString(nullptr));
And here comes a surprise:
DEBUG: Activation date LOCAL: 2019-11-05 00:00:00
DEBUG: Local time zone offset: 1
DEBUG: Activation date UTC (converted from local): 2019-11-04 23:00:00
My time zone difference is in deed 1 hour, as I expected. But suddenly, the converted UTC is different from before (was: 22:00; is: 23:00)! Keep in mind, the only thing I changed in the code is that I added the second DiagnosticOutput()
line.
Does LocalDateTime::GetTimezoneOffset()
change values in the LocalDateTime object?? Is that why it's not const? Why do the SDK docs not mention this?
The only thing I want is:
- Get the current date
- Parse another date from a string
- Comparing both dates to get their difference in days (I know how to do that already). Exact times are to be neglected, but of course time zones still play a role to get the correct date.
FromString() default result values
Also, on a general note: If I use LocalDateTime::FromString()
to parse a date from string, why are the time values not at all initialized?
// Format string for date parsing
const Char* dateFormatString = "%Y%m%d";
// Date string to parse
String activationDateString("20191105"); // November 5th, 2019
// Parse date from string
maxon::LocalDateTime activationDateLocal = maxon::LocalDateTime::FromString(activationDateString, dateFormatString) iferr_ignore();
// DEBUG: Print times
DiagnosticOutput("DEBUG: @", maxon::String("Activation date LOCAL: ") + activationDateLocal.ToString(nullptr));
The readout is:
DEBUG: Activation date LOCAL: 2019-11-05 128:254:144
Why are the time values not zero'ed, or set to the current time, or anything that would make sense?
Greetings,
Frank