Class: JTime
Class Method Summary collapse
-
.at(seconds, microseconds = 0) ⇒ Object
Creates a new time object with the given number of seconds (and optional microseconds) from epoch.
-
.from_date_time_value(dtv) ⇒ Object
Initialize a JTime object from DateTimeValue.
-
.from_time(time) ⇒ Object
Initialize a JTime object from Ruby Time object.
-
.gm ⇒ Object
Creates a time based on given values, interpreted as UTC (GMT).
-
.local(year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
(also: mktime)
Same as JTime.utc, but interprets the values in local time zone.
-
.utc(year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
Creates a time based on given values, interpreted as UTC (GMT).
Instance Method Summary collapse
- #<=>(another_time) ⇒ Object
- #day ⇒ Object
- #hour ⇒ Object
-
#initialize(datetime = JDateTime.new) ⇒ JTime
constructor
Returns a JTime object.
-
#isdst ⇒ Object
(also: #dst?)
Returns true if time occurs during Daylight Saveing Time in its timeezone.
-
#java_zone ⇒ Object
Returns the Java Timezone object.
-
#method_missing(key, *params) ⇒ Object
We should consider getting rid of this or implement this using meta-programming.
- #min ⇒ Object
- #month ⇒ Object (also: #mon)
- #sec ⇒ Object
-
#to_date_time_value ⇒ Object
Returns DateTimeValue object.
-
#to_f ⇒ Object
Returns the value of time as a floating point number of seconds since epoch.
-
#to_i ⇒ Object
Returns the value of time as an integer number of seconds since epoch.
-
#to_java ⇒ Object
Returns the underlying Java DateTime object.
-
#to_s ⇒ Object
Returns a string representing JTime.
-
#to_time ⇒ Object
Returns a Ruby Time object.
-
#utc ⇒ Object
Returns a new JTime object in UTC time.
- #wday ⇒ Object
- #week ⇒ Object
- #yday ⇒ Object
- #year ⇒ Object
-
#zone ⇒ Object
Returns the name of the timezone used for time.
Constructor Details
#initialize(datetime = JDateTime.new) ⇒ JTime
Returns a JTime object. If a Java DateTime object is passed, it is initialized from the Java JDateTime object, otherwise it is initialized to the current system time
Options
datetime-
Optional. The Java DateTime (org.joda.time.DateTime’)object
92 93 94 95 |
# File 'lib/jtime.rb', line 92 def initialize(datetime = JDateTime.new) @time = datetime self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *params) ⇒ Object
We should consider getting rid of this or implement this using meta-programming
194 195 196 |
# File 'lib/jtime.rb', line 194 def method_missing(key, *params) @time.send(key, *params) end |
Class Method Details
.at(seconds, microseconds = 0) ⇒ Object
Creates a new time object with the given number of seconds (and optional microseconds) from epoch.
Options
seconds-
given number of seconds from epoch
microseconds-
Optional. microseconds from epoch
17 18 19 20 |
# File 'lib/jtime.rb', line 17 def at(seconds, microseconds=0) datetime = JDateTime.new(seconds * 1000 + microseconds) new(datetime) end |
.from_date_time_value(dtv) ⇒ Object
Initialize a JTime object from DateTimeValue
47 48 49 |
# File 'lib/jtime.rb', line 47 def from_date_time_value(dtv) utc(dtv.year, dtv.month, dtv.day, dtv.hour, dtv.minute, dtv.second) end |
.from_time(time) ⇒ Object
Initialize a JTime object from Ruby Time object
52 53 54 55 |
# File 'lib/jtime.rb', line 52 def from_time(time) datetime = JDateTime.new((time.to_f * 1000).to_i) new(datetime) end |
.gm ⇒ Object
Creates a time based on given values, interpreted as UTC (GMT). The year must be specified. Other values default to the minimum value for that field.
Options
year-
Year
month-
Optional. Numbers from 1 to 12, or by the three-letter English month names
hour-
Optional.24-hour clock (0..23)
min-
Optional. 0..59
sec-
Optional. seconds of the time
usec-
Optional. microsecond of the time
58 59 60 61 62 |
# File 'lib/jtime.rb', line 58 def utc(year, month=1, day=1, hour=0, min=0, sec=0, usec=0) datetime = JDateTime.new(year, numeric_month(month), day, hour, min, sec, usec, org.joda.time.DateTimeZone::UTC) new(datetime) end |
.local(year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object Also known as: mktime
Same as JTime.utc, but interprets the values in local time zone
41 42 43 44 |
# File 'lib/jtime.rb', line 41 def local(year, month=1, day=1, hour=0, min=0, sec=0, usec=0) datetime = JDateTime.new(year, numeric_month(month), day, hour, min, sec, usec) new(datetime) end |
.utc(year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
Creates a time based on given values, interpreted as UTC (GMT). The year must be specified. Other values default to the minimum value for that field.
Options
year-
Year
month-
Optional. Numbers from 1 to 12, or by the three-letter English month names
hour-
Optional.24-hour clock (0..23)
min-
Optional. 0..59
sec-
Optional. seconds of the time
usec-
Optional. microsecond of the time
34 35 36 37 38 |
# File 'lib/jtime.rb', line 34 def utc(year, month=1, day=1, hour=0, min=0, sec=0, usec=0) datetime = JDateTime.new(year, numeric_month(month), day, hour, min, sec, usec, org.joda.time.DateTimeZone::UTC) new(datetime) end |
Instance Method Details
#<=>(another_time) ⇒ Object
130 131 132 |
# File 'lib/jtime.rb', line 130 def <=>(another_time) @time.compareTo(another_time.to_java) end |
#day ⇒ Object
142 143 144 |
# File 'lib/jtime.rb', line 142 def day @time.getDayOfMonth end |
#hour ⇒ Object
146 147 148 |
# File 'lib/jtime.rb', line 146 def hour @time.getHourOfDay end |
#isdst ⇒ Object Also known as: dst?
Returns true if time occurs during Daylight Saveing Time in its timeezone
188 189 190 191 |
# File 'lib/jtime.rb', line 188 def isdst timezone = @time.getZone.toTimeZone timezone.inDaylightTime(@time.toDate) end |
#java_zone ⇒ Object
Returns the Java Timezone object
183 184 185 |
# File 'lib/jtime.rb', line 183 def java_zone @time.getZone end |
#min ⇒ Object
150 151 152 |
# File 'lib/jtime.rb', line 150 def min @time.getMinuteOfHour end |
#month ⇒ Object Also known as: mon
138 139 140 |
# File 'lib/jtime.rb', line 138 def month @time.getMonthOfYear end |
#sec ⇒ Object
154 155 156 |
# File 'lib/jtime.rb', line 154 def sec @time.getSecondOfMinute end |
#to_date_time_value ⇒ Object
Returns DateTimeValue object
178 179 180 |
# File 'lib/jtime.rb', line 178 def to_date_time_value com.google.ical.values.DateTimeValueImpl.new(self.year, self.month, self.day, self.hour, self.min, self.sec) end |
#to_f ⇒ Object
Returns the value of time as a floating point number of seconds since epoch
114 115 116 |
# File 'lib/jtime.rb', line 114 def to_f @time.getMillis / 1000.0 end |
#to_i ⇒ Object
Returns the value of time as an integer number of seconds since epoch
109 110 111 |
# File 'lib/jtime.rb', line 109 def to_i @time.getMillis / 1000 end |
#to_java ⇒ Object
Returns the underlying Java DateTime object
104 105 106 |
# File 'lib/jtime.rb', line 104 def to_java @time end |
#to_s ⇒ Object
Returns a string representing JTime
119 120 121 |
# File 'lib/jtime.rb', line 119 def to_s @time.toString end |
#to_time ⇒ Object
Returns a Ruby Time object
98 99 100 101 |
# File 'lib/jtime.rb', line 98 def to_time millis = @time.getMillis Time.at(millis / 1000, millis % 1000 * 1000) end |
#utc ⇒ Object
Returns a new JTime object in UTC time. The receiver is unchanged IMPORTANT: The original utc in Ruby modified the receiver object
125 126 127 128 |
# File 'lib/jtime.rb', line 125 def utc datetime = @time.toDateTime(org.joda.time.DateTimeZone::UTC) self.class.new(datetime) end |
#wday ⇒ Object
158 159 160 |
# File 'lib/jtime.rb', line 158 def wday @time.getDayOfWeek end |
#week ⇒ Object
166 167 168 |
# File 'lib/jtime.rb', line 166 def week @time.getWeekOfWeekyear end |
#yday ⇒ Object
162 163 164 |
# File 'lib/jtime.rb', line 162 def yday @time.getDayOfYear end |
#year ⇒ Object
134 135 136 |
# File 'lib/jtime.rb', line 134 def year @time.getYear end |
#zone ⇒ Object
Returns the name of the timezone used for time. IMPORTANT: Unlike Ruby Time, which returns “PST” it actuallys return real timezone ID such as ‘America/Los_Angeles’
173 174 175 |
# File 'lib/jtime.rb', line 173 def zone @time.getZone.getID end |