(iv) Time and import of 4-4 python

Source: Internet
Author: User
Tags timedelta

The Import import statement acts as a means of importing a module, which can appear anywhere in the program. Shortcut keys are ALT + ENTER to import modules directly

1. If the file is imported locally, you can use it directly: import filename
2, if the import is a package, the package can be imported under the __init__.py, otherwise it will error, as long as there is a __init.py file, the Python parser will take this directory as a package
2 commonly used formats
A, from ... import ... What is imported from what module, and what can eventually be imported is a function, class, and module
B, import ...
Summary: one-layer call
Note: What is imported after import, what must be written at the time of the call.
Use from import, can not write suffix name when importing
For example:
The directory structure is as follows:

d:.│  Model.py│└─zhan        test.py        test.pyc        __init__. py        __init__  . pyctest.pydef  Hello ():    print("Hello World"  ") model.py#  import  zhan.test#  Zhan.test.hello ()fromimport  Hellohello ()

Operation Result:

Hello World

DateTime acquisition Time

The DateTime module defines the following classes:
Datetime.date: A class that represents a date. The commonly used attributes are year, month and day;
Datetime.time: A class that represents time. The commonly used properties are hour, minute, second, microsecond;
Datetime.datetime: Represents the DateTime.
Datetime.timedelta: Represents the time interval, which is the length between two points in time.
Datetime.tzinfo: Related information about the time zone.
Note: These types of objects above are immutable (immutable).
For example:

 fromDatetimeImportDatetimenow_time=DateTime.Now ()Print(now_time) new_time= Now_time.strftime ("%y-%m-%d%h:%m:%s")Print(new_time) Now1= Now_time.strftime ("%c")Print(now1) yesterday_time= Now_time + Timedelta (days=-1)Print(yesterday_time) tomorrow_time= Now_time + Timedelta (days=+1,minutes=+100)Print(Tomorrow_time.strftime ("%y-%m-%d%h:%m:%s"))

Operation Result:

2017-11-08 22:47:56.0250002017-11-08 22:47:5611/08/17 22:47:562017-11-07 23:04:05.7380002017-11-10 00:44:05

Note: 1, first import the DateTime class
2. Get the time required by the now method of the DateTime
3, DateTime more usage, can be viewed through the Dir method
4, Now_time or a DateTime class, converted to a string by the Strftime method, the strftime parameter is the form that needs to convert the string
5. Common parameters of Strftime
%Y decimal year with century part
%m Decimal Month
The day ordinal of a month in%d decimal notation
%H 24 Hours of production
%m Decimal Minutes
%s decimal seconds
%c standard Time, such as: 11/08/17 22:47:56

Time
Time-Exclusive usage:
Import time
For I in Xrange (1,10):
Print (i)
Time.sleep (1)
Execution Result:
1
2

Note: The program outputs a result every 1 seconds

Conversion of time formats to each other

 fromDatetimeImportDatetimenow_time=DateTime.Now ()Print(now_time)Print(Type (now_time))Print("*****time to STR * * * * *") _time= Now_time.strftime ("%y-%m-%d%h:%m:%s")Print(_time)Print(Type (_time))Print("*****STR to TIME * * * * *") _d_time= Datetime.strptime (_time,"%y-%m-%d%h:%m:%s")Print(_d_time)Print(Type (_d_time))

Operation Result:

' Datetime.datetime ' ' Str ' ' Datetime.datetime '>

Timestamp to Time object

 from Import datetime Import TimeNow   = time.time ()print= Datetime.fromtimestamp (now)print(now_ Time)print(Type (now_time))

Operation Result:

' Datetime.datetime '>

(iv) Time and import of 4-4 python

Related Article

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.