Time Of Day - allowing for comparisons

I use the design pattern for time of day by @rlkoshak - Design Pattern: Time Of Day

It does work well for most situations…but one place we I think it lets me down is in being able to compare. Let say I have an EVENING and NIGHT ToD values. Quite often for something to take effect in either of these times (normally theres other conditions as well), you can to test for both.

This is because you can’t test for current time >= EVENING, given that it is a string item.

So I was thinking of pegging it to a numerical value…of cource I have one that increaments all day anyway, being the astro sun or moon value.

I’ll need some sort of array of values, where I can tie a label eg EVENING to the current astro value (would be sun.position).

Or Using a 4 digit time pegged to a label is another idea, but it would have to be updated for things relating to sunrise/sunset (ie for lights)

The flaws I see are time based things, ie somthing needing to go off at 6pm - where in winter that might be a night phase with the moon up, and in summer, a couple of hours before sunset.

So this is just a conceptual post for talking about it, and spit balling ideas to try and come up with a better system.

You could use a Number instead. All Design Patterns are intended to be customized for what ever works best for you and your use cases. Using a String is great for creating self documenting code, not so great when you want to do a test like you are after. So don’t feel like you have to keep using a String.

You could split the difference and use alphabetical String (e.g. 0-MORNING, 1-DAY, etc).

I don’t know that you need to go so far as creating the mapping between the String and a number.

I like the string though. But I could just use a sitemap mapping to get it back