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



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

.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



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

#dayObject



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

def day
  @time.getDayOfMonth
end

#hourObject



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

def hour
  @time.getHourOfDay
end

#isdstObject 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_zoneObject

Returns the Java Timezone object



183
184
185
# File 'lib/jtime.rb', line 183

def java_zone
  @time.getZone
end

#minObject



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

def min
  @time.getMinuteOfHour
end

#monthObject Also known as: mon



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

def month
  @time.getMonthOfYear
end

#secObject



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

def sec
  @time.getSecondOfMinute
end

#to_date_time_valueObject

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_fObject

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_iObject

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_javaObject

Returns the underlying Java DateTime object



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

def to_java
  @time
end

#to_sObject

Returns a string representing JTime



119
120
121
# File 'lib/jtime.rb', line 119

def to_s
  @time.toString
end

#to_timeObject

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

#utcObject

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

#wdayObject



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

def wday
  @time.getDayOfWeek
end

#weekObject



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

def week
  @time.getWeekOfWeekyear
end

#ydayObject



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

def yday
  @time.getDayOfYear
end

#yearObject



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

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’



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

def zone
  @time.getZone.getID
end