Back to index

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

Classes

class  timedelta
class  date
class  tzinfo
class  time
class  datetime
class  timezone

Functions

def _cmp
def _is_leap
def _days_before_year
def _days_in_month
def _days_before_month
def _ymd2ord
def _ord2ymd
def _build_struct_time
def _format_time
def _wrap_strftime
def _call_tzinfo_method
def _check_tzname
def _check_utc_offset
def _check_date_fields
def _check_time_fields
def _check_tzinfo_arg
def _cmperror
def _isoweek1monday

Variables

int MINYEAR = 1
int MAXYEAR = 9999
int _MAXORDINAL = 3652059
list _DAYS_IN_MONTH = [None, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
list _DAYS_BEFORE_MONTH = [None]
int dbm = 0
tuple _DI400Y = _days_before_year(401)
tuple _DI100Y = _days_before_year(101)
tuple _DI4Y = _days_before_year(5)
list _MONTHNAMES
list _DAYNAMES = [None, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
 _date_class = date
 _tzinfo_class = tzinfo
 _time_class = time

Detailed Description

Concrete date/time and related types -- prototype implemented in Python.

See http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage

See also http://dir.yahoo.com/Reference/calendars/

For a primer on DST, including many current DST rules, see
http://webexhibits.org/daylightsaving/

For more about DST than you ever wanted to know, see
ftp://elsie.nci.nih.gov/pub/

Sources for time zone and DST data: http://www.twinsun.com/tz/tz-link.htm

This was originally copied from the sandbox of the CPython CVS repository.
Thanks to Tim Peters for suggesting using it.

Function Documentation

def datetime._build_struct_time (   y,
  m,
  d,
  hh,
  mm,
  ss,
  dstflag 
) [private]

Definition at line 161 of file datetime.py.

00161 
00162 def _build_struct_time(y, m, d, hh, mm, ss, dstflag):
00163     wday = (_ymd2ord(y, m, d) + 6) % 7
00164     dnum = _days_before_month(y, m) + d
00165     return _time.struct_time((y, m, d, hh, mm, ss, wday, dnum, dstflag))

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime._call_tzinfo_method (   tzinfo,
  methname,
  tzinfoarg 
) [private]

Definition at line 235 of file datetime.py.

00235 
00236 def _call_tzinfo_method(tzinfo, methname, tzinfoarg):
00237     if tzinfo is None:
00238         return None
00239     return getattr(tzinfo, methname)(tzinfoarg)
00240 
# Just raise TypeError if the arg isn't None or a string.

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime._check_date_fields (   year,
  month,
  day 
) [private]

Definition at line 267 of file datetime.py.

00267 
00268 def _check_date_fields(year, month, day):
00269     if not isinstance(year, int):
00270         raise TypeError('int expected')
00271     if not MINYEAR <= year <= MAXYEAR:
00272         raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year)
00273     if not 1 <= month <= 12:
00274         raise ValueError('month must be in 1..12', month)
00275     dim = _days_in_month(year, month)
00276     if not 1 <= day <= dim:
00277         raise ValueError('day must be in 1..%d' % dim, day)

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime._check_time_fields (   hour,
  minute,
  second,
  microsecond 
) [private]

Definition at line 278 of file datetime.py.

00278 
00279 def _check_time_fields(hour, minute, second, microsecond):
00280     if not isinstance(hour, int):
00281         raise TypeError('int expected')
00282     if not 0 <= hour <= 23:
00283         raise ValueError('hour must be in 0..23', hour)
00284     if not 0 <= minute <= 59:
00285         raise ValueError('minute must be in 0..59', minute)
00286     if not 0 <= second <= 59:
00287         raise ValueError('second must be in 0..59', second)
00288     if not 0 <= microsecond <= 999999:
00289         raise ValueError('microsecond must be in 0..999999', microsecond)

Here is the caller graph for this function:

def datetime._check_tzinfo_arg (   tz) [private]

Definition at line 290 of file datetime.py.

00290 
00291 def _check_tzinfo_arg(tz):
00292     if tz is not None and not isinstance(tz, tzinfo):
00293         raise TypeError("tzinfo argument must be None or of a tzinfo subclass")

Here is the caller graph for this function:

def datetime._check_tzname (   name) [private]

Definition at line 241 of file datetime.py.

00241 
00242 def _check_tzname(name):
00243     if name is not None and not isinstance(name, str):
00244         raise TypeError("tzinfo.tzname() must return None or string, "
00245                         "not '%s'" % type(name))
00246 
00247 # name is the offset-producing method, "utcoffset" or "dst".
00248 # offset is what it returned.
00249 # If offset isn't None or timedelta, raises TypeError.
00250 # If offset is None, returns None.
00251 # Else offset is checked for being in range, and a whole # of minutes.
# If it is, its integer value is returned.  Else ValueError is raised.

Here is the caller graph for this function:

def datetime._check_utc_offset (   name,
  offset 
) [private]

Definition at line 252 of file datetime.py.

00252 
00253 def _check_utc_offset(name, offset):
00254     assert name in ("utcoffset", "dst")
00255     if offset is None:
00256         return
00257     if not isinstance(offset, timedelta):
00258         raise TypeError("tzinfo.%s() must return None "
00259                         "or timedelta, not '%s'" % (name, type(offset)))
00260     if offset % timedelta(minutes=1) or offset.microseconds:
00261         raise ValueError("tzinfo.%s() must return a whole number "
00262                          "of minutes, got %s" % (name, offset))
00263     if not -timedelta(1) < offset < timedelta(1):
00264         raise ValueError("%s()=%s, must be must be strictly between"
00265                          " -timedelta(hours=24) and timedelta(hours=24)"
00266                          % (name, offset))

Here is the caller graph for this function:

def datetime._cmp (   x,
  y 
) [private]

Definition at line 22 of file datetime.py.

00022 
00023 def _cmp(x, y):
00024     return 0 if x == y else 1 if x > y else -1

Here is the caller graph for this function:

def datetime._cmperror (   x,
  y 
) [private]

Definition at line 294 of file datetime.py.

00294 
00295 def _cmperror(x, y):
00296     raise TypeError("can't compare '%s' to '%s'" % (
00297                     type(x).__name__, type(y).__name__))

Here is the caller graph for this function:

def datetime._days_before_month (   year,
  month 
) [private]

Definition at line 63 of file datetime.py.

00063 
00064 def _days_before_month(year, month):
00065     "year, month -> number of days in year preceeding first day of month."
00066     assert 1 <= month <= 12, 'month must be in 1..12'
00067     return _DAYS_BEFORE_MONTH[month] + (month > 2 and _is_leap(year))

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime._days_before_year (   year) [private]

Definition at line 51 of file datetime.py.

00051 
00052 def _days_before_year(year):
00053     "year -> number of days before January 1st of year."
00054     y = year - 1
00055     return y*365 + y//4 - y//100 + y//400

Here is the caller graph for this function:

def datetime._days_in_month (   year,
  month 
) [private]

Definition at line 56 of file datetime.py.

00056 
00057 def _days_in_month(year, month):
00058     "year, month -> number of days in that month in that year."
00059     assert 1 <= month <= 12, month
00060     if month == 2 and _is_leap(year):
00061         return 29
00062     return _DAYS_IN_MONTH[month]

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime._format_time (   hh,
  mm,
  ss,
  us 
) [private]

Definition at line 166 of file datetime.py.

00166 
00167 def _format_time(hh, mm, ss, us):
00168     # Skip trailing microseconds when us==0.
00169     result = "%02d:%02d:%02d" % (hh, mm, ss)
00170     if us:
00171         result += ".%06d" % us
00172     return result
00173 
# Correctly substitute for %z and %Z escapes in strftime formats.

Here is the caller graph for this function:

def datetime._is_leap (   year) [private]

Definition at line 47 of file datetime.py.

00047 
00048 def _is_leap(year):
00049     "year -> 1 if leap year, else 0."
00050     return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

Here is the caller graph for this function:

def datetime._isoweek1monday (   year) [private]

Definition at line 1775 of file datetime.py.

01775 
01776 def _isoweek1monday(year):
01777     # Helper to calculate the day number of the Monday starting week 1
01778     # XXX This could be done more efficiently
01779     THURSDAY = 3
01780     firstday = _ymd2ord(year, 1, 1)
01781     firstweekday = (firstday + 6) % 7 # See weekday() above
01782     week1monday = firstday - firstweekday
01783     if firstweekday > THURSDAY:
01784         week1monday += 7
01785     return week1monday

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime._ord2ymd (   n) [private]

Definition at line 93 of file datetime.py.

00093 
00094 def _ord2ymd(n):
00095     "ordinal -> (year, month, day), considering 01-Jan-0001 as day 1."
00096 
00097     # n is a 1-based index, starting at 1-Jan-1.  The pattern of leap years
00098     # repeats exactly every 400 years.  The basic strategy is to find the
00099     # closest 400-year boundary at or before n, then work with the offset
00100     # from that boundary to n.  Life is much clearer if we subtract 1 from
00101     # n first -- then the values of n at 400-year boundaries are exactly
00102     # those divisible by _DI400Y:
00103     #
00104     #     D  M   Y            n              n-1
00105     #     -- --- ----        ----------     ----------------
00106     #     31 Dec -400        -_DI400Y       -_DI400Y -1
00107     #      1 Jan -399         -_DI400Y +1   -_DI400Y      400-year boundary
00108     #     ...
00109     #     30 Dec  000        -1             -2
00110     #     31 Dec  000         0             -1
00111     #      1 Jan  001         1              0            400-year boundary
00112     #      2 Jan  001         2              1
00113     #      3 Jan  001         3              2
00114     #     ...
00115     #     31 Dec  400         _DI400Y        _DI400Y -1
00116     #      1 Jan  401         _DI400Y +1     _DI400Y      400-year boundary
00117     n -= 1
00118     n400, n = divmod(n, _DI400Y)
00119     year = n400 * 400 + 1   # ..., -399, 1, 401, ...
00120 
00121     # Now n is the (non-negative) offset, in days, from January 1 of year, to
00122     # the desired date.  Now compute how many 100-year cycles precede n.
00123     # Note that it's possible for n100 to equal 4!  In that case 4 full
00124     # 100-year cycles precede the desired day, which implies the desired
00125     # day is December 31 at the end of a 400-year cycle.
00126     n100, n = divmod(n, _DI100Y)
00127 
00128     # Now compute how many 4-year cycles precede it.
00129     n4, n = divmod(n, _DI4Y)
00130 
00131     # And now how many single years.  Again n1 can be 4, and again meaning
00132     # that the desired day is December 31 at the end of the 4-year cycle.
00133     n1, n = divmod(n, 365)
00134 
00135     year += n100 * 100 + n4 * 4 + n1
00136     if n1 == 4 or n100 == 4:
00137         assert n == 0
00138         return year-1, 12, 31
00139 
00140     # Now the year is correct, and n is the offset from January 1.  We find
00141     # the month via an estimate that's either exact or one too large.
00142     leapyear = n1 == 3 and (n4 != 24 or n100 == 3)
00143     assert leapyear == _is_leap(year)
00144     month = (n + 50) >> 5
00145     preceding = _DAYS_BEFORE_MONTH[month] + (month > 2 and leapyear)
00146     if preceding > n:  # estimate is too large
00147         month -= 1
00148         preceding -= _DAYS_IN_MONTH[month] + (month == 2 and leapyear)
00149     n -= preceding
00150     assert 0 <= n < _days_in_month(year, month)
00151 
00152     # Now the year and month are correct, and n is the offset from the
00153     # start of that month:  we're done!
00154     return year, month, n+1
00155 
# Month and day names.  For localized versions, see the calendar module.

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime._wrap_strftime (   object,
  format,
  timetuple 
) [private]

Definition at line 174 of file datetime.py.

00174 
00175 def _wrap_strftime(object, format, timetuple):
00176     year = timetuple[0]
00177     if year < 1000:
00178         raise ValueError("year=%d is before 1000; the datetime strftime() "
00179                          "methods require year >= 1000" % year)
00180     # Don't call utcoffset() or tzname() unless actually needed.
00181     freplace = None # the string to use for %f
00182     zreplace = None # the string to use for %z
00183     Zreplace = None # the string to use for %Z
00184 
00185     # Scan format for %z and %Z escapes, replacing as needed.
00186     newformat = []
00187     push = newformat.append
00188     i, n = 0, len(format)
00189     while i < n:
00190         ch = format[i]
00191         i += 1
00192         if ch == '%':
00193             if i < n:
00194                 ch = format[i]
00195                 i += 1
00196                 if ch == 'f':
00197                     if freplace is None:
00198                         freplace = '%06d' % getattr(object,
00199                                                     'microsecond', 0)
00200                     newformat.append(freplace)
00201                 elif ch == 'z':
00202                     if zreplace is None:
00203                         zreplace = ""
00204                         if hasattr(object, "utcoffset"):
00205                             offset = object.utcoffset()
00206                             if offset is not None:
00207                                 sign = '+'
00208                                 if offset.days < 0:
00209                                     offset = -offset
00210                                     sign = '-'
00211                                 h, m = divmod(offset, timedelta(hours=1))
00212                                 assert not m % timedelta(minutes=1), "whole minute"
00213                                 m //= timedelta(minutes=1)
00214                                 zreplace = '%c%02d%02d' % (sign, h, m)
00215                     assert '%' not in zreplace
00216                     newformat.append(zreplace)
00217                 elif ch == 'Z':
00218                     if Zreplace is None:
00219                         Zreplace = ""
00220                         if hasattr(object, "tzname"):
00221                             s = object.tzname()
00222                             if s is not None:
00223                                 # strftime is going to have at this: escape %
00224                                 Zreplace = s.replace('%', '%%')
00225                     newformat.append(Zreplace)
00226                 else:
00227                     push('%')
00228                     push(ch)
00229             else:
00230                 push('%')
00231         else:
00232             push(ch)
00233     newformat = "".join(newformat)
00234     return _time.strftime(newformat, timetuple)

Here is the call graph for this function:

Here is the caller graph for this function:

def datetime._ymd2ord (   year,
  month,
  day 
) [private]

Definition at line 68 of file datetime.py.

00068 
00069 def _ymd2ord(year, month, day):
00070     "year, month, day -> ordinal, considering 01-Jan-0001 as day 1."
00071     assert 1 <= month <= 12, 'month must be in 1..12'
00072     dim = _days_in_month(year, month)
00073     assert 1 <= day <= dim, ('day must be in 1..%d' % dim)
00074     return (_days_before_year(year) +
00075             _days_before_month(year, month) +
00076             day)

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 918 of file datetime.py.

list datetime._DAYNAMES = [None, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

Definition at line 158 of file datetime.py.

Definition at line 40 of file datetime.py.

list datetime._DAYS_IN_MONTH = [None, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Definition at line 38 of file datetime.py.

Definition at line 78 of file datetime.py.

Definition at line 77 of file datetime.py.

Definition at line 79 of file datetime.py.

Definition at line 27 of file datetime.py.

Initial value:
00001 [None, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00002                      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

Definition at line 156 of file datetime.py.

Definition at line 1296 of file datetime.py.

Definition at line 991 of file datetime.py.

Definition at line 41 of file datetime.py.

Definition at line 26 of file datetime.py.

Definition at line 25 of file datetime.py.