5. Data type library

Source: Internet
Author: User
Tags time zones timedelta

5. Data type library

This library mainly describes the use of different data types, such as date types, time types, fixed type arrays, heap queues, synchronization queues, and collections. Python also provides a number of built-in data types, such as Dict , List , Set and the Frozenset , tuple . and the str class supports Unicode strings, and thebytes class holds binary data. Here's a more data type library to learn about data type libraries.

5.1 datetime--Basic date and Time type

This module datetime provides a simple or complex way to use dates and times. and provide a mathematical calculation of the date and time, the date and time to extract part of the content to use, while the format and ease of operation. The module with this content also has the time and calendar modules.

the Datatime module provides the following constants:

Datetime. Minyear

Allows you to Set the minimum number of years in a date and DateTime object. The default minyear equals 1.

Datetime. Maxyear

Allows you to Set the maximum number of years in a date and DateTime object. The default maxyear equals 9999.

Example:

#python 3.4.3

Import datetime

Print (' DateTime '. Minyear: ', DateTime. Minyear)

Print (' DateTime '. Maxyear: ', DateTime. Maxyear)

The resulting output is as follows:

Datetime. Minyear:1

Datetime. maxyear:9999

5.1.1Available Types

Class Datetime.date

Represents the original date, using the Gregorian calendar. The common attributes are year,month , and Day.

Example:

#python 3.4.3

Import datetime

DT = Datetime.date

Print (' Date: ', Dt.max)

Print (' Date: ', Dt.today ())

The resulting output is as follows:

Date:9999-12-31

Date:2015-10-23

Class Datetime.time

An idealized time class, assuming that every day has 24*60*60 second time. Attributes are:hour,minute,second,microsecond , and tzinfo.

Example:

#python 3.4.3

Import datetime

DT = Datetime.time

Print (' Time: ', Dt.max)

Print (' Time: ', dt.resolution)

The resulting output is as follows:

time:23:59:59.999999

time:0:00:00.000001

Class Datetime.datetime

Combines the functions of class date and class time . Owning attributes are:year,month,day,hour, minute , Second , microsecond and the Tzinfo .

Example:

#python 3.4.3

Import datetime

DT = Datetime.datetime

Print (' Date: ', Dt.today ())

Print (' Time: ', Dt.max)

Print (' Time: ', dt.resolution)

The resulting output is as follows:

Date:2015-10-26 08:43:16.594399

Time:9999-12-31 23:59:59.999999

time:0:00:00.000001

Class Datetime.timedelta

Represents the difference between two date or time or datetime, with a precision of milliseconds (Ms).

Example:

#python 3.4.3

Import datetime

to = Datetime.datetime (2015, 10, 26)

dd = To-datetime.timedelta (days = 100)

Print (to, DD)

The resulting output is as follows:

2015-10-26 00:00:00 2015-07-18 00:00:00

Class Datetime.tzinfo

An abstract representation of the time zone information object. It is used primarily in datetime and time objects, to provide one time zone adjustment, and to calculate temporal differences based on time zone.

Example:

#python 3.4.3

Import datetime

# using the time zone class datetime.tzinfo

Class UTC (Datetime.tzinfo):

"" "UTC" ""

def __init__ (self,offset = 0):

Self._offset = Offset

def utcoffset (self, DT):

Return Datetime.timedelta (Hours=self._offset)

def tzname (self, DT):

Return "UTC +%s"% self._offset

def DST (self, DT):

Return Datetime.timedelta (Hours=self._offset)

# Beijing Time

BEIJING = Datetime.datetime.now (TZ = UTC (8))

# Bangkok Time

Bangkok = Datetime.datetime.now (tz = UTC (7))

# Beijing time turns into Bangkok time

Beijing.astimezone (UTC (7))

# The time zone is also considered when calculating the timing difference

Print (Beijing, Bangkok)

TimeSpan = Beijing-bangkok

Print (TimeSpan)

The resulting output is as follows:

2015-10-27 00:44:05.520355+08:00 2015-10-27 00:44:05.520355+07:00

-1 Day, 23:00:00

Class Datetime.timezone

A derived class that implements the Datetime.tzinfo interface, which is used primarily to manage UTC time zones.

Example:

#python 3.4.3

Import datetime

TZ = Datetime.timezone (Datetime.timedelta (hours = 8), ' utcshh:mm ')

Print (TZ.UTC)

Print (Tz.tzname)

The resulting output is as follows:

utc+00:00

<built-in method Tzname of Datetime.timezone object at 0x029ab280>

All of the above types of objects are objects that are not modified, and whose objects derive from the following:

Object

Timedelta

Tzinfo

TimeZone

Time

Date

Datetime

From here you can see that all classes are derived from Object Objects,timezone derive from the tzinfo class,datetime derived from the time and date classes.


Cai Junsheng qq:9073204 Shenzhen

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

5. Data type library

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.