Date Parsing

C

Clement Ow

I have a script which I need to create a new Time or Date object to
compare 2 dates for deletion. ie to delete files older than a stipulated
date
But i tend to get this error, undefined method 'parse' for
DateTime:class or undefined method 'strptime'

Can anyone tell me what is wrong? A snippet of my code is as follows:
folders.each do |folder|
Dir.glob(folder+"/*") do |file|
match = regexp.match(File.basename(file))
match1 = regexp1.match(File.basename(file))
matchExp = excep_yyyymmdd.match(File.basename(file))
matchExp1 = excep_ddmmyyyy.match(File.basename(file))
if match
file_date = DateTime.strptime(match[1])

if matchExp != nil or matchExp1 != nil
if $keepLastMthDay == true or $keepLastMth == true
puts "File Escaped: #{file} (Keep last day of month option >activated)"
elsif $keepLastMthDay == false and $keepLastMth == false
if delete_date > file_date
size = (File.size(file))/1024
deleted_files << fileData.new(file,size)
puts "Files/Folders deleted: #{file} size: #{size} KB"
#FileUtils.rm_r file
end #if
end #if
elsif matchExp == nil or matchExp1 == nil
if delete_date > file_date
size = (File.size(file))/1024
deleted_files << fileData.new(file,size)
puts "Files/Folders deleted: #{file} size: #{size} KB"
#FileUtils.rm_r file
end #if
end # if matchExp
 
7

7stud --

Clement said:
I have a script which I need to create a new Time or Date object to
compare 2 dates for deletion. ie to delete files older than a stipulated
date
But i tend to get this error, undefined method 'parse' for
DateTime:class or undefined method 'strptime'

Can anyone tell me what is wrong? A snippet of my code is as follows:
folders.each do |folder|
Dir.glob(folder+"/*") do |file|
match = regexp.match(File.basename(file))
match1 = regexp1.match(File.basename(file))
matchExp = excep_yyyymmdd.match(File.basename(file))
matchExp1 = excep_ddmmyyyy.match(File.basename(file))
if match
file_date = DateTime.strptime(match[1])

if matchExp != nil or matchExp1 != nil
if $keepLastMthDay == true or $keepLastMth == true
puts "File Escaped: #{file} (Keep last day of month option >activated)"
elsif $keepLastMthDay == false and $keepLastMth == false
if delete_date > file_date
size = (File.size(file))/1024
deleted_files << fileData.new(file,size)
puts "Files/Folders deleted: #{file} size: #{size} KB"
#FileUtils.rm_r file
end #if
end #if
elsif matchExp == nil or matchExp1 == nil
if delete_date > file_date
size = (File.size(file))/1024
deleted_files << fileData.new(file,size)
puts "Files/Folders deleted: #{file} size: #{size} KB"
#FileUtils.rm_r file
end #if
end # if matchExp

strptime() is essentially undocumented, so I can't help you there. But
here are some other ways to do what you want:


require 'parsedate.rb'
require 'date'

str = "5/6/08"
arr = ParseDate.parsedate(str, true) #true converts 08 to 2008
date1 = Date.new(*arr[0,3])

str2 = "2008-4-29"
arr2 = ParseDate.parsedate(str2)
date2 = Date.new(*arr2[0,3])

if date2 < date1
puts "I want to delete this file"
else
puts "I don't want to delete this file"
end


require 'scanf'

str3 = "2007.11.01"
year, day, month = str3.scanf("%4d.%2d.%2d")
date3 = Date.new(year, month, day)

if date3 < date1
puts "I want to delete this file"
else
puts "I don't want to delete this file"
end
 
C

Clement Ow

puts "I don't want to delete this file"
end

Hi, thanks for that, did'nt know you can do it this way ;)
However, I have 2 formats, one yyyymmdd and the other just ddmmyyyy(w/o
any dashes). Unfortunately Parsedate cannot be used for ddmmyyyy. Only
yyyymmdd can work. Any ideas?
 
C

Clement Ow

Clement said:
Hi, thanks for that, did'nt know you can do it this way ;)
However, I have 2 formats, one yyyymmdd and the other just ddmmyyyy(w/o
any dashes). Unfortunately Parsedate cannot be used for ddmmyyyy. Only
yyyymmdd can work. Any ideas?

or is there any way that we can convert something like ddmmyyyy to
yyyymmdd so that it'll work when we use Date or parsedate to parse dates
in order to not raise an invalid date error?
 
H

Heesob Park

Hi,

Clement said:
or is there any way that we can convert something like ddmmyyyy to
yyyymmdd so that it'll work when we use Date or parsedate to parse dates
in order to not raise an invalid date error?


