DevScript Saga

Welcome to DevScript Saga!

A little Helping Hand for Devs

Today we will learn Python date, time, calender.

In Python, date, time and datetime classes provides a number of function to deal with dates, times and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Whenever you manipulate dates or time, you need to import datetime function.

What is TimeTuple?

Many of Python’s time functions handle time as a tuple of 9 numbers, as shown below −

IndexFieldValues
04-digit year2008
1Month1 to 12
2Day1 to 31
3Hour0 to 23
4Minute0 to 59
5Second0 to 61 (60 or 61 are leap-seconds)
6Day of Week0 to 6 (0 is Monday)
7Day of year1 to 366 (Julian day)
8Daylight savings-1, 0, 1, -1 means library determines DST

The above tuple is equivalent to struct_time structure. This structure has following attributes −

IndexAttributesValues
0tm_year2008
1tm_mon1 to 12
2tm_mday1 to 31
3tm_hour0 to 23
4tm_min0 to 59
5tm_sec0 to 61 (60 or 61 are leap-seconds)
6tm_wday0 to 6 (0 is Monday)
7tm_yday1 to 366 (Julian day)
8tm_isdst-1, 0, 1, -1 means library determines DST

For manipulating dates and times in both simple and complex ways datetime module supplies different classes or categories like :

  • date – Manipulate just date ( Month, day, year)
  • time – Time independent of the day (Hour, minute, second, microsecond)
  • datetime – Combination of time and date (Month, day, year, hour, second, microsecond)
  • timedelta— A duration of time used for manipulating dates
  • tzinfo— An abstract class for dealing with timezones

Using datetime objects

  • Importing datetime objects before executing the code is mandatory
  • Using date.today function for printing individual date/month/year as well as indexing the day
  • Using date.time object to get time in hours, minutes, seconds and milliseconds

Formatting Time-Out with “str f time function”

  • Use “str f time function” to change the format of the year
  • Print day, date, month and year separately,
  • Call out time for any format 12 hrs or 24 hrs

Timedelta Objects

  • With timedelta objects, you can estimate the time for both future and the past
  • Calculate the total days left for the special day(birthday) from the current time
  • Calculate the total days passed for special day(birthday) from the current time

Exception Handling in Python:

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program’s instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises an exception. An exception is a Python object that represents an error.

When a Python script raises an exception, it must either handle the exception immediately otherwise it terminates and quits.

Handling an exception:

If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.

Syntax

Here is simple syntax of try….except…else blocks −

try:
   You do your operations here;
   ......................
except ExceptionI:
   If there is ExceptionI, then execute this block.
except ExceptionII:
   If there is ExceptionII, then execute this block.
   ......................
else:
   If there is no exception then execute this block. 
  • A single try statement can have multiple except statements. This is useful when the try block contains statements that may throw different types of exceptions.
  • You can also provide a generic except clause, which handles any exception.
  • After the except clause(s), you can include an else-clause. The code in the else-block executes if the code in the try: block does not raise an exception.
  • The else-block is a good place for code that does not need the try: block’s protection.
Try below assignment for practice:
1.Python program to print the calendar of a given month and year.
2.Program to calculate number of days between two dates
3.Program for Check if all digits of a number divide it
4.Program to find the most occurring character and its count
Click here for solution...

Leave a comment