Class: TimeCursor::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/time_cursor/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(year: '*', month: '*', day: '*', wday: '*', hour: '*', min: '*', sec: 0, msec: nil) ⇒ Matcher

Returns a new instance of Matcher.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/time_cursor/matcher.rb', line 8

def initialize( year: '*', month: '*', day: '*', wday: '*', hour: '*', min: '*', sec: 0, msec: nil )
  if  msec.nil?
    @years    =  Elements.build( year           )
    @months   =  Elements.build( month,  1..12  )
    @days     =  Elements.build( day,    1..31  )
    @wdays    =  Elements.build( wday,   0..6   )
    @hours    =  Elements.build( hour,   0..23  )
    @mins     =  Elements.build( min,    0..59  )
    @secs     =  Elements.build( sec,    0..59  )
    @usecs    =  Elements.new

    if  !@years.empty? || !@months.empty? || !@days.empty? || !@wdays.empty?
      @next_for  =  :next_for_date
      @prev_for  =  :prev_for_date

    elsif  !@hours.empty? || !@mins.empty?
      @next_for  =  :next_for_time
      @prev_for  =  :prev_for_time

    else
      @secs  =  Elements.build( '0-59', 0..59 )    if @secs.empty?
      @next_for  =  :next_for_sec
      @prev_for  =  :prev_for_sec

    end

  else
    @years    =  Elements.new
    @months   =  Elements.new
    @days     =  Elements.new
    @wdays    =  Elements.new
    @hours    =  Elements.new
    @mins     =  Elements.new
    @secs     =  Elements.new
    @usecs    =  Elements.build( msec,   0..999 )
    @usecs.each_with_index do |item, ndx|
      @usecs[ndx]  *= 1000
    end

    @next_for  =  :next_for_usec
    @prev_for  =  :prev_for_usec

  end
end

Instance Method Details

#back_to_day(time, day) ⇒ Object



313
314
315
316
317
318
319
320
321
# File 'lib/time_cursor/matcher.rb', line 313

def back_to_day( time, day )
  date  =  Date.new( time.year, time.month, time.day )
  loop do
    date  =  date.prev_day
    break    if  date.day == day
  end
  date  =  date.next_day
  Time.new( date.year, date.month, date.day ) - 1
end

#back_to_hour(time, hour) ⇒ Object



333
334
335
336
# File 'lib/time_cursor/matcher.rb', line 333

def back_to_hour( time, hour )
  time  -=  60 * 60 * 24    if  time.hour <= hour
  Time.new( time.year, time.month, time.day, hour, 59, 59 )
end

#back_to_min(time, min) ⇒ Object



338
339
340
341
# File 'lib/time_cursor/matcher.rb', line 338

def back_to_min( time, min )
  time  -=  60 * 60    if  time.min <= min
  Time.new( time.year, time.month, time.day, time.hour, min, 59 )
end

#back_to_month(time, month) ⇒ Object



303
304
305
306
307
308
309
310
311
# File 'lib/time_cursor/matcher.rb', line 303

def back_to_month( time, month )
  date  =  Date.new( time.year, time.month, time.day )
  loop do
    date  =  date.prev_month
    break    if  date.month == month
  end
  date  =  date.next_month
  Time.new( date.year, date.month ) - 1
end

#back_to_sec(time, sec) ⇒ Object



343
344
345
346
# File 'lib/time_cursor/matcher.rb', line 343

def back_to_sec( time, sec )
  time  -=  60    if  time.sec <= sec
  Time.new( time.year, time.month, time.day, time.hour, time.min, sec )
end

#back_to_usec(time, usec) ⇒ Object



348
349
350
351
# File 'lib/time_cursor/matcher.rb', line 348

def back_to_usec( time, usec )
  time  -=  1    if  time.usec < usec
  Time.local( time.year, time.month, time.day, time.hour, time.min, time.sec, usec )
end

#back_to_wday(time, wday) ⇒ Object



323
324
325
326
327
328
329
330
331
# File 'lib/time_cursor/matcher.rb', line 323

def back_to_wday( time, wday )
  date  =  Date.new( time.year, time.month, time.day )
  loop do
    date  =  date.prev_day
    break    if  date.wday == wday
  end
  date  =  date.next_day
  Time.new( date.year, date.month, date.day ) - 1
end

#back_to_year(time, year) ⇒ Object



