Detailed description of Python timing-related operations

Source: Internet
Author: User
Tags month name sleep function timedelta
This article mainly describes the Python timing-related operations, involving the use of Time,datetime module skills, including time stamping, timing, date format and other operations, the need for friends can refer to the following

The examples in this article describe Python timing-related operations. Share to everyone for your reference, as follows:

Content directory:

1. Time stamp
2. Current time
3. Time difference
4. Python format symbols in time date
5. Example

One, time stamp

The timestamp is the total number of seconds from January 1, 1970 (08:00:00 GMT) to the current time. It is also known as the Unix timestamp (Unix Timestamp), which is ubiquitous in the UNIX, C world; The common pattern is floating-point numbers, followed by milliseconds. Subtracting two timestamps is the time interval (in seconds).

Cases:

Import timetime1 = Time.time () time.sleep () time2 = Time.time () print time2-time1

where Time.sleep () is the sleep function, in seconds.

Second, the current time

>>> Import Datetime,time>>> now = Time.strftime ("%y-%m-%d%h:%m:%s") >>> print now2016-04-30 17:02:26>>> now = Datetime.datetime.now () >>> Print now

Three, time difference

#1 yesterday 00:00 to yesterday 23:59

>>> import datetime>>> yestoday = Datetime.datetime.now ()-Datetime.timedelta (Days=1) >>> T1 = "%s-00-00-00"% yestoday.strftime ("%y-%m-%d") >>> t2 = "%s-23-59-59"% yestoday.strftime ("%y-%m-%d") > >> print ' t1 ', t1t1 2016-04-29-00-00-00>>> print ' T2 ', t2t2 2016-04-29-23-59-59

#2 10 hours now.

>>> D1 = Datetime.datetime.now () >>> d3 = d1 + Datetime.timedelta (hours=10) >>> d3.ctime () ' Sun May 1 03:09:58 2

#3 the number of seconds, the subtle number (note is the seconds, the subtle part, not the equivalent conversion)

>>> import datetime>>> starttime = Datetime.datetime.now () >>> endtime = Datetime.datetime.now () >>> starttime = Datetime.datetime.now () >>> endtime = Datetime.datetime.now ( ) >>> print endtime-starttime0:00:07.390988>>> print (endtime-starttime) .seconds7>>> Print (endtime-starttime). microseconds390988

Timestamp of the file

>>> Import os>>> Statinfo=os.stat (r "C:/1.txt") >>> statinfo (33206, 0L, 0, 0, 0, 0, 29L, 1201865 413, 1201867904, 1201865413)

Note: The following three entries in the Os.stat return value statinfo are file St_atime (access time), St_mtime (modification time), St_ctime (creation time), for example, get file modification time:

>>> statinfo.st_mtime1201865413.8952832

Note: This time is a Linux timestamp that can be converted into an easy-to-understand format:

>>> Import time>>> Time.localtime (Statinfo.st_ctime) (2008, 2, 1, 19, 30, 13, 4, 32, 0)

Note: February 1, 2008 19:30 13 seconds (2008-2-1 19:30:13)

Iv. formatting symbols for time and date in Python

%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
One day in%d months (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12)
%M minutes (00=59)
%s seconds (00-59)
%a Local Simplified Week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
%j Day of the Year (001-366)
%p the equivalent of a local a.m. or p.m.
%u weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%W Week of the Year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%Z the name of the current time zone
Percent% of the number itself

V. Examples

#! Coding:utf-8 "' Date-related operation ' from datetime import datetimefrom datetime import timedeltaimport calendardate_fmt = '%y-%m- %d ' datetime_fmt = '%y-%m-%d%h:%m:%s ' date_us_fmt = '%d/%m/%y ' ' "" format commonly used several parameters Y:1999y:99m:mouth 12m:minute 00-59s : Secondd:dayH:hour "def datetostr (date):" "Format the datetime type in the format you want," "Return Datetime.strftime (date, datetime_ FMT) def strtodate (strdate): "" turns str into date used to do some operations "' return Datetime.strptime (Strdate, DATETIME_FMT) def timeelement () : "' Gets the individual elements of a time object ' now = Datetime.today () print ' Year:%s Month:%s Day:%s '% (Now.year, Now.month, Now.day) prin T ' Hour:%s minute:%s Second:%s '% (Now.hour, Now.minute, Now.second) print ' weekday:%s '% (Now.weekday () +1) #一周是从0开始的d EF Timeadd (): "" "Time of the addition and subtraction of the day before the Operation Datetime.timedelta ([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[ , Weeks]]] []]) The parameter can be positive or negative, the resulting object can be added or minus multiplied by the number and the absolute value ' Atime = Timedelta (days=-1) now = Datetime.strptime (' 2001-01- 11:01:02 ', datetime_fmt) Print now + atime print now-abs (atime) print now-abs (atime) *31def lastfirday (): Today = Datetime.today () Targ Etday = Calendar. FRIDAY ThisDay = Today.weekday () de = (thisday-targetday)% 7 res = Today-timedelta (days=de) Print Resdef test (): Print Datetostr (Datetime.today ()) Print strtodate (' 2013-01-31 12:00:01 ') timeelement () Timeadd () Lastfirday () if n ame== ' main ': Test ()

Results

Connected to Pydev Debugger (build 141.1899) 2016-05-18 10:40:262013-01-31 12:00:01year:2016 month:5 day:18hour:10 minu te:41 second:13weekday:32001-01-29 11:01:022001-01-29 11:01:022000-12-30 11:01:022016-05-13 10:41:37.001000
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.