Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: date_utility.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script provides functionality to output the current date or calculate future dates.
- It allows the user to choose between single-day, 7-day week, or full month increments and displays the results in the terminal.
- Requirements:
- - Python 3.x
- - calendar module
- Functions:
- - print_date(timestamp): Formats and prints the date corresponding to the provided timestamp.
- - year_month_day_weekday(timestamp): Returns year, month, day, and weekday for a given timestamp.
- - monthrange(timestamp): Returns the first and last day of the month for a given timestamp.
- Usage:
- Run the script and follow the on-screen prompts to select an option:
- 1. Print today's date
- 2. Print the full 7-day week
- 3. Print the full month
- Additional Notes:
- - Option 1 displays the current date only.
- - Option 2 displays the dates for the next 7 days, starting from today.
- - Option 3 displays all the dates in the current month.
- """
- import sys
- import time
- import calendar
- def print_date(timestamp):
- """
- Print the formatted date.
- """
- year, month, day, weekday = year_month_day_weekday(timestamp)
- print(f"\t{year}-{month:02d}-{day:02d} ({weekday})")
- def year_month_day_weekday(timestamp):
- """
- Return year, month, day, and weekday for a given timestamp.
- """
- struct_time = time.localtime(timestamp)
- year = struct_time.tm_year
- month = struct_time.tm_mon
- day = struct_time.tm_mday
- weekday = (
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday",
- "Sunday",
- )[struct_time.tm_wday]
- return year, month, day, weekday
- def main():
- """
- Main function to ouput the date as a single day, 7-day week or full month.
- """
- print("Select an option:\n")
- print("1. Print today's date")
- print("2. Print the full 7-day week")
- print("3. Print the full month")
- choice = input("\nEnter your choice (1-3): ")
- if choice == "1":
- print("\nToday's Date Is:\n")
- print_date(time.time())
- elif choice == "2":
- print("\nThe 7-Day Week:\n")
- for i in range(7):
- print_date(time.time() + i * 24 * 60 * 60)
- elif choice == "3":
- print("\nFull Month:\n")
- current_time = time.time()
- _, days_in_month = monthrange(current_time)
- for i in range(1, days_in_month + 1):
- print_date(
- calendar.timegm(
- (
- time.localtime(current_time).tm_year,
- time.localtime(current_time).tm_mon,
- i,
- 0,
- 0,
- 0,
- )
- )
- )
- else:
- print("Invalid choice. Please select 1, 2, or 3.")
- def monthrange(timestamp):
- """
- Return the first and last day of the month for a given timestamp.
- """
- struct_time = time.localtime(timestamp)
- year = struct_time.tm_year
- month = struct_time.tm_mon
- days_in_month = calendar.monthrange(year, month)[1]
- return (year, month, 1), days_in_month
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement