Skip to content

Commit

Permalink
Add #yesterday?, #today?, and #tomorrow? to ZonedDateTime, Date…
Browse files Browse the repository at this point in the history
…, and Time classes.

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Sep 17, 2024
1 parent d8f0e9e commit d444bbd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/openhab/core_ext/java/zoned_date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions lib/openhab/core_ext/ruby/date.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/openhab/core_ext/ruby/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions spec/openhab/core_ext/java/zoned_date_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

Expand Down

0 comments on commit d444bbd

Please sign in to comment.