time tuple

Get to know time tuple

The time module contains functions for getting the current time, manipulating times and dates, and reading dates from strings and formatting dates as strings.

Dates can be represented as either a real number which is the seconds since 1970, or a tuple containing nine integers.

The following table has detailed information for date tuples.

(2002, 1, 21, 12, 2, 56, 0, 21, 0)

represents January 21, 2002, at 12:02:56.

The Fields of Python Date Tuples

Index Field Value
0 Year For example, 2000, 2001, and so on
1 Month In the range 1-12
2 DayIn the range 1-31
3 Hour In the range 0-23
4 Minute In the range 0-59
5 Second In the range 0-61
6 Weekday In the range 0-6, where Monday is 0
7 Julian day In the range 1-366
8 Daylight Savings 0, 1, or -1

The range for seconds is 0-61. 61 is for leap seconds and double leap seconds. The Daylight Savings number is a Boolean value (true or false ).

Define your own class to store time and date


import time#   w  ww  . j  a v a 2s  .  c o m
class now:
    def __init__(self):
        self.t = time.time()
        self.storetime()
    def storetime(self):
        self.year, \
        self.month, \
        self.day, \
        self.hour, \
        self.minute, \
        self.second, \
        self.dow, \
        self.doy, \
        self.dst = time.localtime(self.t)
n = now()
print "The year is", n.year
print n

The code above generates the following result.





















Home »
  Python »
    Language Basics »




Python Basics
Operator
Statements
Function Definition
Class
Buildin Functions
Buildin Modules