Lab S1: Timestamp Comparator

Topics

Here is some necessary background needed to complete this lab.

Unix Timestamps

Many computers that run Unix-based operating systems like macOS or Linux keep track of the date and time by counting the seconds since 12:00 AM UTC on January 1, 1970, a date and time known as the Unix epoch. A number that represents the time since the Unix Epoch is called a Unix timestamp. Because they are just a simple integer or floating-point number, Unix timestamps are a easy and convenient way to store the date and time in a program.

Click this to get the current timestamp: ???

In Python, you can get the current Unix timestamp as a float by importing the time module and calling time.time().

>>> import time
>>> time.time()
1680322928.051783

Command-line arguments

When you run a command on the command-line, you not only give the name of the command, but also arguments, which serve as input to the program. Consider a command issued to the OK autograder:

python ok -q hailstone

We can break apart this shell command into the program and its arguments.

  • python is the program being run (the Python interpreter)
  • ok is an argument with the Python program file to run
  • -q is an argument indicating the next argument is the name of the question
  • hailstone is an argument for the name of the question.

Within our program, we can access the arguments by importing the sys module and accessing the argv list inside it.

>>> import sys
>>> sys.argv
['ok', '-q', 'hailstone']

Argument #0 in sys.argv is always the filename of the program being executed. Everything typed after the file name starts at sys.argv[1].

Try this: create a file called argv.py and these contents:

import sys
print(sys.argv)

What gets printed if you run python argv.py hello world? What about python argv.py I love Python? python argv.py? Notice that every element of sys.argv is a str.

Instructions

Phase 1: Difference in Timestamps

Write a program that takes two Unix timestamps (now and when) as arguments. Print the difference between the two timestamps (when - now) as an integer. The output should be positive if when is after (larger than) now or negative if when is before (smaller than) now.

You should be able to run your command as follows:

$ python timediff.py <now> <when>

Example:

$ python timediff.py 1622493354 1676217842
53724488

Phase 2: Difference from Now

Modify your program so that the first argument (now) is optional. If there is only one argument, print the difference between when and the current timestamp as an integer. The output should be positive if it was in the past or negative if it is in the future.

Hint: Remember, to get the current timestamp as a float, import time and call time.time(). To get it as an int, wrap it in int(...).

You should still be able to run your command as you did previously, but now also like so:

$ python timediff.py [<now>] <when>

Example, assuming the current timestamp is 1680253759:

$ python timediff.py 1685559158
5305399

Phase 3: Human-Readable Differences

Modify your program so that it reads out the differences in a human-readable format. For example, in 5 days or 21 seconds ago. Only output the most significant time unit of: days, hours, minutes, or seconds.

If there are two arguments, assume that now is the "current time" and output the relative time of when. In other words, if when is 2 hours after now, print in 2 hours, or if when is 5 days before now, print 5 days ago.

If there is only one argument, if when is in the future, print in ______, and if it is in the past, print ______ ago.

Examples, assuming the current timestamp is 1680253759:

$ python timediff.py 1682611200 1701622800
in 220 days
$ python timediff.py 1682611200
in 27 days
$ python timediff.py 1651075200
337 days ago

© 2023 Brigham Young University, All Rights Reserved