Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #yesterday?, #today?, and #tomorrow? to ZonedDateTime, Date, and Time classes. #341

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 datetime 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
10 changes: 10 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,14 @@ def to_zoned_date_time(context = nil)
to_local_date.to_zoned_date_time(context)
end

# @!method yesterday?
# (see OpenHAB::CoreExt::Java::ZonedDateTime#yesterday?)
# @!method today?
# (see OpenHAB::CoreExt::Java::ZonedDateTime#today?)
# @!method tomorrow?
# (see OpenHAB::CoreExt::Java::ZonedDateTime#tomorrow?)
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
8 changes: 8 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,14 @@ def to_local_date(_context = nil)
# @return [LocalTime]
def_delegator :to_zoned_date_time, :to_local_time

# @!method yesterday?
# (see OpenHAB::CoreExt::Java::ZonedDateTime#yesterday?)
# @!method today?
# (see OpenHAB::CoreExt::Java::ZonedDateTime#today?)
# @!method tomorrow?
# (see OpenHAB::CoreExt::Java::ZonedDateTime#tomorrow?)
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|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't wait for this to fail on Daylight Savings Time transitions :p

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
Loading