Simple textual calendar

skip at pobox.com skip at pobox.com
Mon Nov 3 09:07:07 EST 2008


    ostra> It is an example which I've setted myself to try to solve to see
    ostra> how it would go.

An even simpler solution is the cal command on Unix systems:

    % cal 2009
                                    2009

             Jan                    Feb                    Mar
     S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                 1  2  3    1  2  3  4  5  6  7    1  2  3  4  5  6  7
     4  5  6  7  8  9 10    8  9 10 11 12 13 14    8  9 10 11 12 13 14
    11 12 13 14 15 16 17   15 16 17 18 19 20 21   15 16 17 18 19 20 21
    18 19 20 21 22 23 24   22 23 24 25 26 27 28   22 23 24 25 26 27 28
    25 26 27 28 29 30 31                          29 30 31

             Apr                    May                    Jun
     S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
              1  2  3  4                   1  2       1  2  3  4  5  6
     5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
    12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
    19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
    26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
                           31
             Jul                    Aug                    Sep
     S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
              1  2  3  4                      1          1  2  3  4  5
     5  6  7  8  9 10 11    2  3  4  5  6  7  8    6  7  8  9 10 11 12
    12 13 14 15 16 17 18    9 10 11 12 13 14 15   13 14 15 16 17 18 19
    19 20 21 22 23 24 25   16 17 18 19 20 21 22   20 21 22 23 24 25 26
    26 27 28 29 30 31      23 24 25 26 27 28 29   27 28 29 30
                           30 31
             Oct                    Nov                    Dec
     S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                 1  2  3    1  2  3  4  5  6  7          1  2  3  4  5
     4  5  6  7  8  9 10    8  9 10 11 12 13 14    6  7  8  9 10 11 12
    11 12 13 14 15 16 17   15 16 17 18 19 20 21   13 14 15 16 17 18 19
    18 19 20 21 22 23 24   22 23 24 25 26 27 28   20 21 22 23 24 25 26
    25 26 27 28 29 30 31   29 30                  27 28 29 30 31

:-)

If you really want to write something yourself take a look at the datetime
module.  Here's a trivial (untested) example:

    import datetime
    date = datetime.datetime(2009, 1, 1)
    end = datetime.datetime(2010, 1, 1)
    oneday = datetime.timedelta(days=1)
    while date < end:
        print date.date()
        date += oneday

Correctness, formatting and efficiency are left as an exercise for the
reader. ;-)

-- 
Skip Montanaro - skip at pobox.com - http://www.webfast.com/~skip/



More information about the Python-list mailing list