[sidns@ns ~]$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.strptime('20102008',"%d%m%Y").to_s
=> "2008-10-20"
irb(main):003:0> Date.strptime('20081020',"%Y%m%d").to_s
=> "2008-10-20"


Regards,
Park Heesob
 
7

7stud --

Clement said:
or is there any way that we can convert something like ddmmyyyy to
yyyymmdd so that it'll work when we use Date or parsedate to parse dates
in order to not raise an invalid date error?

Any string can be parsed using one of the examples I posted.
 
7

7stud --

Heesob said:
Hi,

Clement said:
or is there any way that we can convert something like ddmmyyyy to
yyyymmdd so that it'll work when we use Date or parsedate to parse dates
in order to not raise an invalid date error?


[sidns@ns ~]$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.strptime('20102008',"%d%m%Y").to_s
=> "2008-10-20"
irb(main):003:0> Date.strptime('20081020',"%Y%m%d").to_s
=> "2008-10-20"


$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.strptime("20081020", "%Y%m%d").to_s
ArgumentError: invalid date
from /usr/lib/ruby/1.8/date.rb:650:in `new_with_hash'
from /usr/lib/ruby/1.8/date.rb:675:in `strptime'
from (irb):2
 
H

Heesob Park

7stud said:
Heesob said:
Hi,

[sidns@ns ~]$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.strptime('20102008',"%d%m%Y").to_s
=> "2008-10-20"
irb(main):003:0> Date.strptime('20081020',"%Y%m%d").to_s
=> "2008-10-20"


$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.strptime("20081020", "%Y%m%d").to_s
ArgumentError: invalid date
from /usr/lib/ruby/1.8/date.rb:650:in `new_with_hash'
from /usr/lib/ruby/1.8/date.rb:675:in `strptime'
from (irb):2

Ruby 1.8.4 fails but Ruby 1.8.6 works.

Regards,
Park Heesob
 
C

Clement Ow

7stud said:
Heesob said:
Hi,

Clement said:
Clement Ow wrote:

puts "I don't want to delete this file"
end

Hi, thanks for that, did'nt know you can do it this way ;)
However, I have 2 formats, one yyyymmdd and the other just ddmmyyyy(w/o
any dashes). Unfortunately Parsedate cannot be used for ddmmyyyy. Only
yyyymmdd can work. Any ideas?

or is there any way that we can convert something like ddmmyyyy to
yyyymmdd so that it'll work when we use Date or parsedate to parse dates
in order to not raise an invalid date error?


[sidns@ns ~]$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.strptime('20102008',"%d%m%Y").to_s
=> "2008-10-20"
irb(main):003:0> Date.strptime('20081020',"%Y%m%d").to_s
=> "2008-10-20"


$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.strptime("20081020", "%Y%m%d").to_s
ArgumentError: invalid date
from /usr/lib/ruby/1.8/date.rb:650:in `new_with_hash'
from /usr/lib/ruby/1.8/date.rb:675:in `strptime'
from (irb):2

hmmm. i even tried in irb whenever i use parse, it gives me an error:

Date.strptime('20081020',"%Y%m%d")
undefined method `strptime' for Date:Class (NoMethodError)

Date.parse('20081020',"%Y%m%d").to_s
undefined method `parse' for DateTime:Class (NoMethodError)
 
C

Clement Ow

Clement said:
Date.strptime('20081020',"%Y%m%d")
undefined method `strptime' for Date:Class (NoMethodError)

Date.parse('20081020',"%Y%m%d").to_s
undefined method `parse' for DateTime:Class (NoMethodError)

and im running ruby 1.8.5.. could it be why this has a prob? Like a bug
or something?
 
H

Heesob Park

Clement said:
and im running ruby 1.8.5.. could it be why this has a prob? Like a bug
or something?

I guess you missed
require 'date'

Regards,
Park Heesob
 
7

7stud --

Clement said:
and im running ruby 1.8.5.. could it be why this has a prob? Like a bug
or something?

It appears that Date.parse() and Date.strptime() are/were broken, and
therefore the defacto means for parsing Dates from Strings are the
Standard Library modules: ParseDate and Scanf. For instance, "The
Ruby Way" doesn't even mention Date.parse() or Date.strptime() when
discussing how to get Dates from Strings. And pickaxe2 doesn't cover
the Date/Time subject at all.
 
K

khaines

It appears that Date.parse() and Date.strptime() are/were broken, and
therefore the defacto means for parsing Dates from Strings are the
Standard Library modules: ParseDate and Scanf. For instance, "The
Ruby Way" doesn't even mention Date.parse() or Date.strptime() when
discussing how to get Dates from Strings. And pickaxe2 doesn't cover
the Date/Time subject at all.

require 'date'

Nothing is broken.


Kirk Haines
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top