I want to write a helper shortcut that decides what part of the day it is (morning, afternoon, night). I want to start off with a dictionary that holds the start and end hours of each part so it can easily be tweaked later on.
I already have a simple helper shortcut that gets the hour of the date I add as input and I know how to repeat over the dictionary keys. But I am running into trouble testing if the hour is between the start and end of the date.
This is what I have so far:
create dictionary â> comes down to:
{
âMorningâ: { âstartâ: 6, âendâ: 12 },
âAfternoonâ: { âstartâ:12, âendâ: 18 },
etc.
}
run shortcut âget hour of dateâ with current date as input
get all keys of dictionary
repeat with each key
get value of âstartâ of repeat item
get value of âendâ of repeat item
if shortcut result âŚ
Here Iâm running into trouble: I cannot choose something like âbetweenâ or âis greater thanâ, only text based comparisons.
Also, I wonder if this is the most efficient approach. I have looked for a similar shortcut in the forums but I cannot find one.
Wouldnât it be easier to turn the dictionary around, using hours as the keys and âMorning,â âAfternoon,â âEvening,â etc., as the values? Then you just pull the hour value out of the time and look up the part of day in the dictionary. I realize this makes for a bulkier dictionary, what with all the repetition of values, but the logic becomes much simpler, with no looping or nested conditionals.
And for those of us who use a 12-hour clock, the keys would have to include AM/PM.
This approach specifically caters for scenarios that are not based on hours but down to the minute. Thatâs why I specified the evening to night switch occurring at 21:30 as a way to illustrate this.
If, and only if, you only wanted to cater for hours, then I agree, a dictionary of 24 entries would be easier to specify and simpler to process. But I donât think the Shortcut above is overly onerous to set for your own periods.
Another approach would be to get the difference between the current time and 12:00 AM. This would give a numerical value in minutes. Then it is a simple comparison. A value less than 360 (6:00 AM) is night and so forth.
If you take a look at the details of the Shortcut, youâll see that it is doing something very similar. It is removing the colon in the time to get a numeric value rather than calculating an offset in minutes against a time to get a value.
Just uses a text action to specify the parts of day (appreciate the original post called for a dictionary, but I think this makes the shortcut simpler)
Only specifies the start of each part of day as the end is implicit
Uses the built in date comparison functionality
All the times can be in 12 or 24 hour format depending on your preference.
Not suggesting this is better than the solution posted by @sylumer, just a slightly different take on the problem.
New here but I was wondering why no one converted the date to 24 hour format and then just used hour markers in simple if statements to define the part of the day.
An approach based on If statements and hours would be fine in many circumstances and as youâve illustrated would be shorter and simpler. The downside is that is hard coding the definition of the parts of the day and relying on these falling on hour boundaries.
The original post here was looking to be able to flexibly define/change the number of parts of the day and their time boundaries in a dictionary without having to amend the rest of the shortcut.
Later in the thread the requirement was added for the parts of the day to be defined on minute boundaries, not just hours.
This drove the dictionary / loop based approaches suggested by @sylumer and myself.
Ah. Got it. I found the thread because I was searching for a way to do this myself. I understand the additional requirements now driving those solutions.