Class: OpenHAB::CoreExt::Java::Instant

Inherits:
java.lang.Object show all
Includes:
Between, Time
Defined in:
lib/openhab/core_ext/java/instant.rb

Overview

Extensions to java.time.Instant

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Between

#between?

Class Attribute Details

.nowInstant (readonly)

Returns:

[View source]

# File 'lib/openhab/core_ext/java/instant.rb', line 21

Class Method Details

.parse(text, formatter = nil) ⇒ Instant

Parses a string into an Instant object.

Parameters:

Returns:

[View source]

# File 'lib/openhab/core_ext/java/instant.rb', line 24

Instance Method Details

#+(other) ⇒ Instant

Parameters:

Returns:

[View source]

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

Parameters:

Returns:

  • (Duration)

    If other responds to #to_zoned_date_time

  • (Instant)

    If other is a TemporalAmount

[View source]

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?

Returns:

[View source]

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

Parameters:

  • other (#to_instant)

Returns:

[View source]

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_dateDate

Returns:

[View source]

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_fFloat

The number of seconds since the Unix epoch.

Returns:

[View source]

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_iInteger

The number of seconds since the Unix epoch.

Returns:

[View source]

91
92
93
# File 'lib/openhab/core_ext/java/instant.rb', line 91

def to_i
  epoch_second
end

#to_local_dateLocalDate

Returns:

[View source]

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_timeLocalTime

Returns:

[View source]

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_monthMonth

Returns:

[View source]

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_dayMonthDay

Returns:

[View source]

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_timeTime

Returns:

[View source]

# File 'lib/openhab/core_ext/java/instant.rb', line 105

#to_zoned_date_time(context = nil) ⇒ ZonedDateTime

Parameters:

Returns:

[View source]

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.

Returns:

  • (true, false)
[View source]

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.

Returns:

  • (true, false)
[View source]

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.

Returns:

  • (true, false)
[View source]

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?