in reply to Re^2: How to display 10 -1 =09 In Perl
in thread How to display 10 -1 =09 In Perl
Last month with Time::Piece with month granularity
#!/usr/bin/perl -- use strict; use warnings; use Time::Piece qw/ localtime /; Main( @ARGV ); exit( 0 ); sub Main { my $now = localtime; my $lastmonth = $now->prevmonth; print "$now\n$lastmonth\n"; } sub Time::Piece::prevmonth { my $t= shift; if( ! ref $t ) { $t = localtime; } my $y = $t->year; my $m = $t->mon; #~ if( $m == 12 ){ $m=1; $y++; } else { $m++; } if( $m == 1 ){ $m=12; $y--; } else { $m--; } my $nt = Time::Piece->strptime( "$y-$m", '%Y-%m' ); } __END__ Thu Oct 30 03:10:05 2014 Mon Sep 1 00:00:00 2014
|
|---|