295
296
297
298
299
300
301
# File 'lib/time_cursor/matcher.rb', line 295

def back_to_year( time, year )
  if year < time.year
    Time.new( year + 1 ) - 1
  else
    nil
  end
end

#forward_to_day(time, day) ⇒ Object



257
258
259
260
261
262
263
264
# File 'lib/time_cursor/matcher.rb', line 257

def forward_to_day( time, day )
  date  =  Date.new( time.year, time.month, time.day )
  loop do
    date  =  date.next_day
    break    if  date.day == day
  end
  Time.new( date.year, date.month, date.day )
end

#forward_to_hour(time, hour) ⇒ Object



275
276
277
278
# File 'lib/time_cursor/matcher.rb', line 275

def forward_to_hour( time, hour )
  time  +=  60 * 60 * 24    if hour <= time.hour
  Time.new( time.year, time.month, time.day, hour )
end

#forward_to_min(time, min) ⇒ Object



280
281
282
283
# File 'lib/time_cursor/matcher.rb', line 280

def forward_to_min( time, min )
  time  +=  60 * 60    if min <= time.min
  Time.new( time.year, time.month, time.day, time.hour, min )
end

#forward_to_month(time, month) ⇒ Object



248
249
250
251
252
253
254
255
# File 'lib/time_cursor/matcher.rb', line 248

def forward_to_month( time, month )
  date  =  Date.new( time.year, time.month, time.day )
  loop do
    date  =  date.next_month
    break    if  date.month == month
  end
  Time.new( date.year, date.month )
end

#forward_to_sec(time, sec) ⇒ Object



285
286
287
288
# File 'lib/time_cursor/matcher.rb', line 285

def forward_to_sec( time, sec )
  time  +=  60    if sec <= time.sec
  Time.new( time.year, time.month, time.day, time.hour, time.min, sec )
end

#forward_to_usec(time, usec) ⇒ Object



290
291
292
293
# File 'lib/time_cursor/matcher.rb', line 290

def forward_to_usec( time, usec )
  time  +=  1    if  usec <= time.usec
  Time.local( time.year, time.month, time.day, time.hour, time.min, time.sec, usec )
end

#forward_to_wday(time, wday) ⇒ Object



266
267
268
269
270
271
272
273
# File 'lib/time_cursor/matcher.rb', line 266

def forward_to_wday( time, wday )
  date  =  Date.new( time.year, time.month, time.day )
  loop do
    date  =  date.next_day
    break    if  date.wday == wday
  end
  Time.new( date.year, date.month, date.day )
end

#forward_to_year(time, year) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/time_cursor/matcher.rb', line 240

def forward_to_year( time, year )
  if time.year < year
    Time.new( year )
  else
    nil
  end
end

#match(time = Time.now) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/time_cursor/matcher.rb', line 75

def match( time = Time.now )
  case  time
  when  Time
  when  String
    time  =  Time.parse( time )
  else
    raise  ArgumentError, "can not parse as time."
  end

  if  @years.correspond?( time.year )  &&
      @months.correspond?( time.month )  &&
      @days.correspond?( time.day )  &&
      @hours.correspond?( time.hour )  &&
      @mins.correspond?( time.min )  &&
      @secs.correspond?( time.sec )
    time
  else
    nil
  end
end

#next(time = Time.now) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/time_cursor/matcher.rb', line 53

def next( time = Time.now )
  case  time
  when  Time
  when  String
    time  =  Time.parse( time )
  else
    raise  ArgumentError, "can not parse as time."
  end
  send( @next_for, time )
end

#next_for_date(time) ⇒ Object

private



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/time_cursor/matcher.rb', line 98

def next_for_date( time )
  time  +=  1
  loop do
    if  !@years.correspond?( year = time.year )
      year  =  @years.right( year )
      return  time  =  nil   if  year.nil?
      time  =  forward_to_year( time, year )

    elsif  !@months.correspond?( month = time.month )
      month  =  @months.right( month ) || @months.first
      time  =  forward_to_month( time, month )

    elsif  !@wdays.correspond?( wday = time.wday )
      wday  =  @wdays.right( wday ) || @wdays.first
      time  =  forward_to_wday( time, wday )

    elsif  !@days.correspond?( day = time.day )
      day  =  @days.right( day ) || @days.first
      time  =  forward_to_day( time, day )

    elsif  !@hours.correspond?( hour = time.hour )
      hour  =  @hours.right( hour ) || @hours.first
      time  =  forward_to_hour( time, hour )

    elsif  !@mins.correspond?( min = time.min )
      min  =  @mins.right( min ) || @mins.first
      time  =  forward_to_min( time, min )

    elsif  !@secs.correspond?( sec = time.sec )
      sec  =  @secs.right( sec ) || @secs.first
      time  =  forward_to_sec( time, sec )

    else
      return  time

    end
  end
