summaryrefslogtreecommitdiff
path: root/ext/date/lib
diff options
context:
space:
mode:
Diffstat (limited to 'ext/date/lib')
-rw-r--r--ext/date/lib/date/format.rb648
1 files changed, 56 insertions, 592 deletions
diff --git a/ext/date/lib/date/format.rb b/ext/date/lib/date/format.rb
index 00fcb363ef..c57ad22474 100644
--- a/ext/date/lib/date/format.rb
+++ b/ext/date/lib/date/format.rb
@@ -1,4 +1,4 @@
-# format.rb: Written by Tadayoshi Funaba 1999-2010
+# format.rb: Written by Tadayoshi Funaba 1999-2011
class Date
@@ -108,31 +108,8 @@ class Date
x.freeze
end
- class BagStruct < Struct # :nodoc:
-
- def to_hash
- h = {}
- members.each do |k|
- unless /\A_/ =~ k.to_s || self[k].nil?
- h[k] = self[k]
- end
- end
- h
- end
-
- end
-
- Bag = BagStruct.new(:year, :mon, :yday, :mday, :wday,
- :cwyear, :cweek, :cwday,
- :hour, :min, :sec, :sec_fraction,
- :wnum0, :wnum1, :seconds,
- :zone, :offset, :leftover,
- :_cent, :_merid, :_comp)
-
end
-# alias_method :format, :strftime
-
def asctime() strftime('%c') end
alias_method :ctime, :asctime
@@ -167,512 +144,6 @@ class Date
end
end
-=begin
- def beat(n=0)
- i, f = (new_offset(HOURS_IN_DAY).day_fraction * 1000).divmod(1)
- ('@%03d' % i) +
- if n < 1
- ''
- else
- '.%0*d' % [n, (f / Rational(1, 10**n)).round]
- end
- end
-=end
-
- def self.s3e(e, y, m, d, bc=false)
- unless String === m
- m = m.to_s
- end
-
- if y && m && !d
- y, m, d = d, y, m
- end
-
- if y == nil
- if d && d.size > 2
- y = d
- d = nil
- end
- if d && d[0,1] == "'"
- y = d
- d = nil
- end
- end
-
- if y
- y.scan(/(\d+)(.+)?/)
- if $2
- y, d = d, $1
- end
- end
-
- if m
- if m[0,1] == "'" || m.size > 2
- y, m, d = m, d, y # us -> be
- end
- end
-
- if d
- if d[0,1] == "'" || d.size > 2
- y, d = d, y
- end
- end
-
- if y
- y =~ /([-+])?(\d+)/
- if $1 || $2.size > 2
- c = false
- end
- iy = $&.to_i
- if bc
- iy = -iy + 1
- end
- e.year = iy
- end
-
- if m
- m =~ /\d+/
- e.mon = $&.to_i
- end
-
- if d
- d =~ /\d+/
- e.mday = $&.to_i
- end
-
- if c != nil
- e._comp = c
- end
-
- end
-
- private_class_method :s3e
-
- def self._parse_day(str, e) # :nodoc:
- if str.sub!(/\b(#{Format::ABBR_DAYS.keys.join('|')})[^-\d\s]*/io, ' ')
- e.wday = Format::ABBR_DAYS[$1.downcase]
- true
-=begin
- elsif str.sub!(/\b(?!\dth)(su|mo|tu|we|th|fr|sa)\b/i, ' ')
- e.wday = %w(su mo tu we th fr sa).index($1.downcase)
- true
-=end
- end
- end
-
- def self._parse_time(str, e) # :nodoc:
- if str.sub!(
- /(
- (?:
- \d+\s*:\s*\d+
- (?:
- \s*:\s*\d+(?:[,.]\d*)?
- )?
- |
- \d+\s*h(?:\s*\d+m?(?:\s*\d+s?)?)?
- )
- (?:
- \s*
- [ap](?:m\b|\.m\.)
- )?
- |
- \d+\s*[ap](?:m\b|\.m\.)
- )
- (?:
- \s*
- (
- (?:gmt|utc?)?[-+]\d+(?:[,.:]\d+(?::\d+)?)?
- |
- [[:alpha:].\s]+(?:standard|daylight)\stime\b
- |
- [[:alpha:]]+(?:\sdst)?\b
- )
- )?
- /ix,
- ' ')
-
- t = $1
- e.zone = $2 if $2
-
- t =~ /\A(\d+)h?
- (?:\s*:?\s*(\d+)m?
- (?:
- \s*:?\s*(\d+)(?:[,.](\d+))?s?
- )?
- )?
- (?:\s*([ap])(?:m\b|\.m\.))?/ix
-
- e.hour = $1.to_i
- 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.hour %= 12
- if $5.downcase == 'p'
- e.hour += 12
- end
- end
- true
- end
- end
-
-=begin
- def self._parse_beat(str, e) # :nodoc:
- if str.sub!(/@\s*(\d+)(?:[,.](\d*))?/, ' ')
- beat = Rational($1.to_i)
- beat += Rational($2.to_i, 10**$2.size) if $2
- secs = Rational(beat, 1000)
- h, min, s, fr = self.day_fraction_to_time(secs)
- e.hour = h
- e.min = min
- e.sec = s
- e.sec_fraction = fr * 86400
- e.zone = '+01:00'
- true
- end
- end
-=end
-
- def self._parse_eu(str, e) # :nodoc:
- if str.sub!(
- /'?(\d+)[^-\d\s]*
- \s*
- (#{Format::ABBR_MONTHS.keys.join('|')})[^-\d\s']*
- (?:
- \s*
- (c(?:e|\.e\.)|b(?:ce|\.c\.e\.)|a(?:d|\.d\.)|b(?:c|\.c\.))?
- \s*
- ('?-?\d+(?:(?:st|nd|rd|th)\b)?)
- )?
- /iox,
- ' ') # '
- s3e(e, $4, Format::ABBR_MONTHS[$2.downcase], $1,
- $3 && $3[0,1].downcase == 'b')
- true
- end
- end
-
- def self._parse_us(str, e) # :nodoc:
- if str.sub!(
- /\b(#{Format::ABBR_MONTHS.keys.join('|')})[^-\d\s']*
- \s*
- ('?\d+)[^-\d\s']*
- (?:
- \s*
- (c(?:e|\.e\.)|b(?:ce|\.c\.e\.)|a(?:d|\.d\.)|b(?:c|\.c\.))?
- \s*
- ('?-?\d+)
- )?
- /iox,
- ' ') # '
- s3e(e, $4, Format::ABBR_MONTHS[$1.downcase], $2,
- $3 && $3[0,1].downcase == 'b')
- true
- end
- end
-
- def self._parse_iso(str, e) # :nodoc:
- if str.sub!(/('?[-+]?\d+)-(\d+)-('?-?\d+)/, ' ')
- s3e(e, $1, $2, $3)
- true
- end
- end
-
- def self._parse_iso2(str, e) # :nodoc:
- if str.sub!(/\b(\d{2}|\d{4})?-?w(\d{2})(?:-?(\d))?\b/i, ' ')
- e.cwyear = $1.to_i if $1
- e.cweek = $2.to_i
- e.cwday = $3.to_i if $3
- true
- elsif str.sub!(/-w-(\d)\b/i, ' ')
- e.cwday = $1.to_i
- true
- elsif str.sub!(/--(\d{2})?-(\d{2})\b/, ' ')
- e.mon = $1.to_i if $1
- e.mday = $2.to_i
- true
- elsif str.sub!(/--(\d{2})(\d{2})?\b/, ' ')
- e.mon = $1.to_i
- e.mday = $2.to_i if $2
- true
- elsif /[,.](\d{2}|\d{4})-\d{3}\b/ !~ str &&
- str.sub!(/\b(\d{2}|\d{4})-(\d{3})\b/, ' ')
- e.year = $1.to_i
- e.yday = $2.to_i
- true
- elsif /\d-\d{3}\b/ !~ str &&
- str.sub!(/\b-(\d{3})\b/, ' ')
- e.yday = $1.to_i
- true
- end
- end
-
- def self._parse_jis(str, e) # :nodoc:
- if str.sub!(/\b([mtsh])(\d+)\.(\d+)\.(\d+)/i, ' ')
- era = { 'm'=>1867,
- 't'=>1911,
- 's'=>1925,
- 'h'=>1988
- }[$1.downcase]
- e.year = $2.to_i + era
- e.mon = $3.to_i
- e.mday = $4.to_i
- true
- end
- end
-
- def self._parse_vms(str, e) # :nodoc:
- if str.sub!(/('?-?\d+)-(#{Format::ABBR_MONTHS.keys.join('|')})[^-]*
- -('?-?\d+)/iox, ' ')
- s3e(e, $3, Format::ABBR_MONTHS[$2.downcase], $1)
- true
- elsif str.sub!(/\b(#{Format::ABBR_MONTHS.keys.join('|')})[^-]*
- -('?-?\d+)(?:-('?-?\d+))?/iox, ' ')
- s3e(e, $3, Format::ABBR_MONTHS[$1.downcase], $2)
- true
- end
- end
-
- def self._parse_sla(str, e) # :nodoc:
- if str.sub!(%r|('?-?\d+)/\s*('?\d+)(?:\D\s*('?-?\d+))?|, ' ') # '
- s3e(e, $1, $2, $3)
- true
- end
- end
-
- def self._parse_dot(str, e) # :nodoc:
- if str.sub!(%r|('?-?\d+)\.\s*('?\d+)\.\s*('?-?\d+)|, ' ') # '
- s3e(e, $1, $2, $3)
- true
- end
- end
-
- def self._parse_year(str, e) # :nodoc:
- if str.sub!(/'(\d+)\b/, ' ')
- e.year = $1.to_i
- true
- end
- end
-
- def self._parse_mon(str, e) # :nodoc:
- if str.sub!(/\b(#{Format::ABBR_MONTHS.keys.join('|')})\S*/io, ' ')
- e.mon = Format::ABBR_MONTHS[$1.downcase]
- true
- end
- end
-
- def self._parse_mday(str, e) # :nodoc:
- if str.sub!(/(\d+)(st|nd|rd|th)\b/i, ' ')
- e.mday = $1.to_i
- true
- end
- end
-
- def self._parse_ddd(str, e) # :nodoc:
- if str.sub!(
- /([-+]?)(\d{2,14})
- (?:
- \s*
- t?
- \s*
- (\d{2,6})?(?:[,.](\d*))?
- )?
- (?:
- \s*
- (
- z\b
- |
- [-+]\d{1,4}\b
- |
- \[[-+]?\d[^\]]*\]
- )
- )?
- /ix,
- ' ')
- case $2.size
- when 2
- if $3.nil? && $4
- e.sec = $2[-2, 2].to_i
- else
- e.mday = $2[ 0, 2].to_i
- end
- when 4
- if $3.nil? && $4
- e.sec = $2[-2, 2].to_i
- e.min = $2[-4, 2].to_i
- else
- e.mon = $2[ 0, 2].to_i
- e.mday = $2[ 2, 2].to_i
- end
- when 6
- if $3.nil? && $4
- e.sec = $2[-2, 2].to_i
- e.min = $2[-4, 2].to_i
- e.hour = $2[-6, 2].to_i
- else
- e.year = ($1 + $2[ 0, 2]).to_i
- e.mon = $2[ 2, 2].to_i
- e.mday = $2[ 4, 2].to_i
- end
- when 8, 10, 12, 14
- if $3.nil? && $4
- e.sec = $2[-2, 2].to_i
- e.min = $2[-4, 2].to_i
- e.hour = $2[-6, 2].to_i
- e.mday = $2[-8, 2].to_i
- if $2.size >= 10
- e.mon = $2[-10, 2].to_i
- end
- if $2.size == 12
- e.year = ($1 + $2[-12, 2]).to_i
- end
- if $2.size == 14
- e.year = ($1 + $2[-14, 4]).to_i
- e._comp = false
- end
- else
- e.year = ($1 + $2[ 0, 4]).to_i
- e.mon = $2[ 4, 2].to_i
- e.mday = $2[ 6, 2].to_i
- e.hour = $2[ 8, 2].to_i if $2.size >= 10
- e.min = $2[10, 2].to_i if $2.size >= 12
- e.sec = $2[12, 2].to_i if $2.size >= 14
- e._comp = false
- end
- when 3
- if $3.nil? && $4
- e.sec = $2[-2, 2].to_i
- e.min = $2[-3, 1].to_i
- else
- e.yday = $2[ 0, 3].to_i
- end
- when 5
- if $3.nil? && $4
- e.sec = $2[-2, 2].to_i
- e.min = $2[-4, 2].to_i
- e.hour = $2[-5, 1].to_i
- else
- e.year = ($1 + $2[ 0, 2]).to_i
- e.yday = $2[ 2, 3].to_i
- end
- when 7
- if $3.nil? && $4
- e.sec = $2[-2, 2].to_i
- e.min = $2[-4, 2].to_i
- e.hour = $2[-6, 2].to_i
- e.mday = $2[-7, 1].to_i
- else
- e.year = ($1 + $2[ 0, 4]).to_i
- e.yday = $2[ 4, 3].to_i
- end
- end
- if $3
- if $4
- case $3.size
- when 2, 4, 6
- e.sec = $3[-2, 2].to_i
- e.min = $3[-4, 2].to_i if $3.size >= 4
- e.hour = $3[-6, 2].to_i if $3.size >= 6
- end
- else
- case $3.size
- when 2, 4, 6
- e.hour = $3[ 0, 2].to_i
- e.min = $3[ 2, 2].to_i if $3.size >= 4
- e.sec = $3[ 4, 2].to_i if $3.size >= 6
- end
- end
- end
- if $4
- e.sec_fraction = Rational($4.to_i, 10**$4.size)
- end
- if $5
- e.zone = $5
- if e.zone[0,1] == '['
- o, n, = e.zone[1..-2].split(':')
- e.zone = n || o
- if /\A\d/ =~ o
- o = format('+%s', o)
- end
- e.offset = zone_to_diff(o)
- end
- end
- true
- end
- end
-
- private_class_method :_parse_day, :_parse_time, # :_parse_beat,
- :_parse_eu, :_parse_us, :_parse_iso, :_parse_iso2,
- :_parse_jis, :_parse_vms, :_parse_sla, :_parse_dot,
- :_parse_year, :_parse_mon, :_parse_mday, :_parse_ddd
-
- 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
-
def self._iso8601(str) # :nodoc:
if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}|
([-+]?\d{2,})?-\d{3}|
@@ -712,42 +183,42 @@ class Date
(?: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
+ e = {}
+ 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)
+ e[:zone] = $8
+ e[:offset] = zone_to_diff($8)
end
- e.to_hash
+ e
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
+ e = {}
+ 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)
+ e[:zone] = $5
+ e[:offset] = zone_to_diff($5)
end
- e.to_hash
+ e
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
+ e = {}
+ 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)
+ e[:zone] = $4
+ e[:offset] = zone_to_diff($4)
end
- e.to_hash
+ e
end
end
@@ -756,7 +227,7 @@ class Date
\d{1,2}\s+
(?:#{Format::ABBR_MONTHS.keys.join('|')})\s+
-?(\d{2,})\s+ # allow minus, anyway
- \d{2}:\d{2}(:\d{2})?\s*
+ \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
@@ -811,48 +282,41 @@ class Date
end
end
- t = Module.new do
-
- private
-
- def zone_to_diff(zone) # :nodoc:
- zone = zone.downcase
- if zone.sub!(/\s+(standard|daylight)\s+time\z/, '')
- dst = $1 == 'daylight'
+ def self.zone_to_diff(zone) # :nodoc:
+ zone = zone.downcase
+ if zone.sub!(/\s+(standard|daylight)\s+time\z/, '')
+ dst = $1 == 'daylight'
+ else
+ dst = zone.sub!(/\s+dst\z/, '')
+ end
+ if Format::ZONES.include?(zone)
+ offset = Format::ZONES[zone]
+ offset += 3600 if dst
+ elsif zone.sub!(/\A(?:gmt|utc?)?([-+])/, '')
+ sign = $1
+ if zone.include?(':')
+ hour, min, sec, = zone.split(':')
+ elsif zone.include?(',') || zone.include?('.')
+ hour, fr, = zone.split(/[,.]/)
+ min = Rational(fr.to_i, 10**fr.size) * 60
else
- dst = zone.sub!(/\s+dst\z/, '')
- end
- if Format::ZONES.include?(zone)
- offset = Format::ZONES[zone]
- offset += 3600 if dst
- elsif zone.sub!(/\A(?:gmt|utc?)?([-+])/, '')
- sign = $1
- if zone.include?(':')
- hour, min, sec, = zone.split(':')
- elsif zone.include?(',') || zone.include?('.')
- hour, fr, = zone.split(/[,.]/)
- min = Rational(fr.to_i, 10**fr.size) * 60
+ if (zone.size % 2) == 1
+ hour = zone[0,1]
+ min = zone[1,2]
+ sec = zone[3,2]
else
- case zone.size
- when 3
- hour = zone[0,1]
- min = zone[1,2]
- else
- hour = zone[0,2]
- min = zone[2,2]
- sec = zone[4,2]
- end
+ hour = zone[0,2]
+ min = zone[2,2]
+ sec = zone[4,2]
end
- offset = hour.to_i * 3600 + min.to_i * 60 + sec.to_i
- offset *= -1 if sign == '-'
end
- offset
+ offset = hour.to_i * 3600 + min.to_i * 60 + sec.to_i
+ offset *= -1 if sign == '-'
end
-
+ offset
end
- extend t
- include t
+ private_class_method :zone_to_diff
end