Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
feat(time_of_day): support a range of LocalTime for TimeOfDay#between?
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng committed Aug 6, 2022
1 parent 7255ede commit fd12e3d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
22 changes: 11 additions & 11 deletions docs/usage/misc/time_of_day.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ grand_parent: Usage

TimeOfDay class can be used in rules for time related logic. Methods:

| Method | Parameter | Description | Example |
| ----------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| parse | String | Creates a TimeOfDay object with a given time string. The format is hh[:mm[:ss]][am\|pm]. When am/pm is not specified, the time should be in 24h format. | curfew_start = TimeOfDay.parse '19:30' => 19:30<br/>TimeOfDay.parse '2pm' => 14:00<br/> TimeOfDay.parse '12:30am' => 00:30<br/>TimeOfDay.parse '15' => 15:00 |
| now | | Creates a TimeOfDay object that represents the current time | TimeOfDay.now > curfew_start, or TimeOfDay.now > '19:30' |
| MIDNIGHT | | Creates a TimeOfDay object for 00:00 | TimeOfDay.MIDNIGHT |
| NOON | | Creates a TimeOfDay object for 12:00 | TimeOfDay.now < TimeOfDay.NOON |
| constructor | h, m, s | Creates a TimeOfDay with the given hour, minute, second | TimeOfDay.new(h: 17, m: 30, s: 0) |
| hour | | Returns the hour part of the object | TimeOfDay.now.hour |
| minute | | Returns the minute part of the object | TimeOfDay.now.minute |
| second | | Returns the second part of the object | TimeOfDay.now.second |
| between? | TimeOfDayRange | Returns true if it falls within the given time range | TimeOfDay.now.between? '3pm'..'7pm' |
| Method | Parameter | Description | Example |
| ----------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| parse | String | Creates a TimeOfDay object with a given time string. The format is hh[:mm[:ss]][am\|pm]. When am/pm is not specified, the time should be in 24h format. | curfew_start = TimeOfDay.parse '19:30' => 19:30<br/>TimeOfDay.parse '2pm' => 14:00<br/> TimeOfDay.parse '12:30am' => 00:30<br/>TimeOfDay.parse '15' => 15:00 |
| now | | Creates a TimeOfDay object that represents the current time | TimeOfDay.now > curfew_start, or TimeOfDay.now > '19:30' |
| MIDNIGHT | | Creates a TimeOfDay object for 00:00 | TimeOfDay.MIDNIGHT |
| NOON | | Creates a TimeOfDay object for 12:00 | TimeOfDay.now < TimeOfDay.NOON |
| constructor | h, m, s | Creates a TimeOfDay with the given hour, minute, second | TimeOfDay.new(h: 17, m: 30, s: 0) |
| hour | | Returns the hour part of the object | TimeOfDay.now.hour |
| minute | | Returns the minute part of the object | TimeOfDay.now.minute |
| second | | Returns the second part of the object | TimeOfDay.now.second |
| between? | Range | Returns true if it falls within the given time range. Supports a range of TimeOfDay, Ruby Time, string, DateTimeItem, DateTimeType, and LocalTime | TimeOfDay.now.between? '3pm'..'7pm' |


A TimeOfDay object can be compared against another TimeOfDay object or a parseable string representation of time.
Expand Down
8 changes: 5 additions & 3 deletions features/time_of_day.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Feature: time_of_day
When I deploy the rule
Then It should log "TimeOfDay is in the range: <result>" within 2 seconds
Examples:
| from | to | result |
| '11:55:00' | '12:05:00' | true |
| '12:05:00' | '12:10:00' | false |
| from | to | result |
| '11:55:00' | '12:05:00' | true |
| '12:05:00' | '12:10:00' | false |
| LocalTime::NOON.minusMinutes(5) | LocalTime::NOON.plusMinutes(5) | true |
| LocalTime::NOON.plusMinutes(3) | LocalTime::NOON.plusMinutes(5) | false |
2 changes: 2 additions & 0 deletions lib/openhab/dsl/time/time_of_day.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def self.to_time_of_day(object)
when String then TimeOfDay.parse(object)
when Time, OpenHAB::DSL::Types::DateTimeType, OpenHAB::DSL::Items::DateTimeItem
TimeOfDay.new(h: object.hour, m: object.min, s: object.sec)
when LocalTime
TimeOfDay.new(h: object.hour, m: object.minute, s: object.second)
else object
end
end
Expand Down

0 comments on commit fd12e3d

Please sign in to comment.