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 | |
by leons (Pilgrim) on May 28, 2001 at 18:22 UTC | |
(boo)Re: Subroutine for returning a date, similar to sprintf
by boo_radley (Parson) on May 28, 2001 at 23:13 UTC | |
Re: Subroutine for returning a date, similar to sprintf
by John M. Dlugosz (Monsignor) on May 28, 2001 at 23:37 UTC | |
Re: Subroutine for returning a date, similar to sprintf
by juo (Curate) on May 29, 2001 at 12:34 UTC | |
by Anonymous Monk on May 31, 2001 at 19:42 UTC |