Convert ICUDateTime to Unixtime

I want to set the modification date/time of a file. I have the value formatted as yyyy-MM-dd HH:mm:ss but it seems that the KM action Set File Attribute > Set modification date is expecting unixtime.
How do I do the opposite of ICUDateTimeFor?

Try the TIME() function with the appropriate date numbers as its parameters. Here's a macro I use for my own purposes by way of example:

Change File Modification Date.kmmacros (3.3 KB)

Gabe, I think you got caught by the time zone difference between Unix time and the ICU Date/Time:


From the KM Wiki:

TIME() === NOW() === the current unix time (GMT).

This should fix it:

TIME(LocalYear, LocalMonth, LocalDay, LocalHour,LocalMinute,0) - GMTOFFSET()


This is a really tough issue to keep straight in my head when using KM date/time tools.

1 Like

Thanks for the correction, Jim! It's true, that offset has bitten me a few times, and it always seems to slip my mind the next time something like this comes up. Hopefully next time I'll get it right the first time :wink:

1 Like

Thanks Gabe and Jim, this has helped.

Since I am starting with a date/time in ISO8601 format, such as 2020-08-10 12:00:00, I have been able to use Substring to extract characters 1 to 4 into LocalYear, etc (as per Gabe's example) and then %Calculate%TIME(LocalYear,LocalMonth,LocalDay,LocalHour,LocalMinute,LocalSecond) - GMTOFFSET()% to get the UnixTime as 1597026600.

But I thought that it might be more efficient to use Search and Replace to convert the hyphens, space and colons into commas -- 2020,08,10,12,00,00, and then %Calculate%TIME(%Local_DateTime%) - GMTOFFSET()%, where %Local_DateTime% is the variable with the value 2020,08,10,12,00,00.

KM doesn't seem to like that, although %Calculate%TIME(2020,08,10,12,00,00) - GMTOFFSET()% -- where I have pasted in the character string 2020,08,10,12,00,00 -- is fine.

Is there a trick to using a variable within the TIME() function?

And a minor point:
I use three Search and Replace actions to convert the hyphens, space and colons into commas one character change at a time.
I think that I should be able to use a regular expression to target those three different characters in one action.
To save me thinking it through, can anyone tell me if there would be a suitable expression.



Sorry, AFAIK it can't be done. I ran into this earlier and IIRC @peternlewis confirmed it.

I have worked around that problem by reverting to my sub-macro where I split the ISO8601-formatted date/time into LocalYear, LocalMonth,LocalDay,... variables and returning the result of %Calculate%TIME(LocalYear,LocalMonth,LocalDay,LocalHour,LocalMinute,LocalSecond) - GMTOFFSET()% to get the UnixTime.
This works fine.

So my final action is 'Set File Attribute' to set 'Date Modified' of my Finder selection to the value of my variable %Local__Modified%, which contains the UnixTime value returned from my sub-macro.

Again, KM won't cooperate:
"Action Failed
Set File Attribute failed to determine new value from %Local__Modified% ..."

But if I replace the variable with a value such as 1597026600, the action succeeds.

Is this another case of an action not wanting to use the value held in a variable?


It's hard to say for certain without seeing your macro, but no, a variable should work fine in that action. Most likely, there's a typo or some slight difference between the name of the variable you're using to set the mod date and the variable where you're saving the Unix time.

Correct, you cannot put text tokens in a numeric field, so in a numeric field, you cannot do TIME(%Local_DateTime%). Similarly, in a text field, which you can use the %Calculate%numeric field% token, but within the numeric field part, you are effectively in a numeric field and so cannot use text tokens.

You can do this:


Note that the second action, being a text field, processes the %DateTime% token (I removed the Local so that the values show in the action). so that the Time variable is set to TIME(2020,08,10,12,00,00). And then you can use the Filter action to calculate the value stored in there.

Note that in a numeric calculation, when you reference a variable, Keyboard Maestro will actually evaluate it to determine it's value, so if it contains TIME(2020,08,10,12,00,00) you can freely use that in a numeric field contact, for example:


Note that the fields in each of these actions is a text field, not a numeric field, so each one is processing text tokens. Only the part in the middle of the last action (Time - GMTOFFSET()) is a numeric field. Keyboard Maestro retrieves the numeric value from the Time variable by evaluating TIME(2020,08,10,12,00,00).

1 Like

Thanks Peter
I hadn't been paying attention to 'Filter' because I had assumed that it meant "ignore list entries that don't meet certain criteria" but now I see that this can be very useful.
As a result, I have been able to simplify my sub-macro from eight actions to just four.

I thought that with a clearer understanding of text fields and numeric fields, and text tokens and numeric tokens, then I might be able to solve the problem of
"Action Failed
Set File Attribute failed to determine new value from %Local__Modified% in macro...".

I have been thinking that becauseimage
works but
results in the error message, then I haven't been able to make the contents of variable %Local__Modified% numeric.

I have tried naming the variable in the 'to' field as Local__Modified, %Local__Modified%, %Variable%Local__Modified% and %Calculate%Local__Modified%, but none of those variations helped.

I have preceded the action with this one: image
but that didn't help either.

I'm out of ideas because I guess that I don't understand this well enough yet.


When setting the modification date, the field is a calculation, so you use just the variable name Local__Modified which must have a numeric value. If you are editing the field, Keyboard Maestro will tell you what kind of field it is:

This is a calculation field (numeric field).

If you are getting an error, Log or Display the Local__Modified to see what it's value is.

I am currently using Local__Modified as the variable name and the small C icon is in the field.
I had a 'Display text in a window' action to show the target file name, the new creation date and new modification date.
The date values were numerals such as 1596940200 or 1597026600.
But I was displaying this just after returning from the sub-macro.

After that I had recently added a Filter action which most recently was Filter Variable Local__Modified with Value of Named Variable.
I have just moved the Display action further down in the macro and it seems that that Filter was setting the variable to empty!
Having now disabled those Filter actions. the macro is now working as expected.

I think that before I added the Filter actions, the macro wasn't working because I had references to the variables as %Local__Created% and %Local__Modified%.
Then I got that right by removing the percent characters, but I messed it up again with the Filter actions.
Now all errors seem to be removed.

Thanks again, Peter.

1 Like