end

#next_for_sec(time) ⇒ Object



159
160
161
162
# File 'lib/time_cursor/matcher.rb', line 159

def next_for_sec( time )
  sec  =  @secs.right( time.sec ) || @secs.first
  time  =  forward_to_sec( time, sec )
end

#next_for_time(time) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/time_cursor/matcher.rb', line 137

def next_for_time( time )
  time  +=  1
  loop do
    if  !@hours.correspond?( hour = time.hour )
      hour  =  @hours.right( hour ) || @hours.first
      time  =  forward_to_hour( time, hour )

    elsif  !@mins.correspond?( min = time.min )
      min  =  @mins.right( min ) || @mins.first
      time  =  forward_to_min( time, min )

    elsif  !@secs.correspond?( sec = time.sec )
      sec  =  @secs.right( sec ) || @secs.first
      time  =  forward_to_sec( time, sec )

    else
      return  time

    end
  end
end

#next_for_usec(time) ⇒ Object



164
165
166
167
# File 'lib/time_cursor/matcher.rb', line 164

def next_for_usec( time )
  usec  =  @usecs.right( time.usec ) || @usecs.first
  time  =  forward_to_usec( time, usec )
end

#prev(time = Time.now) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/time_cursor/matcher.rb', line 64

def prev( time = Time.now )
  case  time
  when  Time
  when  String
    time  =  Time.parse( time )
  else
    raise  ArgumentError, "can not parse as time."
  end
  send( @prev_for, time )
end

#prev_for_date(time) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/time_cursor/matcher.rb', line 169

def prev_for_date( time )
  time  -=  1
  loop do
    if  !@years.correspond?( year = time.year )
      year  =  @years.left( year )
      return  time  =  nil    if  year.nil?
      time  =  back_to_year( time, year )

    elsif  !@months.correspond?( month = time.month )
      month  =  @months.left( month ) || @months.last
      time  =  back_to_month( time, month )

    elsif  !@wdays.correspond?( wday = time.wday )
      wday  =  @wdays.left( wday ) || @wdays.last
      time  =  back_to_wday( time, wday )

    elsif  !@days.correspond?( day = time.day )
      day  =  @days.left( day ) || @days.last
      time  =  back_to_day( time, day )

    elsif  !@hours.correspond?( hour = time.hour )
      hour  =  @hours.left( hour ) || @hours.last
      time  =  back_to_hour( time, hour )

    elsif  !@mins.correspond?( min = time.min )
      min  =  @mins.left( min ) || @mins.last
      time  =  back_to_min( time, min )

    elsif  !@secs.correspond?( sec = time.sec )
      sec  =  @secs.left( sec ) || @secs.last
      time  =  back_to_sec( time, sec )

    else
      return  time

    end
  end
end

#prev_for_sec(time) ⇒ Object



230
231
232
233
# File 'lib/time_cursor/matcher.rb', line 230

def prev_for_sec( time )
  sec  =  @secs.left( time.sec ) || @secs.last
  time  =  back_to_sec( time, sec )
end

#prev_for_time(time) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/time_cursor/matcher.rb', line 208

def prev_for_time( time )
  time  -=  1
  loop do
    if  !@hours.correspond?( hour = time.hour )
      hour  =  @hours.left( hour ) || @hours.last
      time  =  back_to_hour( time, hour )

    elsif  !@mins.correspond?( min = time.min )
      min  =  @mins.left( min ) || @mins.last
      time  =  back_to_min( time, min )

    elsif  !@secs.correspond?( sec = time.sec )
      sec  =  @secs.left( sec ) || @secs.last
      time  =  back_to_sec( time, sec )

    else
      return  time

    end
  end
end

#prev_for_usec(time) ⇒ Object



235
236
237
238
# File 'lib/time_cursor/matcher.rb', line 235

def prev_for_usec( time )
  usec  =  @usecs.left( time.usec ) || @usecs.last
  time  =  back_to_usec( time, usec )
end