Class: NHKore::DatetimeParser

Inherits:
Object
  • Object
show all
Extended by:
AttrBool::Ext
Defined in:
lib/nhkore/datetime_parser.rb

Constant Summary collapse

FMTS =

Order matters!

[
  '%Y-%m-%d %H:%M',
  '%Y-%m-%d %H',
  '%Y-%m-%d',
  '%m-%d %H:%M',
  '%Y-%m %H:%M',
  '%m-%d %H',
  '%Y-%m %H',
  '%m-%d',
  '%Y-%m',
  '%d %H:%M',
  '%y %H:%M',
  '%d %H',
  '%Y %H',
  '%H:%M',
  '%d',
  '%Y',
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = nil, month = nil, day = nil, hour = nil, min = nil, sec = nil) ⇒ DatetimeParser

Returns a new instance of DatetimeParser.



156
157
158
159
160
161
162
163
# File 'lib/nhkore/datetime_parser.rb', line 156

def initialize(year = nil,month = nil,day = nil,hour = nil,min = nil,sec = nil)
  super()

  set!(year,month,day,hour,min,sec)

  self.has = false
  @min_or_max = false
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



140
141
142
# File 'lib/nhkore/datetime_parser.rb', line 140

def day
  @day
end

#hourObject

Returns the value of attribute hour.



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

def hour
  @hour
end

#minObject

Returns the value of attribute min.



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

def min
  @min
end

#monthObject

Returns the value of attribute month.



143
144
145
# File 'lib/nhkore/datetime_parser.rb', line 143

def month
  @month
end

#secObject

Returns the value of attribute sec.



144
145
146
# File 'lib/nhkore/datetime_parser.rb', line 144

def sec
  @sec
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Class Method Details

.guess_year(year) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nhkore/datetime_parser.rb', line 41

def self.guess_year(year)
  if year < 1000
    century = Util::JST_YEAR / 100 * 100 # 2120 -> 2100
    millennium = Util::JST_YEAR / 1000 * 1000 # 2120 -> 2000

    # If year <= 23 (2022 -> 23)...
    if year <= ((Util::JST_YEAR % 100) + 1)
      # Assume this century.
      year = century + year
    elsif year >= 100
      # If (2000 + 150) <= 2201 (if current year is 2200)...
      if (millennium + year) <= (Util::JST_YEAR + 1)
        # Assume this millennium.
        # So if the current year is 2200, and year is 150,
        # then it will be 2000 + 150 = 2150.
      elsif millennium >= 1000
        # Assume previous millennium (2000 -> 1000),
        # so year 999 will become 1999.
        millennium -= 1000
      end

      year = millennium + year
    else
      # Assume previous century (2000 -> 1900).
      century -= 100 if century >= 100
      year = century + year
    end
  end

  return year
end

.parse_range(value) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
136
137
138
# File 'lib/nhkore/datetime_parser.rb', line 73

def self.parse_range(value)
  # Do not use unspace_web_str(), want spaces for formats.
  value = Util.strip_web_str(Util.reduce_space(value))
  values = value.split('...',2)

  return nil if values.empty? # For '' or '...'

  # For '2020...' or '...2020'.
  if value.include?('...')
    # values.length is always 2 because of 2 in split() above.

    # For '2020...'.
    if Util.empty_web_str?(values[1])
      values[1] = :infinity
    # For '...2020'.
    elsif Util.empty_web_str?(values[0])
      values[0] = :infinity
    end
  end

  datetimes = [
    DatetimeParser.new, # "From" date time
    DatetimeParser.new, # "To" date time
  ]

  values.each_with_index do |v,i|
    dt = datetimes[i]

    # Minimum/Maximum date time for '2020...' or '...2020'.
    if v == :infinity
      # "From" date time.
      if i == 0
        dt.min!
      # "To" date time.
      else
        dt.max!
      end
    else
      v = Util.strip_web_str(v)

      FMTS.each_with_index do |fmt,j|
        # If don't do this, "%d" values will be parsed using "%d %H".
        # It seems as though strptime() ignores space.
        raise ArgumentError if fmt.include?(' ') && !v.include?(' ')

        # If don't do this, "%y..." values will be parsed using "%d...".
        raise ArgumentError if fmt.start_with?('%d') && v.split(' ')[0].length > 2

        dt.parse!(v,fmt)

        break # No problem; this format worked
      rescue ArgumentError
        # Out of formats.
        raise if j >= (FMTS.length - 1)
      end
    end
  end

  from = datetimes[0]
  to = datetimes[1]

  from.autofill!(:from,to)
  to.autofill!(:to,from)

  return [from.jst_time,to.jst_time]
