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