Convert string to time is bugged at noon & midnight

fixed

#1

I am working on a piston converting a local string variable to a global time variable…
and it works correctly for all times except in the noon and midnight hour.

Here is a working example:
ie: (expression) string(time('11:09 pm')) »»» (string) 11:09:00 PM CST
(notice both my input and the output are PM)

The bug happens when doing this on any time that is in the 12 o’clock hour:
ie: (expression) string(time('12:09 pm')) »»» (string) 12:09:00 AM CST
ie: (expression) string(time('12:09 am')) »»» (string) 12:09:00 PM CST
(Notice the AM & PM got switched in the output.)

Is there an easy solution for this?

Thanks in advance


#2

I agree that there seems to be an issue, but you don’t need to use the Time or String function. You can just save the string as a time variable if you use the expression. It worked for me, except at midnight and noon.

This results in the variable @time being set to 11:07:00 am.

But yes, it doesn’t work at noon or midnight. There are several workarounds. You could use military time. 00:00 seems to work fine for midnight.


#3

This time variable is going to be changing twice a day (moon rise and set times) so statistically, every 12 days or so, one of the times will fall into the 12:00-12:59am or 12:00-12:59pm time brackets. If no one comes up with a solution soon, I may modify my PHP to return military time. My moon piston is already 21-22 chunks, so I am limited as to how much new code I can add to my piston


#4

Yeah…i see what you mean. Well, you could do a pattern match and replace. Not exactly this but something similar.
EDIT…new image:

That function returns midnight for 12:00 am.


#5

I’ve tested this and there does indeed appear to be a bug here.

12:09 AM returns 12:09:00 (instead of 00:09:00)

12:09 PM returns 00:09:00 (instead of 12:09:00)


#7

Is that a 5 second solution on your end Robin, or would that be something resolved in a few weeks at the next update? (Just trying to decide if I need to create a work-around now or not)


#8

Looks like the bug is in line 7456 of WebCoRE Piston:

if (dateOrTimeOrString.trim().toLowerCase().endsWith('pm')) time += 43200000

This needs to be smarter and deal with the case where the hour is 12… (this, among others, is why I will always stick to 24h time :wink:)


#9

Good catch eagle eye!!

I will change my PHP file to return time in 24h to bypass the issue for now.


#19

This should be fixed in today’s v0.3.104.20180323 release which will require updating your smartapps at ide.smartthings.com and possibly hard refreshing the dashboard.