Perl

Moderators: Jonathan
Number of threads: 1259
Number of posts: 3644

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
converting epoch seconds to usable date Posted by mdw1982 on 21 Nov 2003 at 8:19 PM
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.

      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\n";
     12 print "The Converted Date is\: $theDate\n";


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?
Report
Re: converting epoch seconds to usable date Posted by Jonathan on 22 Nov 2003 at 3:33 AM
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.
:
:
:       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\n";
:      12 print "The Converted Date is\: $theDate\n";
: 

:
: 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.");

Report
Re: converting epoch seconds to usable date Posted by mdw1982 on 22 Nov 2003 at 6:39 AM
: 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.
: :
: :
: :       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\n";
: :      12 print "The Converted Date is\: $theDate\n";
: : 

: :
: : 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
Report
Re: converting epoch seconds to usable date Posted by Jonathan on 22 Nov 2003 at 7:07 AM
: 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

<grins>

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.");

Report
Re: converting epoch seconds to usable date Posted by mdw1982 on 29 Nov 2003 at 12:13 PM
: : 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
:
: <grins>
:
: Jonathan

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

Mark



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.