openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

[time] Extend toZDT examples #225

Closed maniac103 closed 1 year ago

maniac103 commented 1 year ago

In a lot of rules, I have a need for creating a timestamp at midnight (e.g. for determining today's energy usage). Currently, I have to use a somewhat cumbersome syntax:

var midnight = time.toZDT().withHour(0).withMinute(0).withSecond(0).withNano(0);

It would be great if there was a shorter/more concise version for this, like

var midnight = time.toZDT().toMidnight(); // suggestion 1
var midnight2 = time.toZDT().toStartOfDay(); // suggestion 2, maybe more inline with toToday()?

Additionally, there could be a withTime() method:

var midnight = time.toZDT().withTime(0, 0, 0, 0); // hour, minute, second, nano
var someTime1 = time.toZDT().withTime(9, 0, 0, 0);
florian-h05 commented 1 year ago

IMO toStartOfDay is more concise, because toMidnight doesn’t make clear which midnight is meant: the one this morning or the one this evening?

toStartOfDay is clear: the start of this day and more in line with toToday.

Anyway, I am wondering why you need such a method: For your use case I would simplify do:

time.toZDT(„00:00“);

Sorry for the wrong hyphens, my smartphones keyboard :-/

maniac103 commented 1 year ago

Anyway, I am wondering why you need such a method: For your use case I would simplify do:

Looking at the code, that indeed works as well ... and even is documented as such ;-) Issue is just that you need to know this is actually possible, as otherwise you won't even read the respective docs. Maybe it's just me, but when dealing with unknown API I tend to check for methods that look related and then reading the docs for those methods. It didn't occur to me to check toZDT() in that case. I therefore think a toStartOfDay convenience method makes sense; leaving it up to you to decide on :+1: / :-1: though. If you think it's OK, I can implement it.

florian-h05 commented 1 year ago

I understand your idea, but I‘d like to keep the API clean and only add methods that add additional functionality or make something much easier to use, which is not the case here.

So, sorry 👎

We should maybe extend the examples in the README to actually show what’s possible with time.toZDT.

maniac103 commented 1 year ago

Fair enough. Yes, extending the examples sounds like a good idea.

florian-h05 commented 1 year ago

Do you want to extend the examples yourself?