I had a macro that gave me the ordinal number of the day-of-year. I wanted to improve that to give me the percentage of the year remaining. I found nothing in the forum.

I tried calculating this from the cardinal day number and the number of days in the year (because I want it to be accurate even on leap years), but while trouble-shooting using a variable as numerical input (this is, it turns out, tricky for a novice) I stumbled on a thread in which Peter supplied an example using the Action "Set Variable to Calculation" and thought I saw a spark in there with which I could bake my bun. Sweet aroma — it worked!

(I set it up initially to calculate percentage of year elapsed, then changed it to calculate percentage of year remaining by subtracting this from 1.)

I have some questions.

Can this be bettered, other than by calculating the days from now to the end of year instead of from the beginning of the year and subtracting from 1?

Is this accurate? I tested it manually for the first and last days of the year; it seems to be accurate.

Will I have any issues with time zone? Afaict, this works with local time and saves me the effort of working in milliseconds and GMT and offsets.

This is just for my information — I want it to be right, but it doesn't have to be accurate to the day (though I did check how it rounds to whole numbers).

And also: I overwrote my original macro. What is the easiest way to retrieve it from a backup? (I tried duplicating the Macro and renaming it and then undoing all the changes to the original, but the first undo action is "Undo Duplicate")

Why would that be "better" if it gives the exact same answer? But yes, I give you the formula below for the "better" approach.

Why are you passing YEAR, MONTH and DAY to the TIME function but not including HOUR, MINUTE and SECOND? You could be off by as much as one day, which is 0.3%.

In any case, I think you can do the math differently, like this:

I don't see why any time zone would return a different result from GMT. The percentage of year past and remaining should be identical for all time zones. The time zone cannot change the result.

There seems to be a second mistake in your calculation, resulting in a one day error. You are calculating from Jan 1 to Dec 31 as your divisor. You should be calculating from Jan 1 to Jan 1 of the next year. That way you include every day, rather than one day less than a year.

Note that you can get Jan 1 next year with TIME(YEAR(),12,32). Keyboard Maestro is forgiving with such things. The 32nd day of December is the first day of January.

I would add that there's a design decision to be made -- when does a day "complete"? At 12 noon on the first of January this year, did you still have 366 days left or 365? At 00:01 on the 31st December will you have 0 days left or 1?

What I can't remember is how to (or if you can) include text tokens in a calculation field, to avoid the filter step. Gurus?

I don't understand -- %ICUDateTime%D% returns "day of the year", so leap years are already accounted for and "days" is exactly the precision OP asked for!

Maybe he was referring to calculating the precise percentage of the total days in the given year, which would differ in leap year. I'm sure there are more elegant ways to get there, but FWIW this shows 12/31 of this year is the 365th day.