It is currently April 16th, 2024, 7:07 pm

Time measure cannot parse timezone

Get help with creating, editing & fixing problems with skins
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Time measure cannot parse timezone

Post by mistic100 »

Hello

I guess I am doing something wrong but I cant find it.
I cannot get my time measure to parse a timestamp with a timezone (actually a time offset).

For the sake of simplicity here is an example with a static value, in fact I get the ISO date from a WebParser measure

Code: Select all

[MeasureTest1]
Measure=String
String=2019-04-10T05:03:09+00:00

[MeasureTest2]
Measure=Time
TimeStamp=[MeasureTest1]
TimeStampFormat=%Y-%m-%dT%H:%M:%S%z
Format=%H:%M
DynamicVariables=1
MeasureTest2 is supposed to output "07:03" in France (GMT+2 during summer) but I get a "Invalid TimeStampFormat: %Y-%m-%dT%H:%M:%S%z" error.

I also tried to transform "+00:00" into "+0000", although both are ISO 8601 compliant.

Without the offset and %z it's working well but it outputs "05:03" hase the parsing is done in locale zone.

Thank you.
User avatar
balala
Rainmeter Sage
Posts: 16142
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Time measure cannot parse timezone

Post by balala »

Not very sure what is your intention with the %z format code at the end of the TimeStampFormat option of the [MeasureTest2] measure, but try to remove it first: TimeStampFormat=%Y-%m-%dT%H:%M:%S.
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Time measure cannot parse timezone

Post by mistic100 »

Well my intention is to parse the time offset....

If the time offset is provided the datetime parser **should** detect that the input is zoned and make the necessary ajustements to convert it to the local timezone. At least this is how you do it in Java 8.

If I remove %z the datetime is parsed in local zone, that's not what I want.

--

Beside want I want to achieve, why do I get an error ? I think I followed the doc.
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Time measure cannot parse timezone

Post by mistic100 »

Just for reference, the same thing in Java :

Code: Select all

String input = "2019-06-10T05:03:09+00:00";

// parse input with its timezone
ZonedDateTime parsedDate = ZonedDateTime.parse(input, DateTimeFormatter.ISO_OFFSET_DATE_TIME);

// convert to local timezone 
ZonedDateTime localDate = parsedDate.withZoneSameInstant(ZoneId.systemDefault());

// format
String output = localDate.format(DateTimeFormatter.ISO_LOCAL_TIME);

Assert.equals(output, "07:03:09");
User avatar
balala
Rainmeter Sage
Posts: 16142
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Time measure cannot parse timezone

Post by balala »

mistic100 wrote: April 10th, 2019, 7:54 pm Well my intention is to parse the time offset....

If the time offset is provided the datetime parser **should** detect that the input is zoned and make the necessary ajustements to convert it to the local timezone. At least this is how you do it in Java 8.
First you have to extract the time and the offset from [MeasureTest1]. For this you have to use more String measures, each with a properly written substitution. Then you have to make the needed transformation. Problem is that this kind of transformation is easier to be made on Time measures, but it's very hard on String measures, because a Time measure has different string and numeric values, while the String measures don't have a proper numeric value, which here would be needed.
If no one here on the forum will write a proper example here on the forum, tomorrow I'll try to write one, to show you how does it work. I'm sorry, but I have to leave this for tomorrow, because here is almost midnight and I don't work anymore with this. Stay tuned, if you want...
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Time measure cannot parse timezone

Post by mistic100 »

This is what I came up to, the timezone in the input is irrelevant, it is always "+00:00", I just wanted the Time measure to detect it is parsing an UTC date and needs to make the adjustment.

Code: Select all

; input
[MeasureTest]
Measure=String
String=2019-04-10T05:03:09+00:00

; get hour part
[MeasureHourUTC]
Measure=Time
TimeStamp=[MeasureTest]
TimeStampFormat=%Y-%m-%dT%H:%M:%S
Format=%H
DynamicVariables=1

; get local zone offset (hour only)
[MeasureHourOffset]
Measure=Time
Format=%z
RegExpSubstitute=1
Substitute="^([+-][0-9]{2})[0-9]{2}$":"\1"

; add extracted hour to local zone offset (with zero padded output)
[MeasureHour]
Measure=Calc
Formula=[MeasureHourUTC][MeasureHourOffset]
DynamicVariables=1
RegExpSubstitute=1
Substitute="^([0-9]{1})$":"0\1"

; get minutes part
[MeasureMinute]
Measure=Time
TimeStamp=[MeasureTest]
TimeStampFormat=%Y-%m-%dT%H:%M:%S
Format=%M
DynamicVariables=1

; concat everything
[MeasureFinal]
Measure=String
String=[MeasureHour]:[MeasureMinute]
DynamicVariables=1
still you didn't answered the original question : why I am getting an invalid format error ?
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Time measure cannot parse timezone

Post by mistic100 »

And it looks like "%z" is totally ignoring the daylight saving, it outputs "+0100" when it should be "+0200"...
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Time measure cannot parse timezone

Post by mistic100 »

Found it : the Time measure is using std::get_time for parsing which does not support "%z"

But it uses strftime for formatting which does support "%z"

Hence the documentation is wrong saying TimeStampFormat has the same syntax as Format
I created an issue on rainmeter-docs repository
User avatar
balala
Rainmeter Sage
Posts: 16142
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Time measure cannot parse timezone

Post by balala »

Finally have you got it working as intended? Because I'm not sure...
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Time measure cannot parse timezone

Post by mistic100 »

Yes and no, it's working but it's really convoluted.
And it does not consider daylight saving (well, Europe is considering removing it anyway).