python time module | python datetime tutorials | First Part

Namaste Friends,




                             Friends, This Is Our First Part of Python Date And Time Tutorials Series. And In This Post, I Going To Show You How To Use Python Time Module With Practical Examples. 


But First If You Are New Visitor Then Click Here For Introduction Part

or 

Check Our Blog Post Index. 



So, let's start with some practical usages examples.



times_part_1_(Times) slides
In [1]:
#!/usr/bin/python
#
# ============================================
#       PYTHON TIME MODULE TUTORIAL
# ============================================
#
# Author : SSB
#          surajsinghbisht054@gmail.com
#          https://bitforestinfo.blogspot.com
# 
#
# Here, I am Using
# 
# Operating System : Ubuntu 16.04 LTS
# Python Version   : 2.7.12
# Editor           : ipython notebook
#
#
In [2]:
#
# Import Module
#
import time 
In [3]:
#
# time.ctime([secs])
#           Convert a time expressed in seconds since 
#           the epoch to a string representing local time. 
#           If secs is not provided or None, the current time 
#           as returned by time() is used.
#
#
# Get Current Time In Seconds
# 
t  = time.ctime()
In [4]:
#
# Print Time In Writable Form
#
print t
Thu Mar 30 16:36:25 2017
In [5]:
#
# Get Current Time In Struct Format 
#
t = time.localtime()
#
# This is Called Struct Format
print t
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=16, tm_min=36, tm_sec=25, tm_wday=3, tm_yday=89, tm_isdst=0)
In [6]:
#
# Here, I am Showing How We Can Seperate
# Different Values
#
print " Year          =", t.tm_year
print " Day of Year   =", t.tm_yday
print " Weekday       =", t.tm_wday
print " Second        =", t.tm_sec
print " Hour          =", t.tm_hour
print " Date of Month =", t.tm_mday
print " Minute        =", t.tm_min
print " Month         =", t.tm_mon
 Year          = 2017
 Day of Year   = 89
 Weekday       = 3
 Second        = 25
 Hour          = 16
 Date of Month = 30
 Minute        = 36
 Month         = 3
In [7]:
#
# Get Current Time In 24 Character String Format.
#
print time.asctime()
#
# Wait/sleep For 2 seconds 
#
time.sleep(2)
#
#  Again, Print Current Time
#
print time.asctime()
Thu Mar 30 16:36:25 2017
Thu Mar 30 16:36:27 2017
In [8]:
#
# time.gmtime(...)
#                 Convert seconds in struct time
#    time.gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,
#                           tm_sec, tm_wday, tm_yday, tm_isdst)
#
#
# Get current time in struct again
#
t = time.gmtime()
print t
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=11, tm_min=6, tm_sec=28, tm_wday=3, tm_yday=89, tm_isdst=0)
In [9]:
#
# Print Struct Time 
#
print " Year          =", t.tm_year
print " Day of Year   =", t.tm_yday
print " Weekday       =", t.tm_wday
print " Second        =", t.tm_sec
print " Hour          =", t.tm_hour
print " Date of Month =", t.tm_mday
print " Minute        =", t.tm_min
print " Month         =", t.tm_mon
 Year          = 2017
 Day of Year   = 89
 Weekday       = 3
 Second        = 28
 Hour          = 11
 Date of Month = 30
 Minute        = 6
 Month         = 3
