Skip to content

Commit

Permalink
Support space separator between date and time (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Sep 8, 2024
1 parent 741f18f commit f581a49
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/openhab/core/items/date_time_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ class DateTimeItem < GenericItem
def format_type(command)
return command if command.is_a?(Types::DateTimeType)
return Types::DateTimeType.new(command.to_zoned_date_time) if command.respond_to?(:to_zoned_date_time)
return Types::DateTimeType.new(DSL.try_parse_time_like(command.to_str)) if command.respond_to?(:to_str)

if command.respond_to?(:to_str)
command = command.to_str
begin
return Types::DateTimeType.new(DSL.try_parse_time_like(command))
rescue ArgumentError
return Types::DateTimeType.new(command)
end
end

super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openhab/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ def try_parse_time_like(string)
return string unless string.is_a?(String)

exception = nil
[java.time.LocalTime, java.time.LocalDate, java.time.MonthDay, java.time.ZonedDateTime].each do |klass|
[java.time.LocalTime, java.time.LocalDate, java.time.MonthDay, java.time.ZonedDateTime, Time].each do |klass|
return klass.parse(string)
rescue ArgumentError => e
exception ||= e
Expand Down
10 changes: 10 additions & 0 deletions spec/openhab/core/items/date_time_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@
item.update("3:30pm")
expect(item.state).to eq LocalTime.parse("3:30pm").to_zoned_date_time
end

it "can be updated by a string that looks like a date" do
item.update("2021-01-01")
expect(item.state).to eq LocalDate.parse("2021-01-01").to_zoned_date_time
end

it "can be updated by a string that looks like a date and time" do
item.update("2021-01-01 15:40Z")
expect(item.state).to eq ZonedDateTime.parse("2021-01-01T15:40Z")
end
end

0 comments on commit f581a49

Please sign in to comment.