How to parse a date? (strptime problem)

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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:
    >> 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?
    gabriele renzi Guest

  2. Similar Questions and Discussions

    1. #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...
    2. 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...
    3. 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...
    4. 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...
    5. #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...
  3. #2

    Default Re: How to parse a date? (strptime problem)

    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 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

  4. #3

    Default Re: How to parse a date? (strptime problem)

    >>>>> "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
    ts Guest

  5. #4

    Default 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:
    >>>> 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

    gabriele renzi Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139