Possible Bug: Time Conversion With ICUDateTimeFor Token Returns Incorrect Results

Tagging @peternlewis for what may be a bug in the %ICUDateTimeFor% token’s calculation.

Peter, when calculating the date/time for January 1, 2024 minus 86400 seconds (the equivalent of one day), the result is December 31, 2024. Likely, there is something fundamental I am simply not understanding about this, but it’s had me scratching my head for nearly an hour. Is this perhaps a bug? Should the result not be December 31, 2023? Or is there something wrong with my action(s)?

Attached is a simple macro demonstrating my issue. Any help is appreciated!

-Chris

Download Macro(s): Unix time conversion bug.kmmacros (4.7 KB)

Macro Image (Click to expand/collapse)

Macro Notes (Click to expand/collapse)
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
  • The user must ensure the macro is enabled.
  • The user must also ensure the macro's parent macro-group is enabled.
System Information (Click to expand/collapse)
  • macOS 13.6.3
  • Keyboard Maestro v11.0.2

I've tested this with a few more 1/1 dates and it seems to be a problem that recurs if and only if the year is a leap year.
I'm in Chicago fwiw

1 Like

FWIW in London I am not seeing the discrepancy which you describe:

1 Like

Works fine for me:

So presumably this is either a system bug with selecting the date or some sort of date formatting bug in the time zone.

Coming on the heals of this topic:

It does make me wonder if there are some bugs in Apple’s date formatting or calculation code (or potentially Keyboard Maestro’s code, but I don't immediately see how.

But this is very similar one day (technically 86400 seconds) change to the time resulting in incorrect dates.

1 Like

Perhaps it's seen only when the time zone has a negative value:
GMT -N to the west of Greenwich vs GMT +N in the Greenwich zone and to its east ?

@rolian Interesting! At the very least this proves I’m not completely insane and imagining things. Also, I’m just down the road from you near Cleveland.

@peternlewis Thanks for the link. I’ve been somewhat absent from the forum the last month or two and had not seen that post. I’ll look through it.

@ComplexPoint Interesting theory, perhaps if more people chime in and can test, we can see if that might be the case.

I see the same issue that @cdthomer sees, and I'm on the west coast of the USA.

-rob.

1 Like

The issue does not seem to be present here, GMT +1.

And the bug does not seem to occur whatever timezone, east or west of GMT, I set my computers Date & Time settings to. But maybe this timezone setting is operating on some kind of superficial display level, and this bug, whatever it is, is happening at a deeper level?

In Arizona - I see the bug.

In San Francisco, running your macro reproduces the bug. But after changing your macro's year from YYYY to y:

%ICUDateTimeFor%debug__datePrefix-86400%y-MM-dd%

I get the correct result:

The ICU documentation on Formatting Dates and Times shows year formats as lowercase. And indeed, changing your YYYY to yyyy also returns the correct result.

4 Likes

This is fascinating, thanks for sharing that! Since the Dates and Times wiki entry doesn’t specify any difference between yyyy and YYYY, perhaps @peternlewis can shed light on the issue here.

FWIW, the following screenshot shows the different results using various tokens and formats. The lowercase yyyy returns the correct year, whereas the uppercase YYYY does not.

Variable Inspector Screenshot (click to expand/collapse)

As an aside, I marked @mrpasini’s comment as the solution, since it solved the issue with my macro. Still interested in hearing more about the difference between yyyy and YYYY for anybody who is more knowledgeable on the subject.

UPDATE: From a Google search, it appears that yyyy corresponds to the calendar year, and YYYY corresponds to the year of the week. :exploding_head:

3 Likes

They do not mention “Y” at all because “Y” is not a commonly used one.

They instead link to Formatting Dates and Times | ICU Documentation which explains the difference and is why you are seeing the problem!).

“Y” is year of “Week of Year”

Basically, you never want that.

Sorry I didn't pick that up earlier.

1 Like

I was going to add a warning about Y to the wiki, but indeed it is right there:

⚠️ Do not use capital “Y” (year of “Week of Year”) unless you really mean it or you will get oddly strange results at the beginning/end of each year.
6 Likes

Peter, thanks so much for the info and pointing out that warning on the wiki page. I missed that completely. :man_facepalming:t2:

Oddly enough, I’ve been using KM for about 6 years now, always using Y, and this is the first time I’ve had any problems. It just goes to show I should never be overconfident about my abilities, or in this case, lack thereof. :sweat_smile:

Thanks everyone who chimed in!

2 Likes