Ask a Question related to Ruby, Design and Development.
-
gabriele renzi #1
How to parse a date? (strptime problem)
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:=> true>> require 'date'=> "%m/%b/%Y:%H:%M:%S">> fmt= "%m/%b/%Y:%H:%M:%S"=> "10/Oct/2003:10:16:29">> z=DateTime.now.strftime(fmt)
And I supposed I could use DateTime.strptime() to parse it
(strptime() seem to get a format argument).
But if I try:ArgumentError: invalid date>> DateTime.strptime z,fmt
How should I parse a line using a self-defined format?
gabriele renzi Guest
-
#38938 [NEW]: more useful initialization for strptime
From: soletan at toxa dot de Operating system: Linux PHP version: 5CVS-2006-09-23 (snap) PHP Bug Type: Date/time related Bug... -
How to parse a string into a date?
I have a date string that comes into my Flex app as something like '2005-10-24 00:00:00'. I have a DateFormatter set up as <mx:DateFormatter... -
Problem to get Parse::Yapp and Parse:Flex working together
A) I did: # cd ~/.cpan/build/Parse-Flex-0.03 # more src/default.y %{ #define YY_DECL char* yyylex YY_PROTO(( void )) #undef yywrap int... -
Using perl to parse specific lines by date...
While I've already done this with a simple shell script using grep, I was trying to figure out how I can do the same thing in perl. I have an... -
#14473 [Opn->Csd]: strtotime wont parse date
ID: 14473 Updated by: iliaa@php.net Reported By: lindsay dot marshall at ncl dot ac dot uk -Status: Open... -
Ollivier Robert #2
Re: How to parse a date? (strptime problem)
In article <rd3fpvc8egjrdt5jd7qh9ncmt5abpakfpi@4ax.com>,
gabriele renzi <surrender_it@remove.yahoo.it> wrote:Here is what I do:> How should I parse a line using a self-defined format?
-=-=-
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
rocess_entry
-=-=-
Dates are like this:
Canceled on: 21 Mar 2002 21:27:01 GMT
--
Ollivier ROBERT -=- Eurocontrol EEC/ITM -=- [email]roberto@eurocontrol.fr[/email]
Usenet Canal Historique FreeBSD: The Power to Serve!
Ollivier Robert Guest
-
ts #3
Re: How to parse a date? (strptime problem)
>>>>> "g" == gabriele renzi <surrender_it@remove.yahoo.it> writes:
g> => true>>> require 'date'g> => "%m/%b/%Y:%H:%M:%S">>> fmt= "%m/%b/%Y:%H:%M:%S"g> => "10/Oct/2003:10:16:29">>> z=DateTime.now.strftime(fmt)
^^
^^
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:g> ArgumentError: invalid date>>> DateTime.strptime z,fmt
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
ts Guest
-
gabriele renzi #4
Re: How to parse a date? (strptime problem)
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:>g> ArgumentError: invalid date>>>> DateTime.strptime z,fmt
>
> Your format is wrong, you must use %d (day) rather than %m (month)
oops... thanks for the answers
gabriele renzi Guest



Reply With Quote

