Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Subroutine for returning a date, similar to sprintf

by leons (Pilgrim)
on May 28, 2001 at 16:22 UTC ( [id://83720]=perlcraft: print w/replies, xml ) Need Help??

   1: #!/usr/bin/perl -w
   2: 
   3: # Hi Monks,
   4: #
   5: # This subroutine might come in handy when having to work with dates in their
   6: # various formats. This sub was meant to work similar to sprintf()
   7: #
   8: # You can use the following variables:
   9: #
  10: # %s    Seconds
  11: # %m    Minutes
  12: # %h    Hours
  13: # %D    Day of Month
  14: # %M    Month of Year
  15: # %y    Year (2-digits)
  16: # %Y    Year (4-digits)
  17: #
  18: # GetDate() also has a second (optional) parameter, which is the number of
  19: # days from now.
  20: #
  21: # Nr:     Day:
  22: # -1      yesterday
  23: # 0       today (optional)
  24: # 1       tomorrow
  25: # 2       the day after tomorrow ... et cetera
  26: #
  27: # Hope you like it, Leon
  28: 
  29: use strict;
  30: 
  31: sub GetDate($;$)
  32: {
  33:  my ($s,$m,$h,$D,$M,$y,$Y,$Inp)=((localtime(time-(-($_[1]||0)*86400)))[0..5,5],shift||"");
  34:  $M++; $y%=100; $Y+=1900;
  35:  $Inp=~s/%([\d.-]*)([smhDMYy])/sprintf("%$1d",eval "\$$2")/ge;
  36:  $Inp;
  37: }
  38: 
  39: # Examples
  40: 
  41: #                                                               Output:
  42: print GetDate("%.2D-%.2M-%.4Y,%.2h:%.2m:%.2s\n");               # '21-05-2001,11:55:01' + "\n"
  43: print GetDate("%.2D-%.2M-%.4Y,%.2h:%.2m:%.2s\n",-22);           # '29-04-2001,11:55:01' + "\n"
  44: print GetDate("%.2D-%.2M-%.4Y,%.2h:%.2m:%.2s\n",180);           # '17-11-2001,11:55:01' + "\n"
  45: print GetDate("time: %.2h:%.2m date: %.2D/%.2M/%.4Y\n");        # 'time: 11:55 date: 21/05/2001' + "\n"
  46: print GetDate("%.2D%.2M%.2y");                                  # '210501'
  47: print GetDate("%.2s seconds\n");                                # '01 seconds' + "\n"
  48: print GetDate("%s seconds\n");                                  # '1 seconds' + "\n"

Replies are listed 'Best First'.
Re: Subroutine for returning a date, similar to sprintf
by Kanji (Parson) on May 28, 2001 at 17:51 UTC

    You can get much of the same functionality by using POSIX.pm's strftime function.

    use POSIX qw/strftime/; sub days { return $_[0] * 86400 } print strftime( "%d-%m-%Y,%H:%M:%S\n", localtime ); print strftime( "%d-%m-%Y,%H:%M:%S\n", localtime(time - days( 22))); print strftime( "%d-%m-%Y,%H:%M:%S\n", localtime(time + days(180))); print strftime( "time: %H:%M date: %d/%m/%Y\n", localtime ); print strftime( "%d%m%y\n", localtime ); print strftime( "%S seconds\n", localtime ); print int( strftime( "%S", localtime ) ), " seconds\n";

        --k.


      Ahem,

      Thanks ... this would have saved me quite some time ...
      ... Oh well ... I finally got to use 'eval' ... now that's
      worth something ;-)

      Bye, Leon
(boo)Re: Subroutine for returning a date, similar to sprintf
by boo_radley (Parson) on May 28, 2001 at 23:13 UTC
    Whenever I've started to make "replacement for &foo" things, I've found it helpful to ask myself "why would people choose to use my replacement than the original &foo?"
    99 out of 100, it's been "I guess they wouldn't.", and the hundredth time was because of some data validation I was performing on $bar before it was transformed by &foo, so I doubt it could even be called a replacement, but probably more of a wrapper. Asking this question of myself has saved me from writing some (potentially) bad code.

    In fact, I see that in the heart of your code lies this :

    $Inp=~s/%([\d.-]*)([smhDMYy])/sprintf("%$1d",eval "\$$2")/ge;
    So this really isn't a replacement for sprintf, but an enhancement thereof.
    Also, in addition to Kanji's suggestion of POSIX, there's also the (to borrow a phrase) the all-singing, all-dancing Date::Manip.
Re: Subroutine for returning a date, similar to sprintf
by John M. Dlugosz (Monsignor) on May 28, 2001 at 23:37 UTC
    Personally, I'd use the same letters as in strftime() and in the C++ date formatting thing. But, I like your idea for the delta argument—that can save a step in many common cases.

    —John

Re: Subroutine for returning a date, similar to sprintf
by juo (Curate) on May 29, 2001 at 12:34 UTC

    Very nice but something that would be nice to have their is the date with month as name because US is different then Europe. For example : 29-05-2001 - Europe 05-29-2001 - US 29-May-2001 - Neutral Pieter

      With an appropriate locale setting (via LC_ALL or some such) you might be able to alter time formatting with:
      use locale;
      Does that work for you?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-19 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found