<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Python%3A_time</id>
	<title>Python: time - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Python%3A_time"/>
	<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=Python:_time&amp;action=history"/>
	<updated>2026-04-16T04:14:24Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.4</generator>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=Python:_time&amp;diff=52355&amp;oldid=prev</id>
		<title>Onnowpurbo at 00:31, 31 October 2018</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=Python:_time&amp;diff=52355&amp;oldid=prev"/>
		<updated>2018-10-31T00:31:09Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://onnocenter.or.id/wiki/index.php?title=Python:_time&amp;amp;diff=52355&amp;amp;oldid=46712&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=Python:_time&amp;diff=46712&amp;oldid=prev</id>
		<title>Onnowpurbo: Created page with &quot; The time module  This module provides a number of functions to deal with dates and the time within a day. It’s a thin layer on top of the C runtime library.  A given date a...&quot;</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=Python:_time&amp;diff=46712&amp;oldid=prev"/>
		<updated>2017-01-28T03:59:22Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; The time module  This module provides a number of functions to deal with dates and the time within a day. It’s a thin layer on top of the C runtime library.  A given date a...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
The time module&lt;br /&gt;
&lt;br /&gt;
This module provides a number of functions to deal with dates and the time within a day. It’s a thin layer on top of the C runtime library.&lt;br /&gt;
&lt;br /&gt;
A given date and time can either be represented as a floating point value (the number of seconds since a reference date, usually January 1st, 1970), or as a time tuple.&lt;br /&gt;
Getting the current time&lt;br /&gt;
Example: Using the time module to get the current time&lt;br /&gt;
&lt;br /&gt;
# File: time-example-1.py&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
now = time.time()&lt;br /&gt;
&lt;br /&gt;
print now, &amp;quot;seconds since&amp;quot;, time.gmtime(0)[:6]&lt;br /&gt;
print&lt;br /&gt;
print &amp;quot;or in other words:&amp;quot;&lt;br /&gt;
print &amp;quot;- local time:&amp;quot;, time.localtime(now)&lt;br /&gt;
print &amp;quot;- utc:&amp;quot;, time.gmtime(now)&lt;br /&gt;
&lt;br /&gt;
937758359.77 seconds since (1970, 1, 1, 0, 0, 0)&lt;br /&gt;
&lt;br /&gt;
or in other words:&lt;br /&gt;
- local time: (1999, 9, 19, 18, 25, 59, 6, 262, 1)&lt;br /&gt;
- utc: (1999, 9, 19, 16, 25, 59, 6, 262, 0)&lt;br /&gt;
&lt;br /&gt;
The tuple returned by localtime and gmtime contains (year, month, day, hour, minute, second, day of week, day of year, daylight savings flag), where the year number is four digits, the day of week begins with 0 for Monday, and January 1st is day number 1.&lt;br /&gt;
Converting time values to strings&lt;br /&gt;
&lt;br /&gt;
You can of course use standard string formatting operators to convert a time tuple to a string, but the time module also provides a number of standard conversion functions:&lt;br /&gt;
 &lt;br /&gt;
Example: Using the time module to format dates and times&lt;br /&gt;
&lt;br /&gt;
# File: time-example-2.py&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
now = time.localtime(time.time())&lt;br /&gt;
&lt;br /&gt;
print time.asctime(now)&lt;br /&gt;
print time.strftime(&amp;quot;%y/%m/%d %H:%M&amp;quot;, now)&lt;br /&gt;
print time.strftime(&amp;quot;%a %b %d&amp;quot;, now)&lt;br /&gt;
print time.strftime(&amp;quot;%c&amp;quot;, now)&lt;br /&gt;
print time.strftime(&amp;quot;%I %p&amp;quot;, now)&lt;br /&gt;
print time.strftime(&amp;quot;%Y-%m-%d %H:%M:%S %Z&amp;quot;, now)&lt;br /&gt;
&lt;br /&gt;
# do it by hand...&lt;br /&gt;
year, month, day, hour, minute, second, weekday, yearday, daylight = now&lt;br /&gt;
print &amp;quot;%04d-%02d-%02d&amp;quot; % (year, month, day)&lt;br /&gt;
print &amp;quot;%02d:%02d:%02d&amp;quot; % (hour, minute, second)&lt;br /&gt;
print (&amp;quot;MON&amp;quot;, &amp;quot;TUE&amp;quot;, &amp;quot;WED&amp;quot;, &amp;quot;THU&amp;quot;, &amp;quot;FRI&amp;quot;, &amp;quot;SAT&amp;quot;, &amp;quot;SUN&amp;quot;)[weekday], yearday&lt;br /&gt;
&lt;br /&gt;
Sun Oct 10 21:39:24 1999&lt;br /&gt;
99/10/10 21:39&lt;br /&gt;
Sun Oct 10&lt;br /&gt;
Sun Oct 10 21:39:24 1999&lt;br /&gt;
09 PM&lt;br /&gt;
1999-10-10 21:39:24 CEST&lt;br /&gt;
1999-10-10&lt;br /&gt;
21:39:24&lt;br /&gt;
SUN 283&lt;br /&gt;
&lt;br /&gt;
Converting strings to time values&lt;br /&gt;
&lt;br /&gt;
On some platforms, the time module contains a strptime function, which is pretty much the opposite of strftime. Given a string and a pattern, it returns the corresponding time tuple:&lt;br /&gt;
Example: Using the time.strptime function to parse dates and times&lt;br /&gt;
&lt;br /&gt;
# File: time-example-6.py&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
# make sure we have a strptime function!&lt;br /&gt;
try:&lt;br /&gt;
    strptime = time.strptime&lt;br /&gt;
