Class: OpenHAB::CoreExt::Java::LocalTime

Inherits:
Object
  • Object
show all
Includes:
Between
Defined in:
lib/openhab/core_ext/java/local_time.rb

Overview

Extensions to javajava.timejava.time.LocalTime

Examples:

break_time = LocalTime::NOON

if LocalTime.now > LocalTime.of(17, 30) # comparing two LocalTime objects
  # do something
elsif LocalTime.now < LocalTime.parse('8:30') # comparison against a string
  # do something
end
four_pm = LocalTime.parse('16:00')
# Trigger security light between sunset and sunrise when motion is detected
rule 'Outside Light Motion' do
  updated Motion_Sensor, to: OPEN
  run do
    astro = things['astro:sun:home']
    sunrise = astro.get_event_time('SUN_RISE', nil, nil).to_local_time
    sunset = astro.get_event_time('SUN_SET', nil, nil).to_local_time
    next if (sunrise..sunset).cover?(Time.now)

    Security_Light.on for: 10.minutes
  end
end

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Between

#between?

Class Attribute Details

.nowLocalTime (readonly)

Returns:



# File 'lib/openhab/core_ext/java/local_time.rb', line 42

Class Method Details

.parse(string, formatter = nil) ⇒ LocalTime

Parses strings in the form “h[:mm] [am/pm]” when no formatter is given.

Parameters:

  • string (String)
  • formatter (java.time.format.DateTimeFormatter) (defaults to: nil)

    The formatter to use

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/openhab/core_ext/java/local_time.rb', line 55

def parse(string, formatter = nil)
  return raw_parse(string, formatter) if formatter

  format = /(am|pm)$/i.match?(string) ? "h[:mm[:ss][.S]][ ]a" : "H[:mm[:ss][.S]]"
  java_send(:parse,
            [java.lang.CharSequence, java.time.format.DateTimeFormatter],
            string,
            java.time.format.DateTimeFormatterBuilder.new
                                                     .parse_case_insensitive
                                                     .parse_lenient
                                                     .append_pattern(format)
                                                     .to_formatter(java.util.Locale::ENGLISH))
end

Instance Method Details

#+(other) ⇒ LocalTime

Parameters:

Returns:



83
84
85
86
87
88
# File 'lib/openhab/core_ext/java/local_time.rb', line 83

def +(other)
  return plus(other.seconds) if other.is_a?(Numeric)
  return self if other.is_a?(Period)

  plus(other)
end

#-(other) ⇒ LocalTime

Parameters:

Returns:



73
74
75
76
77
78
# File 'lib/openhab/core_ext/java/local_time.rb', line 73

def -(other)
  return minus(other.seconds) if other.is_a?(Numeric)
  return self if other.is_a?(Period)

  minus(other)
end

#succLocalTime

Returns the next second

Will loop back to the beginning of the day if necessary.

Returns:



97
98
99
# File 'lib/openhab/core_ext/java/local_time.rb', line 97

def succ
  plus_seconds(1)
end

#to_local_timeself

Returns:

  • (self)


102
103
104
# File 'lib/openhab/core_ext/java/local_time.rb', line 102

def to_local_time
  self
end

#to_zoned_date_time(context = nil) ⇒ ZonedDateTime

Parameters:

Returns:



110
111
112
113
# File 'lib/openhab/core_ext/java/local_time.rb', line 110

def to_zoned_date_time(context = nil)
  context ||= ZonedDateTime.now
  context.with(self)
end