Date and Time

logo

This Library adds support to get, create, compare and modify Times and Dates with Blockly.

Blocks

Creation of ZonedDateTime with Ephemeris → date block
oh_date
Please be aware that the existing blocks for dates set their time to 0 and the zone offset to UTC. Although the date block is set as shadow in all blocks of this library, it should not be used. If you compare dates only it does not matter.

now

Obtains the current date-time from the system clock in the default time-zone.
get_zdt_now

ZDT with date <year>-<month>-<day> and time <hour>:<minute>:<second>

Creates a ZonedDateTime based on the given input for date and time with nanos set to 0 and the system’s time-zone. Values must be valid.

Allowed values

year: the year
month: 1 to 12
day: 1 to 28-31
hour: 0 to 23
minute: 0 to 59
second: 0 to 59

ZDT from item <item>

Get the ZonedDateTime from item’s state. Error if item is undef/NULL.
get_zdt_from_oh_item

set item <item> to ZDT <zdt>

Update state of a DateTime item to the provided ZonedDateTime.

copy of <zdt> with time set to <hour>:<minute>:<second>

Returns a copy of the provided ZonedDateTime with hours, minutes and seconds altered.

Allowed values

hour: 0 to 23
minute: 0 to 59
second: 0 to 59

copy of <zdt> +/- <number> <datetime_unit>

Returns a copy of the provided ZonedDateTime with the specified value added or subtracted.

copy of <zdt> with <datetime_unit> altered to <number>

Returns a copy of the provided ZonedDateTime with the selected field altered.

is <zdt1> before/after/equal <zdt2> use date and time/date/time with resolution of <datetime_unit>

Checks if the instant of the first ZonedDateTime is before/after/equal that of the second ZonedDateTime. You can select if you want to compare all components, date only or time only.

is <zdt1> between <zdt2> and <zdt3> use date and time/date/time

Check if the instant of the first ZonedDateTime is between the two other ZonedDateTime. You can select if you want to compare all components, date only or time only.

get <datetime_unit> from <zdt>

Returns the selected unit as number.
get_zdt_component

<datetime_unit> between <zdt1> and <zdt2>

Calculates the amount of time between two ZonedDateTime objects. The result will be negative if the second object is before the first one.

Changelog

Version 0.2

  • Fixed comparison with time only
  • Fixed wrong tooltip
  • Added block compare_zdt_between_zdt_and_zdt

Version 0.1

  • initial release

Resources

https://raw.githubusercontent.com/deibich/openhab-libraries/0f40c616bf45a7c74db2156162b995c543c8e787/blockly/libraries/datetime/datetime.yaml

6 Likes

Examples

Hi David,

I am almost done with porting your blocks to blockly core. You lately added the following block to the library. I know what it does but I wonder what you/we need it for. Can you provide an example?

returns

Europe/Berlin

PS: I did not add your “set item” as it can be achieved by using a generic post-command-block (I do understand that you added it for convenience though in your library). I also combined blocks 4-6 into one block which is much more versatile but couldn’t been done with the block library because it uses mutators. I also added e.g. small additions like milli seconds support. Other than that the new blocks are very much based on your ideas and your work - so this is very much appreciated and I will add that appreciation to the PR that will come eventually.

Hello Stefan,

The idea was to get the local time in other regions of the world. But I didn’t pursue the idea any further and forgot to remove the block in v0.1. I’m not sure if anyone is using this block(don’t think so) and also left it in v0.2 of the library.

Sounds nice.

Thank you work your work to port this library to the core.

1 Like

From now on all these blocks (although a small number of them in a bit modified form) are available in openHABs core blocks. You can find the information about that at New Datetime Blocks, easier Logging and CRLF

Thanks David for the original contribution here.

1 Like

There is an error in the code of the date-between block that is also present in the core blocklies. See the PR that fixes it. You may want to fix it here as well: