Time mathematics

Hi there,

Is there a way to do basic computation with points in time ? Or is there some sort of time comprehension in SC ?

showTime = Date.new(2023, 3, 17, 14, 21, 15, 5));
time = Date.getDate();
"Show starting in % seconds".format((showTime-time).asSeconds).postln;

for some reason I thought .secStamp gave me actual compiled seconds but it is 60 based.

e.g. in Python you can do
datetime.now() - datetime.strptime("0001", "%Y")
which returns:
datetime.timedelta(days=738594, seconds=54539, microseconds=479285)


I don’t think so. It is a little odd you can’t convert to unix time from Date.

@mstep 's suggestion of scheduler will let you do these sorts of thing if you are ultimately trying to schedule something, but won’t work if you are just after the value.

I implemented some pretty self-contained Date improvements like this I need for SC…I think my PR was over five years ago. They weren’t merged in for a long time, then a few years later, when there was interest, unfortunately I didn’t have time to reactivate it and get it back in sync. It looks like others took on the work to get it updated, but then it seems to be still “stalled” for the last few years again. I’d pretty much forgotten about it until now.

I tried various avenues : thisThread.clock.seconds Date.getDate.rawSeconds, etc. But still. If I could get the absolute (unix) second my show starts and the current absolute second I guess I’d be fine. (still having to convert from 1679056364 sec to pretty much today. e.g.: that second was 1 hour, 3 minutes, 35 seconds ago).

@totalgee, wow, is 5 years the average pull-request treatment delay ? :slight_smile:

For the record :
I have this setlist generator that creates a Yaml file for a given show that SC digest happily with .parseYAMLFile. Inside there’s how many times I’ve played every song (in total) and for that specific crowd, etc. Many other data inside e.g.: style, tempo, language, devices settings (PVK, BB, Aeros, etc).

Anywho, I have always wanted a gui with a clock to show where I am in my schedule (some gigs are an hours, other 2, etc) So, in my setlist there are many timing data : ExpectedStartTime, ExpectedEndTime, flags like : MustStopBeforeExpectedEndTime = true for specific time rigid gigs. I can actually start the show minutes before or after ExpectedStartTime then automatically set ActualStartTime, etc.

So, basic math with current time vs event time is something I though would be trivial in SC.

// e.g.:
~show.add(\sched -> Dictionary.new());
~show[\sched].add(\audienceEntry -> Date.?);
~show[\sched].add(\start -> Date.?);
~show[\sched].add(\end -> Date.?);
~show[\sched].add(\audienceExitEnd -> Date.?);
~show[\sched].add(\realStart -> nil);
~show[\sched].add(\realEnd -> nil);

Boy do I feel bad. Like I’ve done something so terribly wrong.

I’ve implemented my own point-in-time translator i.e. from an easy to write array [14, 15, 16] representing hours, minutes, seconds (base 24,60,60) of the event converted to "51316" second from midnight. Then, I can do proper math with the current second in the day which is contained in (0..86400).

Boy is this code ugly, unmaintainable, not even 24h+ proof (i.e. a show starting at 23:00 ending 02:00 the next day (e.g.: rave, etc)

Worst than worst, I also have to convert back this second (e.g. 51416) to a digestible [HH:MM:SS] by hand to show something else in my gui than : "Your show starts in 4439 seconds from now".

Well well well, if it isn’t pull request #3259 again after all these years… What ? Merged ?! :smiley: thank you so much for your work @totalgee, I can’t wait to try it out !