Class: OpenHAB::CoreExt::Java::Instant
- Inherits:
-
java.lang.Object
- Object
- java.lang.Object
- OpenHAB::CoreExt::Java::Instant
- Defined in:
- lib/openhab/core_ext/java/instant.rb
Overview
Extensions to java.time.Instant
Class Attribute Summary collapse
- .now ⇒ Instant readonly
Class Method Summary collapse
-
.parse(text, formatter = nil) ⇒ Instant
Parses a string into an Instant object.
Instance Method Summary collapse
- #+(other) ⇒ Instant
- #-(other) ⇒ Duration, Instant
- #<=>(other) ⇒ Integer?
-
#coerce(other) ⇒ Array?
Converts
other
to Instant, if possible. - #to_date ⇒ Date
-
#to_f ⇒ Float
The number of seconds since the Unix epoch.
-
#to_i ⇒ Integer
The number of seconds since the Unix epoch.
- #to_local_date ⇒ LocalDate
- #to_local_time ⇒ LocalTime
- #to_month ⇒ Month
- #to_month_day ⇒ MonthDay
- #to_time ⇒ Time
- #to_zoned_date_time(context = nil) ⇒ ZonedDateTime
-
#today? ⇒ true, false
Returns true if the date, converted to the system time zone, is today.
-
#tomorrow? ⇒ true, false
Returns true if the date, converted to the system time zone, is tomorrow.
-
#yesterday? ⇒ true, false
Returns true if the date, converted to the system time zone, is yesterday.
Methods included from Between
Class Attribute Details
Class Method Details
.parse(text, formatter = nil) ⇒ Instant
Parses a string into an Instant object.
|
# File 'lib/openhab/core_ext/java/instant.rb', line 24
|
Instance Method Details
#+(other) ⇒ Instant
81 82 83 84 85 |
# File 'lib/openhab/core_ext/java/instant.rb', line 81 def +(other) return plus(other.seconds) if other.is_a?(Numeric) plus(other) end |
#-(other) ⇒ Duration, Instant
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/openhab/core_ext/java/instant.rb', line 64 def -(other) if other.is_a?(Instant) java.time.Duration.between(other, self) elsif other.respond_to?(:to_instant) java.time.Duration.between(other.to_instant, self) elsif other.respond_to?(:to_zoned_date_time) java.time.Duration.between(other.to_zoned_date_time.to_instant, self) elsif other.is_a?(Numeric) minus(other.seconds) else minus(other) end end |
#<=>(other) ⇒ Integer?
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/openhab/core_ext/java/instant.rb', line 109 def <=>(other) logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" } # compare instants, otherwise it will differ by timezone, which we don't want # (use eql? if you care about that) if other.respond_to?(:to_instant) logger.trace { "Comparing #{self} to #{other.to_instant}" } compare_to(other.to_instant(to_zoned_date_time)) elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self)) lhs <=> rhs end end |
#coerce(other) ⇒ Array?
Converts other
to OpenHAB::CoreExt::Java::Instant, if possible
139 140 141 142 143 144 |
# File 'lib/openhab/core_ext/java/instant.rb', line 139 def coerce(other) logger.trace { "Coercing #{self} as a request from #{other.class}" } return [other.to_instant(to_zoned_date_time), self] if other.respond_to?(:to_instant) [other.to_zoned_date_time(zoned_date_time).to_instant, self] if other.respond_to?(:to_zoned_date_time) end |
#to_date ⇒ Date
50 51 52 53 54 55 56 57 58 |
# File 'lib/openhab/core_ext/java/instant.rb', line 50 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |
#to_f ⇒ Float
The number of seconds since the Unix epoch.
99 100 101 |
# File 'lib/openhab/core_ext/java/instant.rb', line 99 def to_f ((epoch_second * 1_000_000_000) + nano).fdiv(1_000_000_000.0) end |
#to_i ⇒ Integer
The number of seconds since the Unix epoch.
91 92 93 |
# File 'lib/openhab/core_ext/java/instant.rb', line 91 def to_i epoch_second end |
#to_local_date ⇒ LocalDate
50 51 52 53 54 55 56 57 58 |
# File 'lib/openhab/core_ext/java/instant.rb', line 50 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |
#to_local_time ⇒ LocalTime
50 51 52 53 54 55 56 57 58 |
# File 'lib/openhab/core_ext/java/instant.rb', line 50 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |
#to_month ⇒ Month
50 51 52 53 54 55 56 57 58 |
# File 'lib/openhab/core_ext/java/instant.rb', line 50 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |
#to_month_day ⇒ MonthDay
50 51 52 53 54 55 56 57 58 |
# File 'lib/openhab/core_ext/java/instant.rb', line 50 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |
#to_zoned_date_time(context = nil) ⇒ ZonedDateTime
123 124 125 126 |
# File 'lib/openhab/core_ext/java/instant.rb', line 123 def to_zoned_date_time(context = nil) zone = context&.zone || java.time.ZoneOffset::UTC at_zone(zone) end |
#today? ⇒ true, false
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.
50 51 52 53 54 55 56 57 58 |
# File 'lib/openhab/core_ext/java/instant.rb', line 50 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |
#tomorrow? ⇒ true, false
Returns true if the date, converted to the system time zone, is tomorrow.
50 51 52 53 54 55 56 57 58 |
# File 'lib/openhab/core_ext/java/instant.rb', line 50 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |
#yesterday? ⇒ true, false
Returns true if the date, converted to the system time zone, is yesterday.
50 51 52 53 54 55 56 57 58 |
# File 'lib/openhab/core_ext/java/instant.rb', line 50 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |