Module: OpenWFE

Defined in:
lib/openwfe/util/otime.rb,
lib/openwfe/util/scheduler.rb

Defined Under Namespace

Modules: Schedulable Classes: AtJob, CronJob, CronLine, EveryJob, Job, Scheduler

Class Method Summary collapse

Class Method Details

.current_time_millisObject

equivalent to java.lang.System.currentTimeMillis()



103
104
105
106
107
108
# File 'lib/openwfe/util/otime.rb', line 103

def OpenWFE.current_time_millis ()

    t = Time.new()
    t = t.to_f * 1000
    t.to_i
end

.is_digit?(c) ⇒ Boolean

returns true if the character c is a digit

Returns:

  • (Boolean)


166
167
168
169
170
# File 'lib/openwfe/util/otime.rb', line 166

def OpenWFE.is_digit? (c)
    return false if not c.kind_of?(String)
    return false if c.length > 1
    (c >= "0" and c <= "9")
end

.nowObject

Returns the current time as an ISO date string



51
52
53
54
# File 'lib/openwfe/util/otime.rb', line 51

def OpenWFE.now ()

    to_iso8601_date(Time.new())
end

.parse_time_string(string) ⇒ Object

turns a string like ‘1m10s’ into a float like ‘70.0’

w -> week d -> day h -> hour m -> minute s -> second M -> month y -> year ‘nada’ -> millisecond



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/openwfe/util/otime.rb', line 122

def OpenWFE.parse_time_string (string)

    string = string.strip

    index = -1
    result = 0.0

    number = ""

    loop do

        index = index + 1

        if index >= string.length
            if number.length > 0
                result = result + (Float(number) / 1000.0)
            end
            break
        end

        c = string[index, 1]

        if is_digit?(c)
            number = number + c
            next
        end

        value = Integer(number)
        number = ""

        multiplier = DURATIONS[c]

        raise "unknown time char '#{c}'" \
            if not multiplier

        result = result + (value * multiplier)
    end

    result
end

.time_to_iso8601_date(time) ⇒ Object

the old method we used to generate our ISO datetime strings



78
79
80
81
82
83
84
85
86
87
# File 'lib/openwfe/util/otime.rb', line 78

def OpenWFE.time_to_iso8601_date (time)

    s = time.getutc().strftime(TIME_FORMAT)
    o = time.utc_offset / 3600
    o = o.to_s + "00"
    o = "0" + o if o.length < 4
    o = "+" + o unless o[0..1] == '-'

    s + " " + o.to_s
end

.to_datetime(time) ⇒ Object

converts a Time instance to a DateTime one



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/openwfe/util/otime.rb', line 184

def OpenWFE.to_datetime (time)

    s = time.sec + Rational(time.usec, 10**6)
    o = Rational(time.utc_offset, 3600 * 24)

    begin

        DateTime.new(
            time.year, 
            time.month, 
            time.day, 
            time.hour, 
            time.min, 
            s, 
            o)

    rescue Exception => e

        #puts
        #puts OpenWFE::exception_to_s(e)
        #puts
        #puts \
        #    "\n Date.new() problem. Params :"+
        #    "\n....y:#{time.year} M:#{time.month} d:#{time.day} "+
        #    "h:#{time.hour} m:#{time.min} s:#{s} o:#{o}"

        DateTime.new(
            time.year, 
            time.month, 
            time.day, 
            time.hour, 
            time.min, 
            time.sec, 
            time.utc_offset)
    end
end

.to_gm_time(dtime) ⇒ Object



221
222
223
# File 'lib/openwfe/util/otime.rb', line 221

def OpenWFE.to_gm_time (dtime)
    to_ttime(dtime.new_offset, :gm)
end

.to_iso8601_date(date) ⇒ Object

As the name implies.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openwfe/util/otime.rb', line 59

def OpenWFE.to_iso8601_date (date)

    if date.kind_of? Float 
        date = to_datetime(Time.at(date))
    elsif date.kind_of? Time
        date = to_datetime(date)
    elsif not date.kind_of? Date
        date = DateTime.parse(date)
    end

    s = date.to_s # this is costly
    s[10] = " "

    s
end

.to_local_time(dtime) ⇒ Object



225
226
227
# File 'lib/openwfe/util/otime.rb', line 225

def OpenWFE.to_local_time (dtime)
    to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local)
end

.to_ruby_time(iso_date) ⇒ Object

Returns a Ruby time



92
93
94
95
# File 'lib/openwfe/util/otime.rb', line 92

def OpenWFE.to_ruby_time (iso_date)

    DateTime.parse(iso_date)
end

.to_ttime(d, method) ⇒ Object



229
230
231
232
# File 'lib/openwfe/util/otime.rb', line 229

def OpenWFE.to_ttime (d, method)
    usec = (d.sec_fraction * 3600 * 24 * (10**6)).to_i
    Time.send(method, d.year, d.month, d.day, d.hour, d.min, d.sec, usec)
end