Class: JTime

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/jtime.rb

Class Method Summary collapse

Instance Method Summary collapse

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



98
99
100
101
# File 'lib/jtime.rb', line 98

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



201
202
203
# File 'lib/jtime.rb', line 201

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



53
54
55
# File 'lib/jtime.rb', line 53

def from_date_time_value(dtv)
  utc(dtv.year, dtv.month, dtv.day, dtv.hour, dtv.minute, dtv.second)
end

.from_date_value(dv, timezone = org.joda.time.DateTimeZone::UTC) ⇒ Object

Initialize a JTime object form DateValue



47
48
49
50
# File 'lib/jtime.rb', line 47

def from_date_value(dv, timezone = org.joda.time.DateTimeZone::UTC)
  datetime = JDateTime.new(dv.year, numeric_month(dv.month), dv.day, 0, 0, 0, 0, timezone)
  new(datetime)
end

.from_time(time) ⇒ Object

Initialize a JTime object from Ruby Time object



58
59
60
61
# File 'lib/jtime.rb', line 58

def from_time(time)
  datetime = JDateTime.new((time.to_f * 1000).to_i)
  new(datetime)
end

.gmObject

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



64
65
66
67
68
# File 'lib/jtime.rb', line 64

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



137
138
139
# File 'lib/jtime.rb', line 137

def <=>(another_time)
  @time.compareTo(another_time.to_java)
end

#dayObject



149
150
151
# File 'lib/jtime.rb', line 149

def day
  @time.getDayOfMonth
end

#hourObject



153
154
155
# File 'lib/jtime.rb', line 153

def hour
  @time.getHourOfDay
end

#isdstObject Also known as: dst?

Returns true if time occurs during Daylight Saveing Time in its timeezone



195
196
197
198
# File 'lib/jtime.rb', line 195

def isdst
  timezone = @time.getZone.toTimeZone
  timezone.inDaylightTime(@time.toDate)
end

#java_zoneObject

Returns the Java Timezone object



190
191
192
# File 'lib/jtime.rb', line 190

def java_zone
  @time.getZone
end

#minObject



157
158
159
# File 'lib/jtime.rb', line 157

def min
  @time.getMinuteOfHour
end

#monthObject Also known as: mon



145
146
147
# File 'lib/jtime.rb', line 145

def month
  @time.getMonthOfYear
end

#secObject



161
162
163
# File 'lib/jtime.rb', line 161

def sec
  @time.getSecondOfMinute
end

#to_date_time_valueObject

Returns DateTimeValue object



185
186
187
# File 'lib/jtime.rb', line 185

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_fObject

Returns the value of time as a floating point number of seconds since epoch



121
122
123
# File 'lib/jtime.rb', line 121

def to_f
  @time.getMillis / 1000.0
end

#to_iObject

Returns the value of time as an integer number of seconds since epoch



116
117
118
# File 'lib/jtime.rb', line 116

def to_i
  @time.getMillis / 1000
end

#to_javaObject

Returns the underlying Java DateTime object



111
112
113
# File 'lib/jtime.rb', line 111

def to_java
  @time
end

#to_sObject

Returns a string representing JTime



126
127
128
# File 'lib/jtime.rb', line 126

def to_s
  @time.toString
end

#to_timeObject

Returns a Ruby Time object, since Ruby Time object has limited time zone support, it always return a UTC time



105
106
107
108
# File 'lib/jtime.rb', line 105

def to_time
  millis = @time.getMillis
  Time.at(millis / 1000, millis % 1000 * 1000).utc
end

#utcObject

Returns a new JTime object in UTC time. The receiver is unchanged IMPORTANT: The original utc in Ruby modified the receiver object



132
133
134
135
# File 'lib/jtime.rb', line 132

def utc
  datetime = @time.toDateTime(org.joda.time.DateTimeZone::UTC)
  self.class.new(datetime)
end

#wdayObject



165
166
167
# File 'lib/jtime.rb', line 165

def wday
  @time.getDayOfWeek
end

#weekObject



173
174
175
# File 'lib/jtime.rb', line 173

def week
  @time.getWeekOfWeekyear
end

#ydayObject



169
170
171
# File 'lib/jtime.rb', line 169

def yday
  @time.getDayOfYear
end

#yearObject



141
142
143
# File 'lib/jtime.rb', line 141

def year
  @time.getYear
end

#zoneObject

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’



180
181
182
# File 'lib/jtime.rb', line 180

def zone
  @time.getZone.getID
end