Class: OpenHAB::CoreExt::Java::Instant
- Inherits:
-
java.lang.Object
- Object
- java.lang.Object
- OpenHAB::CoreExt::Java::Instant
- Includes:
- Between, Time, TimePredicates
- 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
otherto 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
Methods included from TimePredicates
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 25
|
Instance Method Details
#+(other) ⇒ Instant
82 83 84 85 86 |
# File 'lib/openhab/core_ext/java/instant.rb', line 82 def +(other) return plus(other.seconds) if other.is_a?(Numeric) plus(other) end |
#-(other) ⇒ Duration, Instant
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/openhab/core_ext/java/instant.rb', line 65 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?
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/openhab/core_ext/java/instant.rb', line 110 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
140 141 142 143 144 145 |
# File 'lib/openhab/core_ext/java/instant.rb', line 140 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/openhab/core_ext/java/instant.rb', line 51 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.
100 101 102 |
# File 'lib/openhab/core_ext/java/instant.rb', line 100 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.
92 93 94 |
# File 'lib/openhab/core_ext/java/instant.rb', line 92 def to_i epoch_second end |
#to_local_date ⇒ LocalDate
51 52 53 54 55 56 57 58 59 |
# File 'lib/openhab/core_ext/java/instant.rb', line 51 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/openhab/core_ext/java/instant.rb', line 51 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/openhab/core_ext/java/instant.rb', line 51 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/openhab/core_ext/java/instant.rb', line 51 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
124 125 126 127 |
# File 'lib/openhab/core_ext/java/instant.rb', line 124 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.
51 52 53 54 55 56 57 58 59 |
# File 'lib/openhab/core_ext/java/instant.rb', line 51 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.
51 52 53 54 55 56 57 58 59 |
# File 'lib/openhab/core_ext/java/instant.rb', line 51 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.
51 52 53 54 55 56 57 58 59 |
# File 'lib/openhab/core_ext/java/instant.rb', line 51 def_delegators :to_zoned_date_time, :to_local_time, :to_local_date, :to_date, :to_month_day, :to_month, :yesterday?, :today?, :tomorrow? |