converting epoch seconds to usable date - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

converting epoch seconds to usable date

mdw1982mdw1982 Posts: 124Member
Hi all,

I'm stumped...

its very likely I'm missing something, but what that something is alludes me. I'm trying to convert epoch seconds to a usable date format, but I'm having no luck at all. I don't want to use localtime() because it gives a huge string that I really can't use. What I'm after is a date formated like this: yyyy-mm-dd

Below is the code I'm working with and the resulting output.

[code]
1 #!/usr/bin/perl
2 #
3
4 use strict;
5 use Time::CTime;
6
7 my $epochSeconds = time;
8
9 my $theDate= strftime( '%Y-%M-%d', $epochSeconds );
10
11 print "The Epoch Seconds are: $epochSeconds
";
12 print "The Converted Date is: $theDate
";
[/code]

The output:

The Epoch Seconds are: 1069474375
The Converted Date is: 1900-00-00

As far as I know the epoch seconds shown above is current to when the program ran and is the actual date and time that the program was run.

Any ideas?

Comments

  • JonathanJonathan Posts: 2,914Member
    Hi,

    : its very likely I'm missing something, but what that something is
    : alludes me. I'm trying to convert epoch seconds to a usable date
    : format, but I'm having no luck at all. I don't want to use localtime
    : () because it gives a huge string that I really can't use. What I'm
    : after is a date formated like this: yyyy-mm-dd
    Note that you can use localtime in array context to get all the bits of the date:-

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

    $year is an offset since 1900, so you need to do:

    $year += 1900;

    : Below is the code I'm working with and the resulting output.
    :
    : [code]
    : 1 #!/usr/bin/perl
    : 2 #
    : 3
    : 4 use strict;
    : 5 use Time::CTime;
    : 6
    : 7 my $epochSeconds = time;
    : 8
    : 9 my $theDate= strftime( '%Y-%M-%d', $epochSeconds );
    : 10
    : 11 print "The Epoch Seconds are: $epochSeconds
    ";
    : 12 print "The Converted Date is: $theDate
    ";
    : [/code]
    :
    : The output:
    :
    : The Epoch Seconds are: 1069474375
    : The Converted Date is: 1900-00-00
    I think you're using it wrongly; see:-
    http://search.cpan.org/~muir/Time-modules-2003.0211/lib/Time/CTime.pm

    From glancing at the docs, I think what you want to be putting is:-

    my $theDate= strftime( '%Y-%m-%d', localtime($epochSeconds));

    Note %M = minutes, %m is month.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • mdw1982mdw1982 Posts: 124Member
    : Hi,
    :
    : : its very likely I'm missing something, but what that something is
    : : alludes me. I'm trying to convert epoch seconds to a usable date
    : : format, but I'm having no luck at all. I don't want to use localtime
    : : () because it gives a huge string that I really can't use. What I'm
    : : after is a date formated like this: yyyy-mm-dd
    : Note that you can use localtime in array context to get all the bits of the date:-
    :
    : ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    :
    : $year is an offset since 1900, so you need to do:
    :
    : $year += 1900;
    :
    : : Below is the code I'm working with and the resulting output.
    : :
    : : [code]
    : : 1 #!/usr/bin/perl
    : : 2 #
    : : 3
    : : 4 use strict;
    : : 5 use Time::CTime;
    : : 6
    : : 7 my $epochSeconds = time;
    : : 8
    : : 9 my $theDate= strftime( '%Y-%M-%d', $epochSeconds );
    : : 10
    : : 11 print "The Epoch Seconds are: $epochSeconds
    ";
    : : 12 print "The Converted Date is: $theDate
    ";
    : : [/code]
    : :
    : : The output:
    : :
    : : The Epoch Seconds are: 1069474375
    : : The Converted Date is: 1900-00-00
    : I think you're using it wrongly; see:-
    : http://search.cpan.org/~muir/Time-modules-2003.0211/lib/Time/CTime.pm
    :
    : From glancing at the docs, I think what you want to be putting is:-
    :
    : my $theDate= strftime( '%Y-%m-%d', localtime($epochSeconds));
    :
    : Note %M = minutes, %m is month.
    :
    : Jonathan
    :

    Thanks Jonathan... that did the trick. I've really got to stop trying to code so late at night. :)

    Mark
  • JonathanJonathan Posts: 2,914Member
    : Thanks Jonathan... that did the trick. I've really got to stop
    : trying to code so late at night. :)
    Yeah, me too...look at the code about halfway through this one...
    http://www.jwcs.net/~jonathan/perl/Calculus.pm.txt



    Jonathan



    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • mdw1982mdw1982 Posts: 124Member
    : : Thanks Jonathan... that did the trick. I've really got to stop
    : : trying to code so late at night. :)
    : Yeah, me too...look at the code about halfway through this one...
    : http://www.jwcs.net/~jonathan/perl/Calculus.pm.txt
    :
    :
    :
    : Jonathan

    my god! that made my brain hurt just a lookin at it. Quite impressive bit of coding though. Nice job.

    Mark
Sign In or Register to comment.