網頁

2011年8月11日星期四

Python: datetime

The datetime module supplies classes for manipulating dates and times in both simple and complex ways.

Available Types

class datetime.date
An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: year, month, and day.

class datetime.time
An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds (there is no notion of “leap seconds” here). Attributes: hour, minute, second, microsecond, and tzinfo.

class datetime.datetime
A combination of a date and a time. Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo.

class datetime.timedelta
A duration expressing the difference between two date, time, or datetime instances to microsecond resolution.

class datetime.tzinfo
An abstract base class for time zone information objects. These are used by the datetime and time classes to provide a customizable notion of time adjustment (for example, to account for time zone and/or daylight saving time).

timedelta Objects
class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)



AttributeValue
daysBetween -999999999 and 999999999 inclusive
secondsBetween 0 and 86399 inclusive
microsecondsBetween 0 and 999999 inclusive


date Objects
class datetime.date(year, month, day)

datetime Objects
class datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])


Supported operations:
OperationResult
datetime2 = datetime1 + timedelta(1)
datetime2 = datetime1 - timedelta(2)
timedelta = datetime1 - datetime2(3)
datetime1 < datetime2Compares datetime to datetime. (4)


Some class methos:

datetime.strftime(format)
Return a string representing the date and time, controlled by an explicit format string. See section strftime() and strptime() Behavior.

The following is a list of all the format codes that the C standard (1989 version) requires, and these work on all platforms with a standard C implementation. Note that the 1999 version of the C standard added additional format codes.

DirectiveMeaningNotes
%aLocale’s abbreviated weekday name.
%ALocale’s full weekday name.
%bLocale’s abbreviated month name.
%BLocale’s full month name.
%cLocale’s appropriate date and time representation.
%dDay of the month as a decimal number [01,31].
%fMicrosecond as a decimal number [0,999999], zero-padded on the left(1)
%HHour (24-hour clock) as a decimal number [00,23].
%IHour (12-hour clock) as a decimal number [01,12].
%jDay of the year as a decimal number [001,366].
%mMonth as a decimal number [01,12].
%MMinute as a decimal number [00,59].
%pLocale’s equivalent of either AM or PM.(2)
%SSecond as a decimal number [00,61].(3)
%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.(4)
%wWeekday as a decimal number [0(Sunday),6].
%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.(4)
%xLocale’s appropriate date representation.
%XLocale’s appropriate time representation.
%yYear without century as a decimal number [00,99].
%YYear with century as a decimal number.
%zUTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).(5)
%ZTime zone name (empty string if the object is naive).
%%A literal '%' character.









沒有留言:

發佈留言