* ext/date/lib/date/format.rb: omitted to call _parse.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31333 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tadf 2011-04-24 17:00:18 +00:00
parent 0c1d2a0af4
commit 60c7967e12
2 changed files with 257 additions and 58 deletions

View File

@ -1,3 +1,7 @@
Mon Apr 25 01:58:50 2011 Tadayoshi Funaba <tadf@dotrb.org>
* ext/date/lib/date/format.rb: omitted to call _parse.
Mon Apr 25 01:03:03 2011 KOSAKI Motohiro <kosaki.motohiro@gmail.com> Mon Apr 25 01:03:03 2011 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* string.c (rb_to_id): remove unused variable. * string.c (rb_to_id): remove unused variable.

View File

@ -145,36 +145,173 @@ class Date
end end
def self._iso8601(str) # :nodoc: def self._iso8601(str) # :nodoc:
if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}| if /\A\s*(?:([-+]?\d{2,}|-)-(\d{2})-(\d{2})|
([-+]?\d{2,})?-\d{3}| ([-+]?\d{2,})?-(\d{3})|
(\d{2}|\d{4})?-w\d{2}-\d| (\d{2}|\d{4})?-w(\d{2})-(\d)|
-w-\d) -w-(\d))
(t (?:t
\d{2}:\d{2}(:\d{2}([,.]\d+)?)? (\d{2}):(\d{2})(?::(\d{2})(?:[,.](\d+))?)?
(z|[-+]\d{2}(?::?\d{2})?)?)?\s*\z/ix =~ str
if $3
e = {
:mon => $2.to_i,
:mday => $3.to_i
}
if $1 != '-'
y = $1.to_i
if $1.size < 4
y += if y >= 69 then 1900 else 2000 end
end
e[:year] = y
end
elsif $5
e = {
:yday => $5.to_i
}
if $4
y = $4.to_i
if $4.size < 4
y += if y >= 69 then 1900 else 2000 end
end
e[:year] = y
end
elsif $8
e = {
:cweek => $7.to_i,
:cwday => $8.to_i
}
if $6
y = $6.to_i
if $6.size < 4
y += if y >= 69 then 1900 else 2000 end
end
e[:cwyear] = y
end
elsif $9
e = {
:cwday => $9.to_i
}
end
if $10
e[:hour] = $10.to_i
e[:min] = $11.to_i
e[:sec] = $12.to_i if $12
end
if $13
e[:sec_fraction] = Rational($13.to_i, 10**$13.size)
end
if $14
e[:zone] = $14
e[:offset] = zone_to_diff($14)
end
e
elsif /\A\s*(?:([-+]?(?:\d{4}|\d{2})|--)(\d{2})(\d{2})|
([-+]?(?:\d{4}|\d{2}))?(\d{3)}|-(\d{3})|
(\d{4}|\d{2})?w(\d{2})(\d))
(?:t?
(\d{2})(\d{2})(?:(\d{2})(?:[,.](\d+))?)?
(z|[-+]\d{2}(?:\d{2})?)?)?\s*\z/ix =~ str
if $3
e = {
:mon => $2.to_i,
:mday => $3.to_i
}
if $1 != '--'
y = $1.to_i
if $1.size < 4
y += if y >= 69 then 1900 else 2000 end
end
e[:year] = y
end
elsif $5
e = {
:yday => $5.to_i
}
if $4
y = $4.to_i
if $4.size < 4
y += if y >= 69 then 1900 else 2000 end
end
e[:year] = y
end
elsif $6
e = {
:yday => $6.to_i
}
elsif $9
e = {
:cweek => $8.to_i,
:cwday => $9.to_i
}
if $7
y = $7.to_i
if $7.size < 4
y += if y >= 69 then 1900 else 2000 end
end
e[:cwyear] = y
end
end
if $10
e[:hour] = $10.to_i
e[:min] = $11.to_i
e[:sec] = $12.to_i if $12
end
if $13
e[:sec_fraction] = Rational($13.to_i, 10**$13.size)
end
if $14
e[:zone] = $14
e[:offset] = zone_to_diff($14)
end
e
elsif /\A\s*(?:(\d{2}):(\d{2})(?::(\d{2})(?:[,.](\d+))?)?
(z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
_parse(str) e = {}
elsif /\A\s*(([-+]?(\d{2}|\d{4})|--)\d{2}\d{2}| e[:hour] = $1.to_i if $1
([-+]?(\d{2}|\d{4}))?\d{3}|-\d{3}| e[:min] = $2.to_i if $2
(\d{2}|\d{4})?w\d{2}\d) e[:sec] = $3.to_i if $3
(t? if $4
\d{2}\d{2}(\d{2}([,.]\d+)?)? e[:sec_fraction] = Rational($4.to_i, 10**$4.size)
end
if $5
e[:zone] = $5
e[:offset] = zone_to_diff($5)
end
e
elsif /\A\s*(?:(\d{2})(\d{2})(?:(\d{2})(?:[,.](\d+))?)?
(z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
_parse(str) e = {}
elsif /\A\s*(\d{2}:\d{2}(:\d{2}([,.]\d+)?)? e[:hour] = $1.to_i if $1
(z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str e[:min] = $2.to_i if $2
_parse(str) e[:sec] = $3.to_i if $3
elsif /\A\s*(\d{2}\d{2}(\d{2}([,.]\d+)?)? if $4
(z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str e[:sec_fraction] = Rational($4.to_i, 10**$4.size)
_parse(str) end
if $5
e[:zone] = $5
e[:offset] = zone_to_diff($5)
end
e
end end
end end
def self._rfc3339(str) # :nodoc: def self._rfc3339(str) # :nodoc:
if /\A\s*-?\d{4}-\d{2}-\d{2} # allow minus, anyway if /\A\s*(-?\d{4})-(\d{2})-(\d{2}) # allow minus, anyway
(t|\s) (?:t|\s)
\d{2}:\d{2}:\d{2}(\.\d+)? (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?
(z|[-+]\d{2}:\d{2})\s*\z/ix =~ str (z|[-+]\d{2}:\d{2})\s*\z/ix =~ str
_parse(str) e = {
:year => $1.to_i,
:mon => $2.to_i,
:mday => $3.to_i,
:hour => $4.to_i,
:min => $5.to_i,
:sec => $6.to_i,
:zone => $8,
:offset => zone_to_diff($8)
}
e[:sec_fraction] = Rational($7.to_i, 10**$7.size) if $7
e
end end
end end
@ -223,20 +360,27 @@ class Date
end end
def self._rfc2822(str) # :nodoc: def self._rfc2822(str) # :nodoc:
if /\A\s*(?:(?:#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)? if /\A\s*(?:(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)?
\d{1,2}\s+ (\d{1,2})\s+
(?:#{Format::ABBR_MONTHS.keys.join('|')})\s+ (#{Format::ABBR_MONTHS.keys.join('|')})\s+
-?(\d{2,})\s+ # allow minus, anyway (-?\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 ([-+]\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) y = $4.to_i
if $1.size < 4 if $4.size < 4
if e[:year] < 50 y += if y >= 50 then 1900 else 2000 end
e[:year] += 2000
elsif e[:year] < 1000
e[:year] += 1900
end
end end
e = {
:wday => Format::ABBR_DAYS[$1.downcase],
:mday => $2.to_i,
:mon => Format::ABBR_MONTHS[$3.downcase],
:year => y,
:hour => $5.to_i,
:min => $6.to_i,
:zone => $8,
:offset => zone_to_diff($8)
}
e[:sec] = $7.to_i if $7
e e
end end
end end
@ -245,38 +389,89 @@ class Date
def self._httpdate(str) # :nodoc: def self._httpdate(str) # :nodoc:
if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+ if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+
\d{2}\s+ (\d{2})\s+
(#{Format::ABBR_MONTHS.keys.join('|')})\s+ (#{Format::ABBR_MONTHS.keys.join('|')})\s+
-?\d{4}\s+ # allow minus, anyway (-?\d{4})\s+ # allow minus, anyway
\d{2}:\d{2}:\d{2}\s+ (\d{2}):(\d{2}):(\d{2})\s+
gmt\s*\z/iox =~ str (gmt)\s*\z/iox =~ str
_rfc2822(str) {
:wday => Format::ABBR_DAYS[$1.downcase],
:mday => $2.to_i,
:mon => Format::ABBR_MONTHS[$3.downcase],
:year => $4.to_i,
:hour => $5.to_i,
:min => $6.to_i,
:sec => $7.to_i,
:zone => $8,
:offset => zone_to_diff($8)
}
elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+ elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+
\d{2}\s*-\s* (\d{2})\s*-\s*
(#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s* (#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s*
\d{2}\s+ (\d{2})\s+
\d{2}:\d{2}:\d{2}\s+ (\d{2}):(\d{2}):(\d{2})\s+
gmt\s*\z/iox =~ str (gmt)\s*\z/iox =~ str
_parse(str) y = $4.to_i
if y >= 0 && y <= 99
y += if y >= 69 then 1900 else 2000 end
end
{
:wday => Format::DAYS[$1.downcase],
:mday => $2.to_i,
:mon => Format::ABBR_MONTHS[$3.downcase],
:year => y,
:hour => $5.to_i,
:min => $6.to_i,
:sec => $7.to_i,
:zone => $8,
:offset => zone_to_diff($8)
}
elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+ elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+
(#{Format::ABBR_MONTHS.keys.join('|')})\s+ (#{Format::ABBR_MONTHS.keys.join('|')})\s+
\d{1,2}\s+ (\d{1,2})\s+
\d{2}:\d{2}:\d{2}\s+ (\d{2}):(\d{2}):(\d{2})\s+
\d{4}\s*\z/iox =~ str (\d{4})\s*\z/iox =~ str
_parse(str) {
:wday => Format::ABBR_DAYS[$1.downcase],
:mon => Format::ABBR_MONTHS[$2.downcase],
:mday => $3.to_i,
:hour => $4.to_i,
:min => $5.to_i,
:sec => $6.to_i,
:year => $7.to_i
}
end end
end end
def self._jisx0301(str) # :nodoc: def self._jisx0301(str) # :nodoc:
if /\A\s*[mtsh]?\d{2}\.\d{2}\.\d{2} if /\A\s*([mtsh])?(\d{2})\.(\d{2})\.(\d{2})
(t (?:t
(\d{2}:\d{2}(:\d{2}([,.]\d*)?)? (?:(\d{2}):(\d{2})(?::(\d{2})(?:[,.](\d*))?)?
(z|[-+]\d{2}(:?\d{2})?)?)?)?\s*\z/ix =~ str (z|[-+]\d{2}(?::?\d{2})?)?)?)?\s*\z/ix =~ str
if /\A\s*\d/ =~ str era = {
_parse(str.sub(/\A\s*(\d)/, 'h\1')) 'm'=>1867,
else 't'=>1911,
_parse(str) 's'=>1925,
'h'=>1988
}[$1 ? $1.downcase : 'h']
e = {
:year => $2.to_i + era,
:mon => $3.to_i,
:mday => $4.to_i
}
if $5
e[:hour] = $5.to_i
e[:min] = $6.to_i if $6
e[:sec] = $7.to_i if $7
end end
if $8
e[:sec_fraction] = Rational($8.to_i, 10**$8.size)
end
if $9
e[:zone] = $9
e[:offset] = zone_to_diff($9)
end
e
else else
_iso8601(str) _iso8601(str)
end end