Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Simple calendar

by perl_fool (Initiate)
on Mar 08, 2010 at 01:41 UTC ( [id://827299]=perlquestion: print w/replies, xml ) Need Help??

perl_fool has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I would like to create a calendar using perl programming. The program should accept the year input by user and also the the day of the week for January 1 of that particular year (1=sunday; 7=saturday). example if i key in 1 for the day input, Jan 1 should be in sunday; if i key in 2, Jan 1 should be monday.

I've looked through the calendar examples in this website but it's too hard for me, especially the use of local time function..I need some help here, below it's the guideline given by my lecturer, which i don't think it's gonna work..

<code> foreach (1..12) { #12 months
if (day==1) { #if it's sunday
print "\t\t\t\t\t\t\t1\n";
$date=2; #next date will start with 2
}
}

foreach (1..5) { # 5 weeks in a month
foreach (1..7) { #7 days in a week
print "\t$date";
$date++;
print "\n\n";
}
}

if (day=2) { # if it's a monday
print "1\t2\t3\t4\t5\t6\t7\n";
$date=8;
.......#continue to type for 7 days
<code>

i know this program looks stupid but I do really need help with this. How to write codes so that the days do not exceed 28/29/30/31 days per month? and also leap year?? and how to decide the next day for the next new month?? thanks for anyone who help!! (sorry i dunno the html formatting)

Replies are listed 'Best First'.
Re: Simple calendar
by ikegami (Patriarch) on Mar 08, 2010 at 02:12 UTC

    How to write codes so that the days do not exceed 28/29/30/31 days per month? and also leap year??

    You don't. It's already been done. I use DateTime. Date::Calc is mentioned frequently.

    The program should accept [...] the day of the week for January 1 of that particular year

    That's not necessary since you have ->dow().

    sorry i dunno the html formatting

    On this site, <p> at the start of paragraphs, and <c>..</c> around computer text (code, data, output, etc).

      Thanks. Truly appreciate ur help. I will try it out.
Re: Simple calendar
by ww (Archbishop) on Mar 08, 2010 at 02:18 UTC
    perl_fool ("a biotechnology student who is forced to study programming for god's sake!!" according to your self-description):

        Does that mean you believe programming is beneath you? That even a one-glance nod at html instructions is an inappropriate demand upon "a biotechnology student?"

    sorry i dunno the html formatting

    Did you even look at the instructions on the page where you entered your question?

    And if you can't be troubled to research that (ie, read the notes above and the warnings below the text input box), why should we write code for you?

    But you did some research on the problem itself... you said so:

    I've looked through the calendar examples in this website but it's too hard for me, especially the use of local time function.

    Oh....

      :-(

    Same basic issue: lack of effort. So, since creating this kind of simple calendar is an often-solved task... as far as I'm concerned, it's currently yours alone; that is, beyond filling in the details in your supposed "guideline" (which, you're right, won't work, as is... but if it would, it wouldn't be a mere guideline).

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Simple calendar
by chuckbutler (Monsignor) on Mar 08, 2010 at 03:28 UTC

    The calendar thing is one those functions that you do use from day to day. Most of the time I have used Calendar::Calendar to generate a straight grid type, but you mentioned that this may no work for you.

    There are formulas to calculate month length, but the easiest is to set up a table, zero relative of the month length. Thirty days as November, April, June, and September, the others, save February, have thirty-one. February....

    In February a little math is necessary. It is a leap year, 29 day, if:

    $is_leap_year = (($year % 4) == 0) && ((($year % 100) != 0) #<- fixed typo... || (($year % 400) == 0));

    So, right to left, years that are divisible by 400 are leap years, years divisible by 100, but not 400, are not, other years, save those divisible by 100 before, that are divisible by 4 are, and what is left is not.

    I hope was able to give you some timely information.

    Good luck. -c

    Updated to fix a typo... -c

      There are formulas to calculate month length, but the easiest is to set up a table, zero relative of the month length. Thirty days as November, April, June, and September, the others, save February, have thirty-one. February....

      In February a little math is necessary.

      Yeah, that was my first impression as well. But then, given the year, it's also known what day Jan 1 is. But this homework problem allows users to actually set the day of the week of Jan 1. So, the exercise is about imaginary calenders and years. I mean, if Jan 1, 2011 is to be a Wednesday, such a 2011 might as well have a Feb 29.
      yeah i did see this before but i couldnt figure out anything about the 100 and 400. i will try to understand it. Thanks for ur help, I truly appreciate it.
        Wikipedia explains this.
Re: Simple calendar
by planetscape (Chancellor) on Mar 09, 2010 at 03:47 UTC
    a biotechnology student who is forced to study programming for god's sake!!

    If that truly represents your feelings (and I see the sentiments have since been removed), I have but one piece of advice:

    Get out of the sciences. Right now. They aren't for you.

    I am certain your advisor will be able to provide you with alternatives.

    HTH,

    planetscape

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://827299]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-28 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found