To change the time stamp format...

Discussion in 'Perl Misc' started by clearguy02@yahoo.com, Apr 2, 2005.

  1. Guest

    Hi experts,

    I need a function in my perl program that converts the given time stamp
    to a specific time stamp format.

    example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00

    Can some one tell me how to do this?

    Thanks,
    John
     
    , Apr 2, 2005
    #1
    1. Advertising

  2. On 02/04/2005, wrote:

    > example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
    >
    > Can some one tell me how to do this?


    use split with /[-:.]/ to get the individual date parts
    use a hash to translate month abbreviations to month numbers
    use sprintf to format your target date

    --
    felix
     
    Felix Geerinckx, Apr 2, 2005
    #2
    1. Advertising

  3. wrote:
    > I need a function in my perl program that converts the given time stamp
    > to a specific time stamp format.
    >
    > example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
    >
    > Can some one tell me how to do this?


    There are two approaches:

    1) You can search for an applicable module at http://search.cpan.org/

    2) You can write the function yourself. Useful built-in Perl functions
    may be:

    perldoc -f split
    perldoc -f sprintf

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 2, 2005
    #3
  4. Guest

    Thanks Felix,

    Do you have any example code to do this?

    Thanks,
    John
     
    , Apr 2, 2005
    #4
  5. [ Please give some context when you reply to a message. See the posting
    guidelines for this group at
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html ]

    wrote:
    > Felix Geerinckx wrote:
    >>>
    >>> example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
    >>>
    >>> Can some one tell me how to do this?

    >>
    >> use split with /[-:.]/ to get the individual date parts
    >> use a hash to translate month abbreviations to month numbers
    >> use sprintf to format your target date

    >
    > Thanks Felix,
    >
    > Do you have any example code to do this?


    Felix probably has code in his collection of Perl code snippets that
    does exactly the convertion you need, and it was most likely through an
    oversight he didn't post it along with the above hints.

    If I'm wrong in my assumption, you may need to look up those parts of
    the Perl documentation and give it a try yourself. Scaring thought, I
    know, but what can you do...

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 2, 2005
    #5
  6. wrote:
    >
    > I need a function in my perl program that converts the given time stamp
    > to a specific time stamp format.
    >
    > example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
    >
    > Can some one tell me how to do this?



    my $date = '15-Aug-04.19:03';

    my %months = qw(
    Jan 1
    Feb 2
    Mar 3
    Apr 4
    May 5
    Jun 6
    Jul 7
    Aug 8
    Sep 9
    Oct 10
    Nov 11
    Dec 12
    );

    my $mon_lookup = join '|', keys %months;

    if ( $date =~ /(\d+)-($mon_lookup)-(\d+)\.(\d+):(\d+)/ ) {

    printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1, $4, $5;

    }




    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Apr 2, 2005
    #6
  7. "John W. Krahn" <> wrote in
    news:C2F3e.127630$ZO2.8180@edtnps84:

    > wrote:
    >>
    >> I need a function in my perl program that converts the given time
    >> stamp to a specific time stamp format.
    >>
    >> example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
    >>
    >> Can some one tell me how to do this?

    >
    >


    > my $mon_lookup = join '|', keys %months;
    >
    > if ( $date =~ /(\d+)-($mon_lookup)-(\d+)\.(\d+):(\d+)/ ) {
    >
    > printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1,
    > $4, $5;


    Ooops! How about that Y2.1K bug?!

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 2, 2005
    #7
  8. <> wrote:

    > Thanks Felix,
    >
    > Do you have any example code to do this?



    Post the code that you have tried so far, and we will help you fix it.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 2, 2005
    #8
  9. A. Sinan Unur wrote:
    > "John W. Krahn" <> wrote in
    > news:C2F3e.127630$ZO2.8180@edtnps84:
    >>
    >>if ( $date =~ /(\d+)-($mon_lookup)-(\d+)\.(\d+):(\d+)/ ) {
    >>
    >> printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1,
    >> $4, $5;

    >
    > Ooops! How about that Y2.1K bug?!


    Is that thing still around? :)


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Apr 3, 2005
    #9
  10. "John W. Krahn" <> wrote in news:EEG3e.127836$ZO2.94802
    @edtnps84:

    > A. Sinan Unur wrote:
    >> "John W. Krahn" <> wrote in
    >> news:C2F3e.127630$ZO2.8180@edtnps84:
    >>>
    >>>if ( $date =~ /(\d+)-($mon_lookup)-(\d+)\.(\d+):(\d+)/ ) {
    >>>
    >>> printf "20%02d-%02d-%02d %02d:%02d:00\n", $3, $months{$2}, $1,
    >>> $4, $5;

    >>
    >> Ooops! How about that Y2.1K bug?!

    >
    > Is that thing still around? :)


    Yeah, it seems like a new version is released every century :)

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 3, 2005
    #10
  11. A. Sinan Unur wrote:

    > "John W. Krahn" <> wrote in news:EEG3e.127836$ZO2.94802
    > @edtnps84:
    >
    >>A. Sinan Unur wrote:
    >>
    >>>Ooops! How about that Y2.1K bug?!

    >>
    >>Is that thing still around? :)

    >
    > Yeah, it seems like a new version is released every century :)


    Indeed, I always wondered why people insisted on calling the centuary
    bug 'the millenium bug' or the 'Y2K bug'.

    But it's really more often than that. There are all sort of ways that
    various software encodes dates internally. Each of these can introduce
    its own problems.

    There was one on 9th September 2001. (10**9 seconds since 1970).

    Of course there's the famous 'Unix' one on 19th January 2038 (2**31
    seconds since 1970).

    There's a lesser known one one a couple of years earlier (2**32 seconds
    since 1900).

    There's even one in 2010 that some users of systems written in the MUMPS
    programing language may hit.
     
    Brian McCauley, Apr 3, 2005
    #11
  12. <> wrote in message
    news:...
    > Hi experts,
    >
    > I need a function in my perl program that converts the given time stamp
    > to a specific time stamp format.
    >
    > example: 15-Aug-04.19:03 should be converted to 2004-08-15 19:03:00
    >
    > Can some one tell me how to do this?
    >
    > Thanks,
    > John
    >


    Try Date::Manip
    Simple, and powerful.
     
    Doron Nissimi, Apr 3, 2005
    #12
  13. Brian McCauley wrote:
    >
    >
    > A. Sinan Unur wrote:
    >
    >> "John W. Krahn" <> wrote in
    >> news:EEG3e.127836$ZO2.94802
    >> @edtnps84:
    >>
    >>> A. Sinan Unur wrote:
    >>>
    >>>> Ooops! How about that Y2.1K bug?!
    >>>
    >>>
    >>> Is that thing still around? :)

    >>
    >>
    >> Yeah, it seems like a new version is released every century :)

    >
    >
    > Indeed, I always wondered why people insisted on calling the centuary
    > bug 'the millenium bug' or the 'Y2K bug'.
    >
    > But it's really more often than that. There are all sort of ways that
    > various software encodes dates internally. Each of these can introduce
    > its own problems.
    >
    > There was one on 9th September 2001. (10**9 seconds since 1970).
    >
    > Of course there's the famous 'Unix' one on 19th January 2038 (2**31
    > seconds since 1970).
    >
    > There's a lesser known one one a couple of years earlier (2**32 seconds
    > since 1900).
    >
    > There's even one in 2010 that some users of systems written in the MUMPS
    > programing language may hit.


    IBM mainframes started to have "keep forever" files deleted on August
    16th, 1972, which was 9999 days before 2000-01-01.

    (There was also a beta version of OS/VS1 that crashed every 24 hours at
    exactly midnight, GMT, with a zero-divide error in the nucleus, because
    one of the programmers who wrote the date/time code was aware that 1900
    was not a leap year, and another one wasn't.)

    --
    John W. Kennedy
    "The pathetic hope that the White House will turn a Caligula into a
    Marcus Aurelius is as naïve as the fear that ultimate power inevitably
    corrupts."
    -- James D. Barber (1930-2004)
     
    John W. Kennedy, Apr 3, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ladygrinningsoul

    Modifying a file's time stamp

    ladygrinningsoul, Dec 5, 2004, in forum: Perl
    Replies:
    1
    Views:
    565
    ladygrinningsoul
    Dec 6, 2004
  2. =?Utf-8?B?QmVydA==?=

    Date/Time stamp in SQL

    =?Utf-8?B?QmVydA==?=, Jan 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    21,508
    Wardeaux
    Jan 30, 2004
  3. Zean Smith

    Time stamp problem

    Zean Smith, Feb 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    350
    Zean Smith
    Feb 7, 2006
  4. srini
    Replies:
    9
    Views:
    11,743
    Beauregard T. Shagnasty
    Apr 14, 2004
  5. BartlebyScrivener

    shutil.copy time stamp errors on win XP

    BartlebyScrivener, Apr 15, 2006, in forum: Python
    Replies:
    2
    Views:
    402
    BartlebyScrivener
    Apr 16, 2006
Loading...

Share This Page