Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked


by Anonymous Monk
on Aug 27, 2002 at 18:57 UTC ( #193240=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi, i have a program that generates the time in i use gmtime($output) to get the time in GM gmtime(1025546133)would be Mon Jul 1 05:01:54 can i get this to be in the following format in an efficient way: 07/01/2002-05:01:54....thanks

Replies are listed 'Best First'.
Re: gmtime
by Zaxo (Archbishop) on Aug 27, 2002 at 19:11 UTC

    &POSIX::strftime .

    After Compline,

Almost verbatim from CPAN
by charnos (Friar) on Aug 27, 2002 at 19:17 UTC
    use Date::Calc; ($year,$month,$day, $hour,$min,$sec) = Today_and_Now([$gmt]); $final = "$month/$day/$year-$hour:$min:$sec";
Re: gmtime
by grinder (Bishop) on Aug 27, 2002 at 19:58 UTC
    If you want to do it in a module-free manner without using pesky intermediate variables, you could do worse than wrapping up sprintf in an anonymous subroutine, which gives something like:

    my $date = sprintf '%04d/%02d/%02d-%02d:%02d:%02d', sub { $_[5]+1900, $_[4]+1, reverse @_[0..3] }->(gmtime($output));

    I write this snippet so often I have often toyed with the idea of uploading as a module to CPAN so I can use it where ever I work, but it seems a bit frivolous since I can type it so quickly. What is happening is that the output from gmtime is being passed to an anonymous sub, which modifies two elements, reverses the first few elements taken as a hash slice and throws the rest away. The resulting elements are returned in list context from the sub to sprintf, which, by a miraculous turn of events, is waiting with a format string which will process those values in just the right way.

    I really dig this idiom.

    oops: I just realised I coded this up to emit the standard ISO date format. Talk about habit. I would avoid using the M/D/Y format: it looks to me as if you're talking about the 7th of January. Which indeed you might be. But others will think you're talking about the 1st of July. I mean, if you're going to the effort of representing times in GMT...

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: gmtime
by krujos (Curate) on Aug 27, 2002 at 19:24 UTC
    You might want to check out Date::Parse it should give you some easy variables to use and convert to the format you want. Date::Format is another module worth checking out for this.
    Good Luck
Re: gmtime
by Anonymous Monk on Aug 28, 2002 at 03:09 UTC
    While this module can introduce a lot of unnecessary overhead, you might want to look at Date::Manip. It can do just about anything you would want with regard to date comparison, manipulation, conversion, etc. Consider it an all-in-one suite of about a dozen other date modules. It's very easy to use, but like I said, high overhead. Specifically, look at the UnixDate() function in this module. Hope this helps.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://193240]
Approved by xtype
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-19 21:10 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (115 votes). Check out past polls.