Back to index

python3.2  3.2.2
Classes | Functions | Variables
calendar Namespace Reference

Classes

class  IllegalMonthError
class  IllegalWeekdayError
class  _localized_month
class  _localized_day
class  Calendar
class  TextCalendar
class  HTMLCalendar
class  different_locale
class  LocaleTextCalendar
class  LocaleHTMLCalendar

Functions

def isleap
def leapdays
def weekday
def monthrange
def setfirstweekday
def format
def formatstring
def timegm
def main

Variables

list __all__
 error = ValueError
int January = 1
int February = 2
list mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
tuple day_name = _localized_day('%A')
tuple day_abbr = _localized_day('%a')
tuple month_name = _localized_month('%B')
tuple month_abbr = _localized_month('%b')
tuple c = TextCalendar()
 firstweekday = c.getfirstweekday
 monthcalendar = c.monthdayscalendar
 prweek = c.prweek
 week = c.formatweek
 weekheader = c.formatweekheader
 prmonth = c.prmonth
 month = c.formatmonth
 calendar = c.formatyear
 prcal = c.pryear
int _colwidth = 7
int _spacing = 6
int EPOCH = 1970
tuple _EPOCH_ORD = datetime.date(EPOCH, 1, 1)

Detailed Description

Calendar printing functions

Note when comparing these calendars to the ones printed by cal(1): By
default, these calendars have Monday as the first day of the week, and
Sunday as the last (the European convention). Use setfirstweekday() to
set the first day of the week (0=Monday, 6=Sunday).

Function Documentation

def calendar.format (   cols,
  colwidth = _colwidth,
  spacing = _spacing 
)
Prints multi-column formatting for year calendars

Definition at line 578 of file calendar.py.

00578 
00579 def format(cols, colwidth=_colwidth, spacing=_spacing):
00580     """Prints multi-column formatting for year calendars"""
00581     print(formatstring(cols, colwidth, spacing))
00582 

Here is the call graph for this function:

def calendar.formatstring (   cols,
  colwidth = _colwidth,
  spacing = _spacing 
)
Returns a string formatted from n strings, centered within n columns.

Definition at line 583 of file calendar.py.

00583 
00584 def formatstring(cols, colwidth=_colwidth, spacing=_spacing):
00585     """Returns a string formatted from n strings, centered within n columns."""
00586     spacing *= ' '
00587     return spacing.join(c.center(colwidth) for c in cols)
00588 

Here is the caller graph for this function:

def calendar.isleap (   year)
Return True for leap years, False for non-leap years.

Definition at line 97 of file calendar.py.

00097 
00098 def isleap(year):
00099     """Return True for leap years, False for non-leap years."""
00100     return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
00101 

Here is the caller graph for this function:

def calendar.leapdays (   y1,
  y2 
)
Return number of leap years in range [y1, y2).
   Assume y1 <= y2.

Definition at line 102 of file calendar.py.

