Common time methods for Python

Source: Internet
Author: User


In Jython

Let's start by importing a module that must be used
>>> Import Time
Set a time format that will be used below
>>>isotimeformat= '%y-%m-%d%x '
Look at the current time, similar to many other languages this is the number of seconds since the epoch (January 1, 1970 00:00:00) to the current.
>>> Time.time ()
1180759620.859
I can't read the above, let's look at it in a different format.
>>> Time.localtime ()
(2007, 6, 2, 12, 47, 7, 5, 153, 0)
LocalTime returns the time in tuple format, a function similar to it is called gmtime (), the difference between 2 functions is the time zone, Gmtime () returns the value of 0 time zone, LocalTime returns the value of the current time zone.
>>> time.strftime (Isotimeformat, Time.localtime ())
' 2007-06-02 12:54:29′
With our time format defined, using Strftime to make a transition to time, if the time taken now, time.localtime () can not be used.

>>> time.strftime (Isotimeformat, Time.localtime (Time.time ()))
' 2007-06-02 12:54:31′
>>> time.strftime (Isotimeformat, Time.gmtime (Time.time ()))
' 2007-06-02 04:55:02′
The above shows the difference between Gmtime and localtime.
View time zone with
>>> Time.timezone
-28800
The above value is a second value, which is the description of the current time zone and the 0 time zone difference, -28800=-8*3600, which is the East eight zone.

A few simple functions

def isostring2time (s):
‘‘‘
Convert a ISO format time to second
from:2006-04-12 16:46:40 to:23123123
Turn a time into a second
‘‘‘
Return Time.strptime (S, Isotimeformat)
def time2isostring (s):
‘‘‘
Convert second to a ISO format time
from:23123123 to:2006-04-12 16:46:40
Converts a given second into a defined format
‘‘‘
Return Time.strftime (Isotimeformat, Time.localtime (float (s)))
def dateplustime (d, T):
‘‘‘
D=2006-04-12 16:46:40
T=2 hours
return 2006-04-12 18:46:40
To calculate the date of the number of seconds between a date, Time2sec is another function that can handle, 3 days, 13 minutes, 10 hours, and so on, to write this again, and to combine regular expressions.
‘‘‘
Return time2isostring (Time.mktime (Isostring2time (d)) +time2sec (t))
def datemindate (D1, D2):
‘‘‘
Minus to ISO format date,return seconds
Calculate the number of seconds between 2 time differences
‘‘‘
D1=isostring2time (D1)
D2=isostring2time (D2)
return Time.mktime (D1)-time.mktime (D2)

From:http://www.juyimeng.com/python-common-time-function.html

Common time methods for Python

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.