end

Instance Method Details

#autofill!(type, other) ⇒ Object



165
166
167
168
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/nhkore/datetime_parser.rb', line 165

def autofill!(type,other)
  case type
  when :from
    is_from = true
  when :to
    is_from = false
  else
    raise ArgumentError,"invalid type[#{type}]"
  end

  return self if @min_or_max

  has_small = false
  jst_now = Util.jst_now

  # Must be from smallest to biggest.

  if @has_sec || other.has_sec?
    @sec = other.sec unless @has_sec
    has_small = true
  else
    @sec = if has_small
             jst_now.sec
           else
             is_from ? 0 : 59
           end
  end

  if @has_min || other.has_min?
    @min = other.min unless @has_min
    has_small = true
  else
    @min = if has_small
             jst_now.min
           else
             is_from ? 0 : 59
           end
  end

  if @has_hour || other.has_hour?
    @hour = other.hour unless @has_hour
    has_small = true
  else
    @hour = if has_small
              jst_now.hour
            else
              is_from ? 0 : 23
            end
  end

  if @has_day || other.has_day?
    @day = other.day unless @has_day
    has_small = true
  else
    @day = if has_small
             jst_now.day
           else
             is_from ? 1 : :last_day
           end
  end

  if @has_month || other.has_month?
    @month = other.month unless @has_month
    has_small = true
  else
    @month = if has_small
               jst_now.month
             else
               is_from ? 1 : 12
             end
  end

  if @has_year || other.has_year?
    @year = other.year unless @has_year
    has_small = true # rubocop:disable Lint/UselessAssignment
  else
    @year = if has_small
              jst_now.year
            else
              is_from ? Util::MIN_SANE_YEAR : jst_now.year
            end
  end

  # Must be after setting @year & @month.
  if @day == :last_day
    @day = Date.new(@year,@month,-1).day
  end

  return self
end

#has=(value) ⇒ Object



301
302
303
304
305
306
307
308
# File 'lib/nhkore/datetime_parser.rb', line 301

def has=(value)
  @has_day = value
  @has_hour = value
  @has_min = value
  @has_month = value
  @has_sec = value
  @has_year = value
end

#jst_timeObject



310
311
312
# File 'lib/nhkore/datetime_parser.rb', line 310

def jst_time
  return Util.jst_time(time)
end

#max!Object



256
257
258
259
260
261
# File 'lib/nhkore/datetime_parser.rb', line 256

def max!
  @min_or_max = true

  # Ex: 2020-12-31 23:59:59
  return set!(Util::JST_YEAR,12,31,23,59,59)
end

#min!Object



263
264
265
266
267
268
# File 'lib/nhkore/datetime_parser.rb', line 263

def min!
  @min_or_max = true

  # Ex: 1924-01-01 00:00:00
  return set!(Util::MIN_SANE_YEAR,1,1,0,0,0)
end

#parse!(value, fmt) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/nhkore/datetime_parser.rb', line 270

def parse!(value,fmt)
  value = Time.strptime(value,fmt,&self.class.method(:guess_year))

  @has_day = fmt.include?('%d')
  @has_hour = fmt.include?('%H')
  @has_min = fmt.include?('%M')
  @has_month = fmt.include?('%m')
  @has_sec = fmt.include?('%S')
  @has_year = fmt.include?('%Y')

  @day = value.day if @has_day
  @hour = value.hour if @has_hour
  @min = value.min if @has_min
  @month = value.month if @has_month
  @sec = value.sec if @has_sec
  @year = value.year if @has_year

  return self
end

#set!(year = nil, month = nil, day = nil, hour = nil, min = nil, sec = nil) ⇒ Object



290
291
292
293
294
295
296
297
298
299
# File 'lib/nhkore/datetime_parser.rb', line 290

def set!(year = nil,month = nil,day = nil,hour = nil,min = nil,sec = nil)
  @year = year
  @month = month
  @day = day
  @hour = hour
  @min = min
  @sec = sec

  return self
end

#timeObject



314
315
316
# File 'lib/nhkore/datetime_parser.rb', line 314

def time
  return Time.new(@year,@month,@day,@hour,@min,@sec)
end

#to_sObject



318
319
320
# File 'lib/nhkore/datetime_parser.rb', line 318

def to_s
  return "#{@year}-#{@month}-#{@day} #{@hour}:#{@min}:#{@sec}"
end