Your Task

"Days-From-Date" is an interesting and useful feature in website design. It shows up how many days (or years, hours, minutes, and seconds) after an event started. For example, a feature of Days-From-Birth shows the number of years, days, hours, minutes, and seconds after the date of birth. In this assignment, you are going to write a Python program to implement the "Days-From-Birth" feature.

The Concept of Time in Python

In Python, a time is defined as a Date Object. Each date object stores its state as a time value, which is a primitive number that encodes a date as seconds since 1 January 1970 00:00:00 UTC. Thus, a date later than 1 January 1970 00:00:00 UTC will have a positive time value, whereas an earlier date will have a negative time value. On the basis of the common timeline (which we all live on), the distance between any two dates can be calculated using their time values in seconds. Figure 1 illustrates the concept, where C is a date earlier than 1 January 1970 00:00:00 UTC, and A and B are later with B being further than A.

Figure 1: The Difference Between Two Date Instances: see image.

Functional Requirements

Variables

Within the script section, create variables following professional conventions and initialise them using the right values. Some variables have been suggested in the following tables. You should create more when necessary.

Table 1: Variables1

Description Value
Number of seconds in a year# 365 * 60 * 60 * 24
Number of seconds in a day 60 * 60 * 24
Number of seconds in an hour 60 * 60
Number of seconds in a minute 60

# In this assignment, we assume every year has 365 days.

Table 2: Variables2 Initializing value description

Description Initializing value description Type
Year of the event The year of event Number
Month of the event The month of event Number
Day of the event The day of event Number

Calculation

1.Create a Date object for the birth by using the variables created previously.

Use three input functions to input the year, month, and day of the birth one by one with the hints "Please enter the year of your birth, e.g., 1970-2021", "Enter the month of your birth, e.g. 1-12" and "Enter the day of your birth, e.g. 1-31". No validation plan is required. We assume users will enter valid numbers according to hints. For example, if the year of birth is not a leap year. The user won't enter 29 for the date of birth if the month is February. Then transfer them to the Date constructor.

Example of transferring number to the Date constructor and calculating how many seconds of the date since 1 January 1970 00:00:00 UTC:

>>> import datetime, time
>>> t = datetime.datetime (2011, 10, 21, 0, 0)
>>> time.mktime(t.timetuple())
1319148000.0

Mind the order of arguments sent to the constructor

2. Obtain the current time instant, a floating-point number of seconds since "the epoch".

3. Calculate the difference between the current time and the birth time (assume it is the starting time 0:00:00 a.m. on the date of the birth):

  • Get the birth time value in seconds as in the example above.
  • Deduct the birth time value by using the time value of the current time.

4. Calculate the number of years to the event:

  • Divide the time value difference by the number of seconds in a year (1 year =365 days).
  • Use the floor division to reduce the resulting number to an integer.

5. Calculate the number of days, hours, minutes, and seconds in the remaining time value:

  • Mod the time value difference by the number of seconds in a year.
  • Divide the mod result by the number of seconds in a day.
  • Use the floor division to reduce the number to an integer for the number of days.
  • Repeat the three steps above to calculate the number of hours, minutes, and seconds. You may need to update the calculating formula accordingly.

Presentation

Sample output is presented below when running the "Days-From-Birth" program.

"Your birthday is 25/7/1999 (XXX years, XXX days, XXX hours, XXX minutes, and XXX seconds ago)."

Note that

  • the information should be displayed using the print function.
  • wherever possible you should use variables in expressions instead of explicit values (e.g., numbers), The date format must be the same as "25/7/1999". Other formats (e.g., 25/07/1999 or 25 July 1999) are not acceptable; "XXX" is the result of your calculations.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.