5.1.5 Time Object

Source: Internet
Author: User
Tags timedelta

The object created by class time represents the local times and is not related to any special date. Time can be adjusted through time zone tzinfo .

Class Datetime.time (hour=0, minute=0, Second=0, Microsecond=0, Tzinfo=none)

All parameters are optional. In addition to the Tzinfo object, other parameters are integer representations. The range of parameters is as follows:

L 0 <= Hour < 24

L 0 <= Minute < 60

L 0 <= Second < 60

L 0 <= Microsecond < 1000000

An exception ValueError is thrown when the parameter range exceeds these ranges .

Used primarily to create a time object.

Example:

#python 3.4.3

Import datetime

Time = datetime.time (hour = 10)

Print (' Time: ', time)

The resulting output is as follows:

time:10:00:00

Class Properties:

Time.min

Returns the Time object that represents the minimum.

Time.max

Returns the Time object that represents the largest.

Time.resolution

Returns the precision of the minimum difference between two time differences. is currently a microsecond.

Example:

#python 3.4.3

Import datetime

Time = datetime.time (hour = 10)

Print (' Time.min ', time.min)

Print (' Time.max ', Time.max)

Print (' time.resolution ', time.resolution)

The resulting output is as follows:

Time.min 00:00:00

Time.max 23:59:59.999999

Time.resolution 0:00:00.000001

Instance properties can only be read and cannot be modified:

Time.hour

The range is the hour.

Time.minute

The range is minutes.

Time.second

The range is in seconds.

Time.microsecond

The range is 1000000 microseconds.

Time.tzinfo

Save the time zone information object or save it as nonewhen it is not.

Example:

#python 3.4.3

Import datetime

Time = datetime.time (hour = ten, minute = 1, second = $, microsecond = 30)

Print (' Time.hour: ', Time.hour)

Print (' Time.minute: ', Time.minute)

Print (' Time.second: ', Time.second)

Print (' Time.microsecond: ', Time.microsecond)

The resulting output is as follows:

Time.hour:10

Time.minute:1

Time.second:20

Time.microsecond:30

Time.replace ([hour[, minute[, second[, microsecond[, Tzinfo]] []]

By modifying some parameters to get a new time object, other parameters that are not modified are consistent with the instance.

Example:

#python 3.4.3

Import datetime

Time = datetime.time (hour = ten, minute = 1, second = $, microsecond = 30)

New = Time.replace (20)

Print (' Time.hour: ', Time.hour)

Print (' New.hour: ', New.hour)

The resulting output is as follows:

Time.hour:10

New.hour:20

Time.isoformat ()

Returns a time string formatted with the ISO8601 standard. If the microsecond is not 0, format is formatted as HH:MM:SS.mmmmmm , and if Microsecond is 0, press hh:mm: SS to format. If there is a time zone, add the time zone later:hh:mm:ss.mmmmmm+hh:mm or hh:mm:ss+hh:mm.

Example:

#python 3.4.3

Import datetime

Time = datetime.time (hour = ten, minute = 1, second = $, microsecond = 30)

Print (' Time.isoformat: ', Time.isoformat ())

The resulting output is as follows:

time.isoformat:10:01:20.000030

TIME.__STR__ ()

The str () function of the time object is formatted, andstr (t) is equivalent to t.isoformat ().

Example:

#python 3.4.3

Import datetime

Time = datetime.time (hour = ten, minute = 1, second = $, microsecond = 30)

Print (' Time.isoformat: ', str (time))

The resulting output is as follows:

time.isoformat:10:01:20.000030

Time.strftime (format)

Formatting time is controlled by a format string.

Example:

#python 3.4.3

Import datetime

Time = datetime.time (hour = ten, minute = 1, second = $, microsecond = 30)

Print (' Time.isoformat: ', Time.strftime ('%h:%m:%s '))

The resulting output is as follows:

Time.isoformat:10:01:20

TIME.__FORMAT__ (format)

The same function as time.strftime () , just this function is used in Str.format () call.

Time.utcoffset ()

Returns the relative time of UTC. Returns None if the time zone tzinfo is empty . If not NULL, returns self.tzinfo.utcoffset (None).

TIME.DST ()

If tzinfo returns None. returns SELF.TZINFO.DST (None)if tzinfo is not empty .

Time.tzname ()

Returns the name of the time zone. If tzinfo is empty, returns None.

Example:

#python 3.4.3

Import datetime

Class GMT8 (Datetime.tzinfo):

def utcoffset (self, DT):

Return Datetime.timedelta (hours=8)

def DST (self, DT):

Return Datetime.timedelta (0)

def tzname (SELF,DT):

Return "Asia/beijing"

T = datetime.time (+, TZINFO=GMT8 ())

Print (t)

Print (T.DST ())

Print (T.tzname ())

Print (' The {} is {:%h:%m}. '. Format ("Time", T))

The resulting output is as follows:

12:10:30+08:00

0:00:00

Asia/beijing

The time is 12:10.


Cai Junsheng qq:9073204 Shenzhen

5.1.5 Time Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.