[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to parse a date? (strptime problem

gabriele renzi

10/23/2003 8:21:00 AM

Hi gurus and nubys,
I was wondering how to parse a line like this:
01/Oct/1980:01:56:57

I know I can use strftime() to write it:
>> require 'date'
=> true
>> fmt= "%m/%b/%Y:%H:%M:%S"
=> "%m/%b/%Y:%H:%M:%S"
>> z=DateTime.now.strftime(fmt)
=> "10/Oct/2003:10:16:29"

And I supposed I could use DateTime.strptime() to parse it
(strptime() seem to get a format argument).
But if I try:
>> DateTime.strptime z,fmt
ArgumentError: invalid date

How should I parse a line using a self-defined format?
4 Answers

Ollivier Robert

10/23/2003 12:06:00 PM

0

In article <rd3fpvc8egjrdt5jd7qh9ncmt5abpakfpi@4ax.com>,
gabriele renzi <surrender_it@remove.yahoo.it> wrote:
> How should I parse a line using a self-defined format?

Here is what I do:

-=-=-
def process_entry (entry)
# Get the ASCII date
#
c_date = entry[-1].sub(/^Canceled on: (.*)$/, '\1')

# Get a hash from the string
#
h_date = Date::strptime(c_date, "%d %b %Y %T %Z")

# Get a Time object
#
n_date = Time.gm(h_date[:year], h_date[:mon], h_date[:mday],
h_date[:hour], h_date[:min], h_date[:sec])

# Check whether we keep the record or not (i.e. older than $num_days)
#
if n_date < ($now - $num_days * SPERDAY)
return true
else
return false
end
end

private :process_entry
-=-=-

Dates are like this:

Canceled on: 21 Mar 2002 21:27:01 GMT

--
Ollivier ROBERT -=- Eurocontrol EEC/ITM -=- roberto@eurocontrol.fr
Usenet Canal Historique FreeBSD: The Power to Serve!

ts

10/23/2003 12:39:00 PM

0

>>>>> "g" == gabriele renzi <surrender_it@remove.yahoo.it> writes:

>>> require 'date'
g> => true
>>> fmt= "%m/%b/%Y:%H:%M:%S"
g> => "%m/%b/%Y:%H:%M:%S"
>>> z=DateTime.now.strftime(fmt)
g> => "10/Oct/2003:10:16:29"
^^
^^

Well, try these 2 commands

DateTime.now.to_s
DateTime.now.strftime("%m/%b/%Y:%H:%M:%S")

You don't think that it exist a problem ?

g> And I supposed I could use DateTime.strptime() to parse it
g> (strptime() seem to get a format argument).
g> But if I try:
>>> DateTime.strptime z,fmt
g> ArgumentError: invalid date

Your format is wrong, you must use %d (day) rather than %m (month)

svg% irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> fmt= "%d/%b/%Y:%H:%M:%S"
=> "%d/%b/%Y:%H:%M:%S"
irb(main):003:0> z = DateTime.now.strftime(fmt)
=> "23/Oct/2003:14:36:15"
irb(main):004:0> DateTime.strptime z,fmt
=> #<DateTime: 2825782397/1152,0,2299161>
irb(main):005:0>
svg%


--

Guy Decoux

gabriele renzi

10/23/2003 3:03:00 PM

0

il 23 Oct 2003 14:39:11 +0200, ts <decoux@moulon.inra.fr> ha scritto::

>>>>>> "g" == gabriele renzi <surrender_it@remove.yahoo.it> writes:
>

>g> And I supposed I could use DateTime.strptime() to parse it
>g> (strptime() seem to get a format argument).
>g> But if I try:
>>>> DateTime.strptime z,fmt
>g> ArgumentError: invalid date
>
> Your format is wrong, you must use %d (day) rather than %m (month)


oops... thanks for the answers

Jeffrey Hamilton

7/24/2009 4:42:00 PM

0

Allan wrote:
> "Jeffrey Hamilton" <bberesford@cogeco.ca> wrote in message
> news:klI9m.42816$Ta5.18519@newsfe15.iad...
>> Allan wrote:
>>> I've got an awful feeling that I could regret posting this on here
>>> but never mind, you said a few weeks back that you'd quite like to
>>> hear a song so I've obliged. Used my son's cheap camera so sorry
>>> about the quality of the recording. I can sort that out
>>> eventually, hopefully along with the funny facial expressions, but
>>> the singing I'm stick with :-)
>>>
>>> http://www.youtube.com/watch?v=r...
>>>
>>> Allan
>>
>> That was quite nice Allan, is that song your own composition ?
>
> Yeagh that is one of my quieter ones which can only really be played
> in our pub sessions late on or if we have a mic set up. There is a town
> called Hawick which calls itself Queen o' a' the Borders and a
> village called Swinton which calls itself Queen o' the Merse so I
> amalgamated the two into Queen o' a' the Merse and made it about a
> woman rather than a place.

That makes sense, if it ever becomes a hit recording, you won't be plagued
with lawsuits demanding royalties. <G>

cheers.....Jeff


> cheers
>
> Allan