From d444bbdd08899788bd9aa0404efadd5a1699be14 Mon Sep 17 00:00:00 2001 From: Jimmy Tanagra Date: Tue, 17 Sep 2024 10:35:47 +1000 Subject: [PATCH] Add `#yesterday?`, `#today?`, and `#tomorrow?` to ZonedDateTime, Date, and Time classes. Signed-off-by: Jimmy Tanagra --- lib/openhab/core_ext/java/zoned_date_time.rb | 30 ++++++++++++++++ lib/openhab/core_ext/ruby/date.rb | 12 +++++++ lib/openhab/core_ext/ruby/time.rb | 10 ++++++ .../core_ext/java/zoned_date_time_spec.rb | 35 +++++++++++++++++++ 4 files changed, 87 insertions(+) diff --git a/lib/openhab/core_ext/java/zoned_date_time.rb b/lib/openhab/core_ext/java/zoned_date_time.rb index 9ce9530005..8188ab17a4 100644 --- a/lib/openhab/core_ext/java/zoned_date_time.rb +++ b/lib/openhab/core_ext/java/zoned_date_time.rb @@ -103,6 +103,36 @@ def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgumen self end + # + # Returns true if the date, converted to the system time zone, is yesterday. + # + # @return [true, false] + # + def yesterday? + with_zone_same_instant(ZoneId.system_default).to_local_date == LocalDate.now - 1 + end + + # + # Returns true if the date, converted to the system time zone, is today. + # + # This is the equivalent of checking if the current ZonedDateTime is between midnight and end of the day + # of the system time zone. + # + # @return [true, false] + # + def today? + with_zone_same_instant(ZoneId.system_default).to_local_date == LocalDate.now + end + + # + # Returns true if the date, converted to the system time zone, is tomorrow. + # + # @return [true, false] + # + def tomorrow? + with_zone_same_instant(ZoneId.system_default).to_local_date == LocalDate.now + 1 + end + # @group Ephemeris Methods # (see CoreExt::Ephemeris) diff --git a/lib/openhab/core_ext/ruby/date.rb b/lib/openhab/core_ext/ruby/date.rb index 43472aaa07..dc2e96d3e8 100644 --- a/lib/openhab/core_ext/ruby/date.rb +++ b/lib/openhab/core_ext/ruby/date.rb @@ -1,9 +1,11 @@ # frozen_string_literal: true +require "forwardable" require "date" # Extensions to Date class Date + extend Forwardable include OpenHAB::CoreExt::Between include OpenHAB::CoreExt::Ephemeris @@ -62,6 +64,16 @@ def to_zoned_date_time(context = nil) to_local_date.to_zoned_date_time(context) end + # @!method yesterday? + # (see ZonedDateTime#yesterday?) + # @!method today? + # (see ZonedDateTime#today?) + # @!method tomorrow? + # (see ZonedDateTime#tomorrow?) + + # @!visibility private + def_delegators :to_zoned_date_time, :yesterday?, :today?, :tomorrow? + # @return [Integer, nil] def compare_with_coercion(other) return compare_without_coercion(other) if other.is_a?(self.class) diff --git a/lib/openhab/core_ext/ruby/time.rb b/lib/openhab/core_ext/ruby/time.rb index 7369c7952b..e4def55772 100644 --- a/lib/openhab/core_ext/ruby/time.rb +++ b/lib/openhab/core_ext/ruby/time.rb @@ -74,6 +74,16 @@ def to_local_date(_context = nil) # @return [LocalTime] def_delegator :to_zoned_date_time, :to_local_time + # @!method yesterday? + # (see ZonedDateTime#yesterday?) + # @!method today? + # (see ZonedDateTime#today?) + # @!method tomorrow? + # (see ZonedDateTime#tomorrow?) + + # @!visibility private + def_delegators :to_zoned_date_time, :yesterday?, :today?, :tomorrow? + # @return [Month] def to_month java.time.Month.of(month) diff --git a/spec/openhab/core_ext/java/zoned_date_time_spec.rb b/spec/openhab/core_ext/java/zoned_date_time_spec.rb index 1173b5e46d..e5c2127d8c 100644 --- a/spec/openhab/core_ext/java/zoned_date_time_spec.rb +++ b/spec/openhab/core_ext/java/zoned_date_time_spec.rb @@ -85,6 +85,41 @@ end end + describe "#yesterday?" do + it "returns true if the date is yesterday" do + now = ZonedDateTime.now + expect(now.yesterday?).to be false + expect((now + 1.day).yesterday?).to be false + expect((now - 1.day).yesterday?).to be true + expect((now - 1.day).with(LocalTime::MIDNIGHT).yesterday?).to be true + expect((now - 1.day).with(LocalTime::NOON).yesterday?).to be true + expect((now - 1.day).with(LocalTime.parse("23:59:59")).yesterday?).to be true + end + end + + describe "#today?" do + it "returns true if the date is today" do + -12.upto(12) do |offset| + now = ZonedDateTime.now.with_zone_same_instant(java.time.ZoneOffset.of_hours(offset)) + expect(now.today?).to be true + expect((now + 1.day).today?).to be false + expect((now - 1.day).today?).to be false + end + end + end + + describe "#tomorrow?" do + it "returns true if the date is tomorrow" do + now = ZonedDateTime.now + expect(now.tomorrow?).to be false + expect((now + 1.day).tomorrow?).to be true + expect((now - 1.day).tomorrow?).to be false + expect((now + 1.day).with(LocalTime::MIDNIGHT).tomorrow?).to be true + expect((now + 1.day).with(LocalTime::NOON).tomorrow?).to be true + expect((now + 1.day).with(LocalTime.parse("23:59:59")).tomorrow?).to be true + end + end + describe "#<=>" do let(:zdt) { ZonedDateTime.parse("2022-11-09T02:09:05+00:00") }