Skip to main content

Python Date and Time

Python Date and Time
There are various ways Python supplies date and time feature to add in program. Python’s Time and calendar module helps in tracking date and time. Also the ‘datetime’ provide classes for controlling date and time by both simple and complex ways.
There are two different date and time objects. These are:
  • naive
  • aware
The differences between ‘naive’ and ‘aware’ objects are:
  1. The ‘aware’ object holds the details about time-zone and daylight saving information whereas naĂŻve objects do not have that feature.
  2. Naive objects are easy to understand and to work with.

Defining Tick

As we all can make an idea that time intervals have to be represented in floating point number. The floating-point numbers in units of seconds are signified by Tick in python. Particular instants of time are represented in seconds since 12:00 am, 1st of January, of the year 1990. A popular module name ‘time’ is available which provides functions that let’s programmer work with time and made conversion between time-representation possible.
The pre-defined function time.time() is used to return the current time of the system.
The daytime module exports the constants listed below:
  • MINYEAR
  • MAXYEAR
Two programs are shown showing the use of two different modules:
Example:
#!/usr/bin/python

import time;

ticktock
= time.time();

print("Number of ticks:", ticktock)
Output:
Number of ticks: 1465927104.951356
Another program showing datetime module:
Example:
#!/usr/bin/python

from datetime import datetime

presentime
= datetime.now()

print(" NOW THE TIME IS:", presentime)

print("Todays Date is:", presentime.strftime('%Y-%m-%d') )

print("Year is:", presentime.year)

print("MOnth is:", presentime.month)

print("Day is:", presentime.day)
Output:
NOW THE TIME IS: 2016-06-14 18:01:25.831269
Todays Date is: 2016-06-14
Year is: 2016
MOnth is: 6
Day is: 14

Timetuple In Python

Python usually handles time’s function as a tuple with nine numbers starting from zero (0) to eight (8).
0Four digit number (year)2016
1Months1 – 12
2Day1 – 31
3Hour0 – 23
4Minute0 – 59
5Second0 – 60
6Days of the week0 – 6 (where, Zero designates Monday)
7Days of a year1 – 366
8Daylight saving-1, 0, 1, -1

Timedelta Object

It is used to represent duration, i.e.  the difference between two dates and or times.  For invoking this, the syntax is:
Syntax:
datetime.timedelta([days, [,seconds [,microseconds [,milliseconds [, minutes [,hours [,weeks]]]]]])
In the above scenario, all arguments are optional and their default value is zero (0).

Program To Get Current Time

Example:
#!/usr/bin/python

import time;

curtime
= time.localtime(time.time())

print("Current Time is:", curtime)
Output:
Current Time is: time.struct_time(tm_year=2016, tm_mon=6, tm_mday=14, tm_hour=0, tm_min=4, tm_sec=49, tm_wday=1, tm_yday=166, tm_isdst=0)

Calendar Of A Month

This facility is provided by “calendar” module which provides a wide range of methods to deal with monthly calendars. A simple program showing how to use:
Example:
#!/usr/bin/python

import calendar

calndr
= calendar.month(2016, 6)

print("The calendar for the month June of Year 2016 is:")

print (calndr)
Output:

The calendar for the month June of Year 2016 is:
June 2016
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...