Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/format.rb,
lib/hijri/gregorian.rb

Overview

format.rb: Written by Tadayoshi Funaba 1999-2009 $Id: format.rb,v 2.43 2008-01-17 20:16:31+09 tadf Exp $

Direct Known Subclasses

DateTime

Defined Under Namespace

Modules: Format

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._httpdate(str) ⇒ Object

:nodoc:



1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/format.rb', line 1195

def self._httpdate(str) # :nodoc:
  if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+
  \d{2}\s+
  (#{Format::ABBR_MONTHS.keys.join('|')})\s+
  -?\d{4}\s+ # allow minus, anyway
  \d{2}:\d{2}:\d{2}\s+
  gmt\s*\z/iox =~ str
    _rfc2822(str)
  elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+
  \d{2}\s*-\s*
  (#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s*
  \d{2}\s+
  \d{2}:\d{2}:\d{2}\s+
  gmt\s*\z/iox =~ str
    _parse(str)
  elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+
  (#{Format::ABBR_MONTHS.keys.join('|')})\s+
  \d{1,2}\s+
  \d{2}:\d{2}:\d{2}\s+
  \d{4}\s*\z/iox =~ str
    _parse(str)
  end
end

._iso8601(str) ⇒ Object

:nodoc:



1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
# File 'lib/format.rb', line 1096

def self._iso8601(str) # :nodoc:
  if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}|
     ([-+]?\d{2,})?-\d{3}|
     (\d{2}|\d{4})?-w\d{2}-\d|
     -w-\d)
  (t
  \d{2}:\d{2}(:\d{2}([,.]\d+)?)?
  (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
    _parse(str)
  elsif /\A\s*(([-+]?(\d{2}|\d{4})|--)\d{2}\d{2}|
     ([-+]?(\d{2}|\d{4}))?\d{3}|-\d{3}|
     (\d{2}|\d{4})?w\d{2}\d)
  (t?
  \d{2}\d{2}(\d{2}([,.]\d+)?)?
  (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
    _parse(str)
  elsif /\A\s*(\d{2}:\d{2}(:\d{2}([,.]\d+)?)?
  (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
    _parse(str)
  elsif /\A\s*(\d{2}\d{2}(\d{2}([,.]\d+)?)?
  (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
    _parse(str)
  end
end

._jisx0301(str) ⇒ Object

:nodoc:



1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
# File 'lib/format.rb', line 1219

def self._jisx0301(str) # :nodoc:
  if /\A\s*[mtsh]?\d{2}\.\d{2}\.\d{2}
  (t
  (\d{2}:\d{2}(:\d{2}([,.]\d*)?)?
  (z|[-+]\d{2}(:?\d{2})?)?)?)?\s*\z/ix =~ str
    if /\A\s*\d/ =~ str
  _parse(str.sub(/\A\s*(\d)/, 'h\1'))
    else
  _parse(str)
    end
  else
    _iso8601(str)
  end
end

._parse(str, comp = true) ⇒ Object



1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
# File 'lib/format.rb', line 1030

def self._parse(str, comp=true)
  str = str.dup

  e = Format::Bag.new

  e._comp = comp

  str.gsub!(/[^-+',.\/:@[:alnum:]\[\]]+/, ' ')

  _parse_time(str, e) # || _parse_beat(str, e)
  _parse_day(str, e)

  _parse_eu(str, e)     ||
  _parse_us(str, e)     ||
  _parse_iso(str, e)    ||
  _parse_jis(str, e)    ||
  _parse_vms(str, e)    ||
  _parse_sla(str, e)    ||
  _parse_dot(str, e)    ||
  _parse_iso2(str, e)   ||
  _parse_year(str, e)   ||
  _parse_mon(str, e)    ||
  _parse_mday(str, e)   ||
  _parse_ddd(str, e)

  if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/i, ' ')
    if e.year
  e.year = -e.year + 1
    end
  end

  if str.sub!(/\A\s*(\d{1,2})\s*\z/, ' ')
    if e.hour && !e.mday
  v = $1.to_i
  if (1..31) === v
 e.mday = v
  end
    end
    if e.mday && !e.hour
  v = $1.to_i
  if (0..24) === v
 e.hour = v
  end
    end
  end

  if e._comp
    if e.cwyear
  if e.cwyear >= 0 && e.cwyear <= 99
 e.cwyear += if e.cwyear >= 69
      then 1900 else 2000 end
  end
    end
    if e.year
  if e.year >= 0 && e.year <= 99
 e.year += if e.year >= 69
    then 1900 else 2000 end
  end
    end
  end

  e.offset ||= zone_to_diff(e.zone) if e.zone

  e.to_hash
end

._rfc2822(str) ⇒ Object Also known as: _rfc822

:nodoc:



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
# File 'lib/format.rb', line 1174

def self._rfc2822(str) # :nodoc:
  if /\A\s*(?:(?:#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)?
  \d{1,2}\s+
  (?:#{Format::ABBR_MONTHS.keys.join('|')})\s+
  -?(\d{2,})\s+ # allow minus, anyway
  \d{2}:\d{2}(:\d{2})?\s*
  (?:[-+]\d{4}|ut|gmt|e[sd]t|c[sd]t|m[sd]t|p[sd]t|[a-ik-z])\s*\z/iox =~ str
    e = _parse(str, false)
    if $1.size < 4
  if e[:year] < 50
 e[:year] += 2000
  elsif e[:year] < 1000
 e[:year] += 1900
  end
    end
    e
  end
end

._rfc3339(str) ⇒ Object

:nodoc:



1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/format.rb', line 1121

def self._rfc3339(str) # :nodoc:
  if /\A\s*-?\d{4}-\d{2}-\d{2} # allow minus, anyway
  (t|\s)
  \d{2}:\d{2}:\d{2}(\.\d+)?
  (z|[-+]\d{2}:\d{2})\s*\z/ix =~ str
    _parse(str)
  end
end

._strptime(str, fmt = '%F') ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/format.rb', line 574

def self._strptime(str, fmt='%F')
  str = str.dup
  e = Format::Bag.new
  return unless _strptime_i(str, fmt, e)

  if e._cent
    if e.cwyear
  e.cwyear += e._cent * 100
    end
    if e.year
  e.  year += e._cent * 100
    end
  end

  if e._merid
    if e.hour
  e.hour %= 12
  e.hour += e._merid
    end
  end

  unless str.empty?
    e.leftover = str
  end

  e.to_hash
end

._xmlschema(str) ⇒ Object

:nodoc:



1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'lib/format.rb', line 1130

def self._xmlschema(str) # :nodoc:
  if /\A\s*(-?\d{4,})(?:-(\d{2})(?:-(\d{2}))?)?
  (?:t
 (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?)?
  (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
    e = Format::Bag.new
    e.year = $1.to_i
    e.mon = $2.to_i if $2
    e.mday = $3.to_i if $3
    e.hour = $4.to_i if $4
    e.min = $5.to_i if $5
    e.sec = $6.to_i if $6
    e.sec_fraction = Rational($7.to_i, 10**$7.size) if $7
    if $8
  e.zone = $8
  e.offset = zone_to_diff($8)
    end
    e.to_hash
  elsif /\A\s*(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?
  (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
    e = Format::Bag.new
    e.hour = $1.to_i if $1
    e.min = $2.to_i if $2
    e.sec = $3.to_i if $3
    e.sec_fraction = Rational($4.to_i, 10**$4.size) if $4
    if $5
  e.zone = $5
  e.offset = zone_to_diff($5)
    end
    e.to_hash
  elsif /\A\s*(?:--(\d{2})(?:-(\d{2}))?|---(\d{2}))
  (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
    e = Format::Bag.new
    e.mon = $1.to_i if $1
    e.mday = $2.to_i if $2
    e.mday = $3.to_i if $3
    if $4
  e.zone = $4
  e.offset = zone_to_diff($4)
    end
    e.to_hash
  end
end

Instance Method Details

#asctimeObject Also known as: ctime

alias_method :format, :strftime



341
# File 'lib/format.rb', line 341

def asctime() strftime('%c') end

#httpdateObject

:nodoc:



355
# File 'lib/format.rb', line 355

def httpdate() new_offset(0).strftime('%a, %d %b %Y %T GMT') end

#iso8601Object



345
# File 'lib/format.rb', line 345

def iso8601() strftime('%F') end

#jisx0301Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/format.rb', line 357

def jisx0301
  if jd < 2405160
    iso8601
  else
    case jd
    when 2405160...2419614
  g = 'M%02d' % (year - 1867)
    when 2419614...2424875
  g = 'T%02d' % (year - 1911)
    when 2424875...2447535
  g = 'S%02d' % (year - 1925)
    else
  g = 'H%02d' % (year - 1988)
    end
    g + strftime('.%m.%d')
  end
end

#last_day_of_gregorian_month(month) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/hijri/gregorian.rb', line 6

def last_day_of_gregorian_month(month)
  # Compute the last date of the month for the Gregorian calendar.
  if month == 2
    return 29 if (self.year % 4 == 0 && self.year % 100 != 0) || (self.year % 400 == 0)
  end
  return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]
end

#rfc2822Object Also known as: rfc822



351
# File 'lib/format.rb', line 351

def rfc2822() strftime('%a, %-d %b %Y %T %z') end

#rfc3339Object



347
# File 'lib/format.rb', line 347

def rfc3339() iso8601 end

#strftime(fmt = '%F') ⇒ Object



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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/format.rb', line 215

def strftime(fmt='%F')
  fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do
    f = {}
    m = $&
    s, w, c = $1, $2, $3
    if s
  s.scan(/./) do |k|
 case k
 when '-'; f[:p] = '-'
 when '_'; f[:p] = "\s"
 when '0'; f[:p] = '0'
 when '^'; f[:u] = true
 when '#'; f[:x] = true
 end
  end
    end
    if w
  f[:w] = w.to_i
    end
    case c
    when 'A'; emit_ad(DAYNAMES[wday], 0, f)
    when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
    when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
    when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
    when 'C', 'EC'; emit_sn((year / 100).floor, 2, f)
    when 'c', 'Ec'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
    when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
    when 'd', 'Od'; emit_n(mday, 2, f)
    when 'e', 'Oe'; emit_a(mday, 2, f)
    when 'F'
  if m == '%F'
 format('%.4d-%02d-%02d', year, mon, mday) # 4p
  else
 emit_a(strftime('%Y-%m-%d'), 0, f)
  end
    when 'G'; emit_sn(cwyear, 4, f)
    when 'g'; emit_n(cwyear % 100, 2, f)
    when 'H', 'OH'; emit_n(hour, 2, f)
    when 'h'; emit_ad(strftime('%b'), 0, f)
    when 'I', 'OI'; emit_n((hour % 12).nonzero? || 12, 2, f)
    when 'j'; emit_n(yday, 3, f)
    when 'k'; emit_a(hour, 2, f)
    when 'L'
  f[:p] = nil
  w = f[:w] || 3
  u = 10**w
  emit_n((sec_fraction * u).floor, w, f)
    when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
    when 'M', 'OM'; emit_n(min, 2, f)
    when 'm', 'Om'; emit_n(mon, 2, f)
    when 'N'
  f[:p] = nil
  w = f[:w] || 9
  u = 10**w
  emit_n((sec_fraction * u).floor, w, f)
    when 'n'; emit_a("\n", 0, f)
    when 'P'; emit_ad(strftime('%p').downcase, 0, f)
    when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
    when 'Q'
  s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round
  emit_sn(s, 1, f)
    when 'R'; emit_a(strftime('%H:%M'), 0, f)
    when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
    when 'S', 'OS'; emit_n(sec, 2, f)
    when 's'
  s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round
  emit_sn(s, 1, f)
    when 'T'
  if m == '%T'
 format('%02d:%02d:%02d', hour, min, sec) # 4p
  else
 emit_a(strftime('%H:%M:%S'), 0, f)
  end
    when 't'; emit_a("\t", 0, f)
    when 'U', 'W', 'OU', 'OW'
  emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f)
    when 'u', 'Ou'; emit_n(cwday, 1, f)
    when 'V', 'OV'; emit_n(cweek, 2, f)
    when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
    when 'w', 'Ow'; emit_n(wday, 1, f)
    when 'X', 'EX'; emit_a(strftime('%H:%M:%S'), 0, f)
    when 'x', 'Ex'; emit_a(strftime('%m/%d/%y'), 0, f)
    when 'Y', 'EY'; emit_sn(year, 4, f)
    when 'y', 'Ey', 'Oy'; emit_n(year % 100, 2, f)
    when 'Z'; emit_au(strftime('%:z'), 0, f)
    when /\A(:{0,3})z/
  t = $1.size
  sign = if offset < 0 then -1 else +1 end
  fr = offset.abs
  ss = fr.div(SECONDS_IN_DAY) # 4p
  hh, ss = ss.divmod(3600)
  mm, ss = ss.divmod(60)
  if t == 3
 if    ss.nonzero? then t =  2
 elsif mm.nonzero? then t =  1
 else                   t = -1
 end
  end
  case t
  when -1
 tail = []
 sep = ''
  when 0
 f[:w] -= 2 if f[:w]
 tail = ['%02d' % mm]
 sep = ''
  when 1
 f[:w] -= 3 if f[:w]
 tail = ['%02d' % mm]
 sep = ':'
  when 2
 f[:w] -= 6 if f[:w]
 tail = ['%02d' % mm, '%02d' % ss]
 sep = ':'
  end
  ([emit_z(sign * hh, 2, f)] + tail).join(sep)
    when '%'; emit_a('%', 0, f)
    when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
    else
  m
    end
  end
end

#to_absObject



14
15
16
17
18
19
20
21
# File 'lib/hijri/gregorian.rb', line 14

def to_abs
  @d = self.day
  @m = self.month - 1
  @m.downto(1) do |m|
    @d += last_day_of_gregorian_month(@m, year)
  end
  return (@d + 365 * (year - 1) + (year -1) / 4.0 - (year - 1) / 100.0 + (year - 1) / 400.0).to_i
end

#to_hijriObject



2
3
4
# File 'lib/hijri/gregorian.rb', line 2

def to_hijri
  Hijri::Date.new *Hijri::Converter.greo_to_hijri(self)
end

#xmlschemaObject

:nodoc:



349
# File 'lib/format.rb', line 349

def xmlschema() iso8601 end