#!/usr/bin/perl -w # Hi Monks, # # This subroutine might come in handy when having to work with dates in their # various formats. This sub was meant to work similar to sprintf() # # You can use the following variables: # # %s Seconds # %m Minutes # %h Hours # %D Day of Month # %M Month of Year # %y Year (2-digits) # %Y Year (4-digits) # # GetDate() also has a second (optional) parameter, which is the number of # days from now. # # Nr: Day: # -1 yesterday # 0 today (optional) # 1 tomorrow # 2 the day after tomorrow ... et cetera # # Hope you like it, Leon use strict; sub GetDate($;$) { my ($s,$m,$h,$D,$M,$y,$Y,$Inp)=((localtime(time-(-($_[1]||0)*86400)))[0..5,5],shift||""); $M++; $y%=100; $Y+=1900; $Inp=~s/%([\d.-]*)([smhDMYy])/sprintf("%$1d",eval "\$$2")/ge; $Inp; } # Examples # Output: print GetDate("%.2D-%.2M-%.4Y,%.2h:%.2m:%.2s\n"); # '21-05-2001,11:55:01' + "\n" print GetDate("%.2D-%.2M-%.4Y,%.2h:%.2m:%.2s\n",-22); # '29-04-2001,11:55:01' + "\n" print GetDate("%.2D-%.2M-%.4Y,%.2h:%.2m:%.2s\n",180); # '17-11-2001,11:55:01' + "\n" print GetDate("time: %.2h:%.2m date: %.2D/%.2M/%.4Y\n"); # 'time: 11:55 date: 21/05/2001' + "\n" print GetDate("%.2D%.2M%.2y"); # '210501' print GetDate("%.2s seconds\n"); # '01 seconds' + "\n" print GetDate("%s seconds\n"); # '1 seconds' + "\n"