0% found this document useful (0 votes)
2 views

Date Abstract Data Type

The Date Abstract Data Type (ADT) defines a model for managing calendar dates, including properties like year, month, and day, along with operations such as setting, getting, and comparing dates. It provides methods to check for leap years and to add or subtract days from a date. The document includes example code demonstrating the implementation of the Date ADT in Python.

Uploaded by

Tejas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Date Abstract Data Type

The Date Abstract Data Type (ADT) defines a model for managing calendar dates, including properties like year, month, and day, along with operations such as setting, getting, and comparing dates. It provides methods to check for leap years and to add or subtract days from a date. The document includes example code demonstrating the implementation of the Date ADT in Python.

Uploaded by

Tejas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Date Abstract Data Type

A Date Abstract Data Type (ADT) is a conceptual model that defines the
properties and operations for managing dates, abstracting away the
implementation details. The Date ADT encapsulates the idea of a calendar date,
typically comprising year, month, and day components. It provides a set of
operations that can be performed on these date values.

Properties of Date ADT

● Year: A four-digit integer representing the year.


● Month: An integer between 1 and 12 representing the month.
● Day: An integer representing the day of the month, with a valid range
depending on the month and year (considering leap years).

Operations of Date ADT

1. Set Date: Initialize or modify the date.


2. Get Date: Retrieve the current date.
3. Check Leap Year: Determine if the current year is a leap year.
4. Add Days: Add a specified number of days to the date.
5. Subtract Days: Subtract a specified number of days from the date.
6. Compare Dates: Compare two dates to determine their chronological
order.
7. Format Date: Return the date in a specified string format.

Code:

Basic Code:

from datetime import date

# You can create a date object containing

# the current date

# by using a classmethod named today()


current = date.today()

# print current year, month, and year individually

print("Current Day is :", current.day)

print("Current Month is :", current.month)

print("Current Year is :", current.year)

# strftime() creates string representing date in

# various formats

print("\n")

print("Let's print date, month and year in different-different ways")

format1 = current.strftime("%m/%d/%y")

# prints in month/date/year format

print("format1 =", format1)

format2 = current.strftime("%b-%d-%Y")

# prints in month(abbreviation)-date-year format

print("format2 =", format2)

format3 = current.strftime("%d/%m/%Y")

# prints in date/month/year format

print("format3 =", format3)

format4 = current.strftime("%B %d, %Y")

# prints in month(words) date, year format

print("format4 =", format4)


O/P

Current Day is : 23

Current Month is : 3

Current Year is : 2021

Let's print date, month and year in different-different ways

format1 = 03/23/21

format2 = Mar-23-2021

format3 = 23/03/2021

format4 = March 23, 2021

Next Code
class DateADT:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day

def __repr__(self):
return
f"{self.year:04d}-{self.month:02d}-{self.day:02d}"

def set_date(self, year, month, day):


self.year = year
self.month = month
self.day = day
def get_date(self):
return (self.year, self.month, self.day)

def is_leap_year(self):
if (self.year % 4 == 0 and self.year % 100 != 0) or
(self.year % 400 == 0):
return True
return False

def add_days(self, days):


days_in_month = [31, 29 if self.is_leap_year() else 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

self.day += days
while self.day > days_in_month[self.month - 1]:
self.day -= days_in_month[self.month - 1]
self.month += 1
if self.month > 12:
self.month = 1
self.year += 1

# Example usage:
date = DateADT(2024, 6, 29)
print(date)
date.add_days(5)
print(date)
print(date.is_leap_year())
O/P
Output: 2024-06-29
Output: 2024-07-04
Output: True

You might also like