00102 
00103 def leapdays(y1, y2):
00104     """Return number of leap years in range [y1, y2).
00105        Assume y1 <= y2."""
00106     y1 -= 1
00107     y2 -= 1
00108     return (y2//4 - y1//4) - (y2//100 - y1//100) + (y2//400 - y1//400)
00109 

Here is the caller graph for this function:

def calendar.main (   args)

Definition at line 603 of file calendar.py.

00603 
00604 def main(args):
00605     import optparse
00606     parser = optparse.OptionParser(usage="usage: %prog [options] [year [month]]")
00607     parser.add_option(
00608         "-w", "--width",
00609         dest="width", type="int", default=2,
00610         help="width of date column (default 2, text only)"
00611     )
00612     parser.add_option(
00613         "-l", "--lines",
00614         dest="lines", type="int", default=1,
00615         help="number of lines for each week (default 1, text only)"
00616     )
00617     parser.add_option(
00618         "-s", "--spacing",
00619         dest="spacing", type="int", default=6,
00620         help="spacing between months (default 6, text only)"
00621     )
00622     parser.add_option(
00623         "-m", "--months",
00624         dest="months", type="int", default=3,
00625         help="months per row (default 3, text only)"
00626     )
00627     parser.add_option(
00628         "-c", "--css",
00629         dest="css", default="calendar.css",
00630         help="CSS to use for page (html only)"
00631     )
00632     parser.add_option(
00633         "-L", "--locale",
00634         dest="locale", default=None,
00635         help="locale to be used from month and weekday names"
00636     )
00637     parser.add_option(
00638         "-e", "--encoding",
00639         dest="encoding", default=None,
00640         help="Encoding to use for output."
00641     )
00642     parser.add_option(
00643         "-t", "--type",
00644         dest="type", default="text",
00645         choices=("text", "html"),
00646         help="output type (text or html)"
00647     )
00648 
00649     (options, args) = parser.parse_args(args)
00650 
00651     if options.locale and not options.encoding:
00652         parser.error("if --locale is specified --encoding is required")
00653         sys.exit(1)
00654 
00655     locale = options.locale, options.encoding
00656 
00657     if options.type == "html":
00658         if options.locale:
00659             cal = LocaleHTMLCalendar(locale=locale)
00660         else:
00661             cal = HTMLCalendar()
00662         encoding = options.encoding
00663         if encoding is None:
00664             encoding = sys.getdefaultencoding()
00665         optdict = dict(encoding=encoding, css=options.css)
00666         write = sys.stdout.buffer.write
00667         if len(args) == 1:
00668             write(cal.formatyearpage(datetime.date.today().year, **optdict))
00669         elif len(args) == 2:
00670             write(cal.formatyearpage(int(args[1]), **optdict))
00671         else:
00672             parser.error("incorrect number of arguments")
00673             sys.exit(1)
00674     else:
00675         if options.locale:
00676             cal = LocaleTextCalendar(locale=locale)
00677         else:
00678             cal = TextCalendar()
00679         optdict = dict(w=options.width, l=options.lines)
00680         if len(args) != 3:
00681             optdict["c"] = options.spacing
00682             optdict["m"] = options.months
00683         if len(args) == 1:
00684             result = cal.formatyear(datetime.date.today().year, **optdict)
00685         elif len(args) == 2:
00686             result = cal.formatyear(int(args[1]), **optdict)
00687         elif len(args) == 3:
00688             result = cal.formatmonth(int(args[1]), int(args[2]), **optdict)
00689         else:
00690             parser.error("incorrect number of arguments")
00691             sys.exit(1)
00692         write = sys.stdout.write
00693         if options.encoding:
00694             result = result.encode(options.encoding)
00695             write = sys.stdout.buffer.write
00696         write(result)
00697 

Here is the call graph for this function:

def calendar.monthrange (   year,
  month 
)
Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
   year, month.

Definition at line 116 of file calendar.py.

00116 
00117 def monthrange(year, month):
00118     """Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
00119        year, month."""
00120     if not 1 <= month <= 12:
00121         raise IllegalMonthError(month)
00122     day1 = weekday(year, month, 1)
00123     ndays = mdays[month] + (month == February and isleap(year))
00124     return day1, ndays
00125 

Here is the call graph for this function:

Here is the caller graph for this function:

def calendar.setfirstweekday (   firstweekday)

Definition at line 558 of file calendar.py.

00558 
00559 def setfirstweekday(firstweekday):
00560     if not MONDAY <= firstweekday <= SUNDAY:
00561         raise IllegalWeekdayError(firstweekday)
00562     c.firstweekday = firstweekday

Here is the caller graph for this function:

def calendar.timegm (   tuple)
Unrelated but handy function to calculate Unix timestamp from GMT.

Definition at line 593 of file calendar.py.

00593 
00594 def timegm(tuple):
00595     """Unrelated but handy function to calculate Unix timestamp from GMT."""
00596     year, month, day, hour, minute, second = tuple[:6]
00597     days = datetime.date(year, month, 1).toordinal() - _EPOCH_ORD + day - 1
00598     hours = days*24 + hour
00599     minutes = hours*60 + minute
00600     seconds = minutes*60 + second
00601     return seconds
00602 

Here is the caller graph for this function:

def calendar.weekday (   year,
  month,
  day 
)
Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
   day (1-31).

Definition at line 110 of file calendar.py.

00110 
00111 def weekday(year, month, day):
00112     """Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
00113        day (1-31)."""
00114     return datetime.date(year, month, day).weekday()
00115 

Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
00002            "firstweekday", "isleap", "leapdays", "weekday", "monthrange",
00003            "monthcalendar", "prmonth", "month", "prcal", "calendar",
00004            "timegm", "month_name", "month_abbr", "day_name", "day_abbr"]

Definition at line 12 of file calendar.py.

Definition at line 574 of file calendar.py.

Definition at line 590 of file calendar.py.

Definition at line 575 of file calendar.py.

Definition at line 554 of file calendar.py.

calendar.calendar = c.formatyear

Definition at line 569 of file calendar.py.

Definition at line 87 of file calendar.py.

Definition at line 86 of file calendar.py.

Definition at line 589 of file calendar.py.

calendar.error = ValueError

Definition at line 18 of file calendar.py.

Definition at line 37 of file calendar.py.

calendar.firstweekday = c.getfirstweekday

Definition at line 556 of file calendar.py.

Definition at line 36 of file calendar.py.

list calendar.mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Definition at line 40 of file calendar.py.

calendar.month = c.formatmonth

Definition at line 568 of file calendar.py.

Definition at line 91 of file calendar.py.

Definition at line 90 of file calendar.py.

calendar.monthcalendar = c.monthdayscalendar

Definition at line 563 of file calendar.py.

calendar.prcal = c.pryear

Definition at line 570 of file calendar.py.

calendar.prmonth = c.prmonth

Definition at line 567 of file calendar.py.

calendar.prweek = c.prweek

Definition at line 564 of file calendar.py.

calendar.week = c.formatweek

Definition at line 565 of file calendar.py.

calendar.weekheader = c.formatweekheader

Definition at line 566 of file calendar.py.