"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.
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.
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 |
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):
4. Calculate the number of years to the event:
5. Calculate the number of days, hours, minutes, and seconds in the remaining time value:
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