Questions about Date.strptime

Hi all,

I want to parse some strings into Date object and I find in rdoc that
strptime is the right place to go. For some formats of string it's
really easy to parse it. For instance, '%Y.%m.%d' will parse something
like '2007.09.12'. But I have two kinds of string that I don't now how
to parse.

'20070912': there is no '.', '-' or '/' as the seperator and '%Y%m%d'
doesn't work, it will throw an exception saying that: "ArgumentError: 3
elements of civil date are necessary".

'2007.09': in this kind of string, I don't care about the exactly
date(in fact, the data I received is lacked of that information). As a
result, '%Y.%m' doesn't work with the same error as above.

How can I deal with this two kinds of string? Thanks a lot.

···

--
Posted via http://www.ruby-forum.com/.

Allen Young wrote:

strptime is the right place to go. For some formats of string it's [...]

'20070912': there is no '.', '-' or '/' as the seperator and '%Y%m%d'
doesn't work, it will throw an exception saying that: "ArgumentError: 3
elements of civil date are necessary".

looks like it would be handy if there is something like

  Date.strptime("010207", "%2m%2d%2y").to_s

but right now that won't run. maybe it tries to avoid ambiguity.

you can at least use one of these:

a = "20070923"

if match = /^(\d{4})(\d{2})(\d{2}$)/.match(a)
  a.replace(match[1..3].join("/"))
  puts a
end

a = "20070923"
a[4,0] = a[6,0] = "/"
puts a

E:\>ruby date.rb
2007/09/23
2007/09/23

···

--
Posted via http://www.ruby-forum.com/\.

Allen Young wrote:

'20070912': there is no '.', '-' or '/' as the seperator and '%Y%m%d'
doesn't work, it will throw an exception saying that: "ArgumentError: 3
elements of civil date are necessary".

'2007.09': in this kind of string, I don't care about the exactly
date(in fact, the data I received is lacked of that information). As a
result, '%Y.%m' doesn't work with the same error as above.

How can I deal with this two kinds of string? Thanks a lot.

Try this:

require "parsedate"
include ParseDate

str = '20071029'
arr = parsedate(str)
p arr #[2007, 10, 29, nil, nil ...]

begin
    d = Date.new(arr[0], arg[1], arr[2])
    puts d.year #2007
    puts d.month #10
rescue ArgumentError
    puts "invalid date, eg. month = 15"
end

str = '2007.9'
arr = parsedate(str)
p arr #[nil, 20, 7, nil, nil ...]

pieces = str.split(".")
begin
    d = Date.new(Integer(pieces[0]), Integer(pieces[1]), 1)
    puts d.year #2007
    puts d.month #9
rescue ArgumentError
    puts "invalid date, e.g. day = 32"
end

···

--
Posted via http://www.ruby-forum.com/\.

Hi Allen,

It surprises me that this isn't easier. Here's what I'm doing to convert a similar input date/time format.

s = "20070926.155656"
d, t = s.scan(/\d+/) # => ["20070926", "155656"]
d1 = "#{d[0..3]}-#{d[4..5]}-#{d[6..7]}" # => "2007-09-26"
t1 = "#{t[0..1]}:#{t[2..3]}:#{t[4..5]}" # => "15:56:56"
dt = "-#{d1}T#{t1}Z" # => "-2007-09-26T15:56:56Z"
dt1 = DateTime.parse(dt) # => #<DateTime: 10673317777/10800,0,2299161>

···

At 4:08 PM +0900 9/23/07, Allen Young wrote:

Hi all,

I want to parse some strings into Date object and I find in rdoc that
strptime is the right place to go. For some formats of string it's
really easy to parse it. For instance, '%Y.%m.%d' will parse something
like '2007.09.12'. But I have two kinds of string that I don't now how
to parse.

'20070912': there is no '.', '-' or '/' as the seperator and '%Y%m%d'
doesn't work, it will throw an exception saying that: "ArgumentError: 3
elements of civil date are necessary".

'2007.09': in this kind of string, I don't care about the exactly
date(in fact, the data I received is lacked of that information). As a
result, '%Y.%m' doesn't work with the same error as above.

How can I deal with this two kinds of string? Thanks a lot.

7stud -- wrote:

rescue ArgumentError
    puts "invalid date, e.g. day = 32"
end

Actually, that ArgumentError could be caused by both the Integer() and
the Date.new() methods, so if you want to distinguish between the two
errors, you should convert to an Integer() in a different begin/rescue
block.

···

--
Posted via http://www.ruby-forum.com/\.

7stud -- wrote:

Try this:

Whoops. I forgot to post a solution for that leading "0" in the month:

require "date"

str = '2007.09'

pieces = str.split(".")
begin
    year = Integer(pieces[0])

    month = pieces[1]
    if month[0, 1] == '0'
        month = Integer(month[1,1])
    else
        month = Integer(month)
    end
rescue ArgumentError
    puts "invalid date string: couldn't convert to ints"
end

begin
    d = Date.new(year, month, 1)
    puts d.year
    puts d.month
rescue ArgumentError
    puts "invalid date args, e.g. day = 32"
end

···

--
Posted via http://www.ruby-forum.com/\.

SpringFlowers AutumnMoon wrote:> a = "20070923"

if match = /^(\d{4})(\d{2})(\d{2}$)/.match(a)
  a.replace(match[1..3].join("/"))
  puts a
end

oops, or it can be

a = "20070923"

=> "20070923"

a.sub!(/^(\d{4})(\d{2})(\d{2})$/, '\1/\2/\3')

=> "2007/09/23"

and then use Date.strptime

···

--
Posted via http://www.ruby-forum.com/\.

7stud -- wrote:

Whoops. I forgot to post a solution for that leading "0" in the month:

Here are some better ways:

month = "09"
begin
    month = month.to_i(10)
    puts month #9

    raise ArgumentError if month == 0

rescue ArgumentError
    puts "invalid date string"
end

···

#------------

require "scanf"

str = "2007.09"
year, month = str.scanf("%d.%d")
puts year #2007
puts month #9

str = "20080627"
y, m, d = str.scanf("%4d%2d%2d")
puts y, m, d #2008 6 27
--
Posted via http://www.ruby-forum.com/\.