In [10]:
#
# time.timezone
#        The offset of the local (non-DST) timezone, 
#        in seconds west of UTC 
#        (negative in most of Western Europe, 
#               positive in the US, zero in the UK).
#
# Get Current Time Zone
#
print time.timezone
-19800
In [11]:
#
# time.tzname
#        the first is the name of the local non-DST timezone, 
#        the second is the name of the local DST timezone.
#
print time.tzname
('IST', 'IST')
In [12]:
#
# time.time()
#         Return the time in seconds since the epoch
#                         as a floating point number.
#
t1 = time.time()
#
# print time
#
print t1
#
# Adding 16 Seconds in time.
#
t2 = t1 + 16
1490871988.43
In [13]:
#
# Here, I am showing some valid mathematics function
# With Time.
#
# So, Let's See
#
# Substraction
#
print t2 - t1
#
# Addition
#
print t2 + t1
#
# Multiplication
#
print t2 / 2
#
16.0
2981743992.86
745436002.214
In [14]:
#
# time.strftime(format[, t])
#       With time.strftime we can get time in specified string
#       format as we want. We Only Need to pass format argument.
#
# Here, I am sharing a list of Directive  and their meanings.
# ============================================
# Directive = Meanings  + Starting From Here +
# ============================================
#
# %a  =  Locale’s abbreviated weekday name. 
# %A  =  Locale’s full weekday name.
# %b  =  Locale’s abbreviated month name.
# %B  =  Locale’s full month name.
# %c  =  Locale’s appropriate date and time representation. 
# %d  =  Day of the month as a decimal number [01,31].
# %H  =  Hour (24-hour clock) as a decimal number [00,23]. 
# %I  =  Hour (12-hour clock) as a decimal number [01,12]. 
# %j  =  Day of the year as a decimal number [001,366].
# %m  =  Month as a decimal number [01,12]. 
# %M  =  Minute as a decimal number [00,59].
# %p  =  Locale’s equivalent of either AM or PM.
# %S  =  Second as a decimal number [00,61].
# %U  =  Week number of the year (Sunday as the first day of the week) as 
#             a decimal number [00,53]. All days in a new year preceding 
#             the first Sunday are considered to be in week 0.
# %w  =  Weekday as a decimal number [0(Sunday),6].
# %W  =  Week number of the year (Monday as the first day of the week)
#             as a decimal number [00,53]. All days in a new year 
#             preceding the first Monday are considered to be in week 0.
# %x  =  Locale’s appropriate date representation.
# %X  =  Locale’s appropriate time representation.
# %y  =  Year without century as a decimal number [00,99].
# %Y  =  Year with century as a decimal number.  
# %Z  =  Time zone name (no characters if no time zone exists).
# %%  =  A literal '%' character.
#
# ============================================
#             + Ends Here +
# ============================================
#
# With the help of these directive, we can do many
# many function easily.
#
#
# let me show you some examples of these directives
#
In [15]:
# 
# Here, I am Showing You 9 Examples.
# Pay Attention on passing format arguments and directives.
#
print "[1]" ,time.strftime("%a", time.gmtime())
print "[2]" ,time.strftime("%A %d", time.gmtime())
print "[3]" ,time.strftime("%A %d %H:%M:%S", time.gmtime())
print "[4]" ,time.strftime("%a, %d %b %H:%M:%S", time.gmtime())
print "[5]" ,time.strftime("%a, %d %B %H:%M:%S", time.gmtime())
print "[6]" ,time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
print "[7]" ,time.strftime("%a, %d %b %Y %I:%M:%S %p", time.gmtime())
print "[8]" ,time.strftime("%a, %d %b %Y %H:%M:%S ", time.gmtime())
print "[9]" ,time.strftime("%a, %d %b %Y %I:%M:%S %p %Z", time.gmtime())
[1] Thu
[2] Thursday 30
[3] Thursday 30 11:06:28
[4] Thu, 30 Mar 11:06:28
[5] Thu, 30 March 11:06:28
[6] Thu, 30 Mar 2017 11:06:28
[7] Thu, 30 Mar 2017 11:06:28 AM
[8] Thu, 30 Mar 2017 11:06:28 
[9] Thu, 30 Mar 2017 11:06:28 AM IST
In [16]:
#
# As, I Already Describe you, what is struct time.
# but if you forget. then, don't worry.
# here, i am showing you again.
t = time.gmtime()
print t
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=11, tm_min=6, tm_sec=28, tm_wday=3, tm_yday=89, tm_isdst=0)
In [17]:
#
# time.strptime("time_string","time_format")
#          Parse a string representing a time according to a 
#          format. The return value is a struct_time
#
# As You Can See in below example.        
#  time_string = "29 Mar 1889 11:51:37"
#  time_format = "%d %b %Y %H:%M:%S"
#
t = time.strptime("29 Mar 1889 11:51:37","%d %b %Y %H:%M:%S")
In [18]:
#
# Friends, as you can see in this example. with this method,
# we got extact weekday of "29 Mar 1889 11:51:37" date.
#
print " Year          =", t.tm_year
print " Day of Year   =", t.tm_yday
print " Weekday       =", t.tm_wday
print " Second        =", t.tm_sec
print " Hour          =", t.tm_hour
print " Date of Month =", t.tm_mday
print " Minute        =", t.tm_min
print " Month         =", t.tm_mon
 Year          = 1889
 Day of Year   = 88
 Weekday       = 4
 Second        = 37
 Hour          = 11
 Date of Month = 29
 Minute        = 51
 Month         = 3
Python DateTime tutorials Part One Ends Here.
For Next Part Click Here.



Thanks For Reading,

For More Updates And Tutorials,
Subscribe our blog,
like, comment and share.


Have a Wonderful day
SSB :) 

Share this

Related Posts

Previous
Next Post »