If the current date is in the first half of January, then presumably you want the previous year.
For these kind of problems I find it useful to write a test routine that checks across the boundary conditions like this
use warnings;
use strict;
# test routine
for my $y (2001..2002){
for my $m (1,6,12){
for my $d (1,15,16,31){
print "$d\t$m\t$y = ".newrelease($d,$m-1,$y-1900)."\n";
}
}
print "\n";
}
sub newrelease {
#my ($day,$month,$year) = (localtime)[3,4,5];
my ($day,$month,$year) = @_; # testing
# make normal
$year+=1900;
$month+=1;
# return values
my ($start_date,$end_date);
# logic
if ($day > 15){
# easy one same month
$month = sprintf "%02d",$month;
$start_date = $year.$month."01";
$end_date = $year.$month."15";
} else {
# day is 15 or less use previous month
# if now jan then prev month is dec last year
if ($month == 1){
--$year;
$month=12;
} else {
$month = sprintf "%02d",$month-1;
}
$start_date = $year.$month."16";
$end_date = $year.$month."31";
}
return "$start_date to $end_date";
}
poj
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.