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"