except AttributeError:&lt;br /&gt;
    from strptime import strptime&lt;br /&gt;
&lt;br /&gt;
print strptime(&amp;quot;31 Nov 00&amp;quot;, &amp;quot;%d %b %y&amp;quot;)&lt;br /&gt;
print strptime(&amp;quot;1 Jan 70 1:30pm&amp;quot;, &amp;quot;%d %b %y %I:%M%p&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The time.strptime function is currently only made available by Python if it’s provided by the platform’s C libraries. For platforms that don’t have a standard implementation (this includes Windows), here’s a partial replacement:&lt;br /&gt;
 &lt;br /&gt;
Example: A strptime implementation&lt;br /&gt;
&lt;br /&gt;
# File: strptime.py&lt;br /&gt;
&lt;br /&gt;
import re&lt;br /&gt;
import string&lt;br /&gt;
&lt;br /&gt;
MONTHS = [&amp;quot;Jan&amp;quot;, &amp;quot;Feb&amp;quot;, &amp;quot;Mar&amp;quot;, &amp;quot;Apr&amp;quot;, &amp;quot;May&amp;quot;, &amp;quot;Jun&amp;quot;, &amp;quot;Jul&amp;quot;, &amp;quot;Aug&amp;quot;,&lt;br /&gt;
          &amp;quot;Sep&amp;quot;, &amp;quot;Oct&amp;quot;, &amp;quot;Nov&amp;quot;, &amp;quot;Dec&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
SPEC = {&lt;br /&gt;
    # map formatting code to a regular expression fragment&lt;br /&gt;
    &amp;quot;%a&amp;quot;: &amp;quot;(?P&amp;lt;weekday&amp;gt;[a-z]+)&amp;quot;,&lt;br /&gt;
    &amp;quot;%A&amp;quot;: &amp;quot;(?P&amp;lt;weekday&amp;gt;[a-z]+)&amp;quot;,&lt;br /&gt;
    &amp;quot;%b&amp;quot;: &amp;quot;(?P&amp;lt;month&amp;gt;[a-z]+)&amp;quot;,&lt;br /&gt;
    &amp;quot;%B&amp;quot;: &amp;quot;(?P&amp;lt;month&amp;gt;[a-z]+)&amp;quot;,&lt;br /&gt;
    &amp;quot;%C&amp;quot;: &amp;quot;(?P&amp;lt;century&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%d&amp;quot;: &amp;quot;(?P&amp;lt;day&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%D&amp;quot;: &amp;quot;(?P&amp;lt;month&amp;gt;\d\d?)/(?P&amp;lt;day&amp;gt;\d\d?)/(?P&amp;lt;year&amp;gt;\d\d)&amp;quot;,&lt;br /&gt;
    &amp;quot;%e&amp;quot;: &amp;quot;(?P&amp;lt;day&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%h&amp;quot;: &amp;quot;(?P&amp;lt;month&amp;gt;[a-z]+)&amp;quot;,&lt;br /&gt;
    &amp;quot;%H&amp;quot;: &amp;quot;(?P&amp;lt;hour&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%I&amp;quot;: &amp;quot;(?P&amp;lt;hour12&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%j&amp;quot;: &amp;quot;(?P&amp;lt;yearday&amp;gt;\d\d?\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%m&amp;quot;: &amp;quot;(?P&amp;lt;month&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%M&amp;quot;: &amp;quot;(?P&amp;lt;minute&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%p&amp;quot;: &amp;quot;(?P&amp;lt;ampm12&amp;gt;am|pm)&amp;quot;,&lt;br /&gt;
    &amp;quot;%R&amp;quot;: &amp;quot;(?P&amp;lt;hour&amp;gt;\d\d?):(?P&amp;lt;minute&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%S&amp;quot;: &amp;quot;(?P&amp;lt;second&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%T&amp;quot;: &amp;quot;(?P&amp;lt;hour&amp;gt;\d\d?):(?P&amp;lt;minute&amp;gt;\d\d?):(?P&amp;lt;second&amp;gt;\d\d?)&amp;quot;,&lt;br /&gt;
    &amp;quot;%U&amp;quot;: &amp;quot;(?P&amp;lt;week&amp;gt;\d\d)&amp;quot;,&lt;br /&gt;
    &amp;quot;%w&amp;quot;: &amp;quot;(?P&amp;lt;weekday&amp;gt;\d)&amp;quot;,&lt;br /&gt;
    &amp;quot;%W&amp;quot;: &amp;quot;(?P&amp;lt;weekday&amp;gt;\d\d)&amp;quot;,&lt;br /&gt;
    &amp;quot;%y&amp;quot;: &amp;quot;(?P&amp;lt;year&amp;gt;\d\d)&amp;quot;,&lt;br /&gt;
    &amp;quot;%Y&amp;quot;: &amp;quot;(?P&amp;lt;year&amp;gt;\d\d\d\d)&amp;quot;,&lt;br /&gt;
    &amp;quot;%%&amp;quot;: &amp;quot;%&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class TimeParser:&lt;br /&gt;
    def __init__(self, format):&lt;br /&gt;
        # convert strptime format string to regular expression&lt;br /&gt;
        format = string.join(re.split(&amp;quot;(?:\s|%t|%n)+&amp;quot;, format))&lt;br /&gt;
        pattern = []&lt;br /&gt;
        try:&lt;br /&gt;
            for spec in re.findall(&amp;quot;%\w|%%|.&amp;quot;, format):&lt;br /&gt;
                if spec[0] == &amp;quot;%&amp;quot;:&lt;br /&gt;
                    spec = SPEC[spec]&lt;br /&gt;
                pattern.append(spec)&lt;br /&gt;
        except KeyError:&lt;br /&gt;
            raise ValueError, &amp;quot;unknown specificer: %s&amp;quot; % spec&lt;br /&gt;
        self.pattern = re.compile(&amp;quot;(?i)&amp;quot; + string.join(pattern, &amp;quot;&amp;quot;))&lt;br /&gt;
    def match(self, daytime):&lt;br /&gt;
        # match time string&lt;br /&gt;
        match = self.pattern.match(daytime)&lt;br /&gt;
        if not match:&lt;br /&gt;
            raise ValueError, &amp;quot;format mismatch&amp;quot;&lt;br /&gt;
        get = match.groupdict().get&lt;br /&gt;
        tm = [0] * 9&lt;br /&gt;
        # extract date elements&lt;br /&gt;
        y = get(&amp;quot;year&amp;quot;)&lt;br /&gt;
        if y:&lt;br /&gt;
            y = int(y)&lt;br /&gt;
            if y &amp;lt; 68:&lt;br /&gt;
                y = 2000 + y&lt;br /&gt;
            elif y &amp;lt; 100:&lt;br /&gt;
                y = 1900 + y&lt;br /&gt;
            tm[0] = y&lt;br /&gt;
        m = get(&amp;quot;month&amp;quot;)&lt;br /&gt;
        if m:&lt;br /&gt;
            if m in MONTHS:&lt;br /&gt;
                m = MONTHS.index(m) + 1&lt;br /&gt;
            tm[1] = int(m)&lt;br /&gt;
        d = get(&amp;quot;day&amp;quot;)&lt;br /&gt;
        if d: tm[2] = int(d)&lt;br /&gt;
        # extract time elements&lt;br /&gt;
        h = get(&amp;quot;hour&amp;quot;)&lt;br /&gt;
        if h:&lt;br /&gt;
            tm[3] = int(h)&lt;br /&gt;
        else:&lt;br /&gt;
            h = get(&amp;quot;hour12&amp;quot;)&lt;br /&gt;
            if h:&lt;br /&gt;
                h = int(h)&lt;br /&gt;
                if string.lower(get(&amp;quot;ampm12&amp;quot;, &amp;quot;&amp;quot;)) == &amp;quot;pm&amp;quot;:&lt;br /&gt;
                    h = h + 12&lt;br /&gt;
                tm[3] = h&lt;br /&gt;
        m = get(&amp;quot;minute&amp;quot;)&lt;br /&gt;
        if m: tm[4] = int(m)&lt;br /&gt;
        s = get(&amp;quot;second&amp;quot;)&lt;br /&gt;
        if s: tm[5] = int(s)&lt;br /&gt;
        # ignore weekday/yearday for now&lt;br /&gt;
        return tuple(tm)&lt;br /&gt;
&lt;br /&gt;
def strptime(string, format=&amp;quot;%a %b %d %H:%M:%S %Y&amp;quot;):&lt;br /&gt;
    return TimeParser(format).match(string)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    # try it out&lt;br /&gt;
    import time&lt;br /&gt;
    print strptime(&amp;quot;2000-12-20 01:02:03&amp;quot;, &amp;quot;%Y-%m-%d %H:%M:%S&amp;quot;)&lt;br /&gt;
    print strptime(time.ctime(time.time()))&lt;br /&gt;
&lt;br /&gt;
(2000, 12, 20, 1, 2, 3, 0, 0, 0)&lt;br /&gt;
(2000, 11, 15, 12, 30, 45, 0, 0, 0)&lt;br /&gt;
&lt;br /&gt;
Converting time values&lt;br /&gt;
&lt;br /&gt;
Converting a time tuple back to a time value is pretty easy, at least as long as we’re talking about local time. Just pass the time tuple to the mktime function:&lt;br /&gt;
Example: Using the time module to convert a local time tuple to a time integer&lt;br /&gt;
&lt;br /&gt;
# File: time-example-3.py&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
t0 = time.time()&lt;br /&gt;
tm = time.localtime(t0)&lt;br /&gt;
&lt;br /&gt;
print tm&lt;br /&gt;
&lt;br /&gt;
print t0&lt;br /&gt;
print time.mktime(tm)&lt;br /&gt;
&lt;br /&gt;
(1999, 9, 9, 0, 11, 8, 3, 252, 1)&lt;br /&gt;
936828668.16&lt;br /&gt;
936828668.0&lt;br /&gt;
&lt;br /&gt;
Unfortunately, there’s no function in the 1.5.2 standard library that converts UTC time tuples back to time values (neither in Python nor in the underlying C libraries). The following example provides a Python implementation of such a function, called timegm:&lt;br /&gt;
 &lt;br /&gt;
Example: Converting a UTC time tuple to a time integer&lt;br /&gt;
&lt;br /&gt;
# File: time-example-4.py&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
def _d(y, m, d, days=(0,31,59,90,120,151,181,212,243,273,304,334,365)):&lt;br /&gt;
    # map a date to the number of days from a reference point&lt;br /&gt;
    return (((y - 1901)*1461)/4 + days[m-1] + d +&lt;br /&gt;
        ((m &amp;gt; 2 and not y % 4 and (y % 100 or not y % 400)) and 1))&lt;br /&gt;
&lt;br /&gt;
def timegm(tm, epoch=_d(1970,1,1)):&lt;br /&gt;
    year, month, day, h, m, s = tm[:6]&lt;br /&gt;
    assert year &amp;gt;= 1970&lt;br /&gt;
    assert 1 &amp;lt;= month &amp;lt;= 12&lt;br /&gt;
    return (_d(year, month, day) - epoch)*86400 + h*3600 + m*60 + s&lt;br /&gt;
&lt;br /&gt;
t0 = time.time()&lt;br /&gt;
tm = time.gmtime(t0)&lt;br /&gt;
&lt;br /&gt;
print tm&lt;br /&gt;
&lt;br /&gt;
print t0&lt;br /&gt;
print timegm(tm)&lt;br /&gt;
&lt;br /&gt;
(1999, 9, 8, 22, 12, 12, 2, 251, 0)&lt;br /&gt;
936828732.48&lt;br /&gt;
936828732&lt;br /&gt;
&lt;br /&gt;
In 1.6 and later, a similar function is available in the calendar module, as calendar.timegm.&lt;br /&gt;
Timing things&lt;br /&gt;
&lt;br /&gt;
The time module can be used to time the execution of a Python program. You can measure either “wall time” (real world time), or “process time” (the amount of CPU time the process has consumed, this far).&lt;br /&gt;
Example: Using the time module to benchmark an algorithm&lt;br /&gt;
&lt;br /&gt;
# File: time-example-5.py&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
def procedure():&lt;br /&gt;
    time.sleep(2.5)&lt;br /&gt;
&lt;br /&gt;
# measure process time&lt;br /&gt;
t0 = time.clock()&lt;br /&gt;
procedure()&lt;br /&gt;
print time.clock() - t0, &amp;quot;seconds process time&amp;quot;&lt;br /&gt;
&lt;br /&gt;
# measure wall time&lt;br /&gt;
t0 = time.time()&lt;br /&gt;
procedure()&lt;br /&gt;
print time.time() - t0, &amp;quot;seconds wall time&amp;quot;&lt;br /&gt;
&lt;br /&gt;
0.0 seconds process time&lt;br /&gt;
2.50903499126 seconds wall time&lt;br /&gt;
&lt;br /&gt;
Not all systems can measure the true process time. On such systems (including Windows), clock usually measures the wall time since the program was started.&lt;br /&gt;
&lt;br /&gt;
Also see the timing module, which measures the wall time between two events.&lt;br /&gt;
&lt;br /&gt;
The process time has limited precision. On many systems, it wraps around after just over 30 minutes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Referensi==&lt;br /&gt;
&lt;br /&gt;
* http://effbot.org/librarybook/time.htm&lt;/div&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
</feed>