Advertisement
Python253

date_utility

May 11th, 2024
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: date_utility.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script provides functionality to output the current date or calculate future dates.
  10. It allows the user to choose between single-day, 7-day week, or full month increments and displays the results in the terminal.
  11.  
  12. Requirements:
  13.    - Python 3.x
  14.    - calendar module
  15.  
  16. Functions:
  17.    - print_date(timestamp): Formats and prints the date corresponding to the provided timestamp.
  18.    - year_month_day_weekday(timestamp): Returns year, month, day, and weekday for a given timestamp.
  19.    - monthrange(timestamp): Returns the first and last day of the month for a given timestamp.
  20.  
  21. Usage:
  22. Run the script and follow the on-screen prompts to select an option:
  23.    1. Print today's date
  24.    2. Print the full 7-day week
  25.    3. Print the full month
  26.  
  27. Additional Notes:
  28.    - Option 1 displays the current date only.
  29.    - Option 2 displays the dates for the next 7 days, starting from today.
  30.    - Option 3 displays all the dates in the current month.
  31. """
  32.  
  33. import sys
  34. import time
  35. import calendar
  36.  
  37. def print_date(timestamp):
  38.     """
  39.    Print the formatted date.
  40.    """
  41.     year, month, day, weekday = year_month_day_weekday(timestamp)
  42.     print(f"\t{year}-{month:02d}-{day:02d} ({weekday})")
  43.  
  44. def year_month_day_weekday(timestamp):
  45.     """
  46.    Return year, month, day, and weekday for a given timestamp.
  47.    """
  48.     struct_time = time.localtime(timestamp)
  49.     year = struct_time.tm_year
  50.     month = struct_time.tm_mon
  51.     day = struct_time.tm_mday
  52.     weekday = (
  53.         "Monday",
  54.         "Tuesday",
  55.         "Wednesday",
  56.         "Thursday",
  57.         "Friday",
  58.         "Saturday",
  59.         "Sunday",
  60.     )[struct_time.tm_wday]
  61.     return year, month, day, weekday
  62.  
  63. def main():
  64.     """
  65.    Main function to ouput the date as a single day, 7-day week or full month.
  66.    """
  67.     print("Select an option:\n")
  68.     print("1. Print today's date")
  69.     print("2. Print the full 7-day week")
  70.     print("3. Print the full month")
  71.  
  72.     choice = input("\nEnter your choice (1-3): ")
  73.  
  74.     if choice == "1":
  75.         print("\nToday's Date Is:\n")
  76.         print_date(time.time())
  77.     elif choice == "2":
  78.         print("\nThe 7-Day Week:\n")
  79.         for i in range(7):
  80.             print_date(time.time() + i * 24 * 60 * 60)
  81.     elif choice == "3":
  82.         print("\nFull Month:\n")
  83.         current_time = time.time()
  84.         _, days_in_month = monthrange(current_time)
  85.         for i in range(1, days_in_month + 1):
  86.             print_date(
  87.                 calendar.timegm(
  88.                     (
  89.                         time.localtime(current_time).tm_year,
  90.                         time.localtime(current_time).tm_mon,
  91.                         i,
  92.                         0,
  93.                         0,
  94.                         0,
  95.                     )
  96.                 )
  97.             )
  98.     else:
  99.         print("Invalid choice. Please select 1, 2, or 3.")
  100.  
  101. def monthrange(timestamp):
  102.     """
  103.    Return the first and last day of the month for a given timestamp.
  104.    """
  105.     struct_time = time.localtime(timestamp)
  106.     year = struct_time.tm_year
  107.     month = struct_time.tm_mon
  108.     days_in_month = calendar.monthrange(year, month)[1]
  109.     return (year, month, 1), days_in_month
  110.  
  111. if __name__ == "__main__":
  112.     main()
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement