Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
more useful options
 
PerlMonks  

Re: date

by little (Curate)
on Feb 13, 2002 at 06:07 UTC ( [id://145131]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Year shows as 20102 in date

Take a look at Date::Format which is comliant with the timefmt string that apache takes in AND also has some kind of language packs so you get the short and long names for days and monts in many languages, so yours might be there as well. If you don't need that "language feature" think about the possibility of others reusing your code and the effort then to adapt it to other languages.

Have a nice day
All decision is left to your taste

Replies are listed 'Best First'.
Re: Re: date
by beebware (Pilgrim) on Feb 13, 2002 at 13:47 UTC
    If you don't want to use Date::Format and don't care that your code might be 'internationalised', here's a snippet of code that I continually reuse:
    sub library_todayis { my ($time)=@_; if ($time<1) { $time=time(); } my @months=qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!; my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $tz) = loca +ltime($time); if ($day<10) { $day="0".$day; } if ($hour<10) { $hour="0".$hour; } if ($min<10) { $min="0".$min; } if ($sec<10) { $sec="0".$sec; } $year = $year + 1900; return ("$day $months[$mon] $year $hour:$min"); }
    Pass it the timestamp you want converting (or leave blank for the current time), and in return you'll get something like 13 Feb 2002 18:48 in your current timezone.

    Update: Thanks to little, for a 'duh! why are you doing it like that?' here's a shortened version doing it the slightly better way using sprintf:

    sub library_todayis { my $time=shift || time(); my @months=qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!; my ($sec,$min,$hour,$day,$mon,$year)=localtime($time); $year+=1900;$mon++; return sprintf("%02d $months[$mon] $year %02d:%02d",$day,$hour,$min); }
    Which, I have to confess is a lot better looking and probably a bit faster (although I haven't benchmarked it)
      But you've heard about printf or sprintf or POSIX?
      Those would shorten this your code by 50% up to 80%. *grin*
      And you don't need to install them cause they are where Perl is.

      Have a nice day
      All decision is left to your taste

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://145131]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.