The faster you move, the slower time goes relative to an obvserver. As you approach the speed of light, those effects can become massive, and hard to wrap your head around. I have recently been reading Project Hail Mary by Andy Weir, and a long space journey where time dilation comes into play is part of the plot. It got me thinking if we could use Python to calculate the dilation effects exactly for various journeys.

In this Python script, we can calculate the time dilation experienced during a space journey, given the velocity of the spaceship, its acceleration, and the duration of the journey. The script uses the astropy library, which provides a set of great tools for astronomy and astrophysics (you can have endless fun in general with astropy). The script takes three arguments from the command line, which are the velocity of the spaceship, the acceleration of the spaceship and the duration of the journey.

It then calculates the Lorentz factor and the time dilation factor. The Lorentz factor is a measure of how much time slows down for a moving object, and the time dilation factor is the reciprocal of the Lorentz factor and represents the amount by which time appears to be stretched for a moving object relative to a stationary observer. There’s some big maths involved here, but astropy can help us out with some handy constants.

The script calculates the time interval measured on the spaceship and the dates on the spaceship and Earth at the end of the journey. Finally, it prints out the results of the calculations.

From this, we learn something cool: consider a spaceship departing on the 26th Feb 2023 on a 50 year journey, averaging at 90% of the speed of light at 0.9g acceleration. When it’s journey is complete, the date on earth will be 26th Feb 2073 - 50 years from now. But the date on the spaceship will be 13th December 2044. So, only 21.79 years will have passed on the ship for the 50 years on earth. Cool, huh?

Here is a breakdown of the code. First, let’s set up out imports and turn off some warnings triggered by the odd dates we might generate, and import our command line variables:

import argparse
from astropy import constants as const
from astropy import units as u
from astropy.time import Time
import warnings
from astropy.utils.exceptions import AstropyWarning

# Ingore warnings about odd dates
warnings.simplefilter('ignore', category=AstropyWarning)

# Parse command line arguments
parser = argparse.ArgumentParser(description='Calculate the time dilation experienced during a space journey.')
parser.add_argument('velocity', type=float, help='velocity of the spaceship as a fraction of the speed of light')
parser.add_argument('acceleration', type=float, help='acceleration of the spaceship in units of g0')
parser.add_argument('duration', type=float, help='duration of the journey in years')
args = parser.parse_args()

Now, we need to set up our variables and get them into the correct formats for astropy to deal with:

# Set up input parameters
v = args.velocity * const.c  # velocity of the spaceship
a = args.acceleration * const.g0  # acceleration of the spaceship
duration = args.duration * u.yr  # duration of the journey
earth_time = Time.now() # Define starting time on Earth (as the current time)

And the big sum, calculating the Lorentz factor, followed by the time dilation factor. We use the constants we get from astropy to help us:

# Calculate the Lorentz factor, using the formula γ = 1 / sqrt(1 - v^2/c^2)
gamma = 1 / (1 - (v / const.c) ** 2) ** 0.5

# Calculate the time dilation factor
dilation_factor = 1 / gamma

Simple! Now, we can simply multiple that by the dilation to calculate how much time will have actually passed for the voyagers:

# Calculate the time interval measured on the spaceship
delta_t = dilation_factor * duration

# Calculate the date on the spaceship
ship_time = earth_time + delta_t

# Calculate the date on Earth after journey ends
earth_time_after = earth_time + duration

And we are done! Print out some info in a handy format, or whatever else you might want to do with it:

print('Date on Earth departure:', earth_time.iso)
print('Date on spaceship at journey end:', ship_time.iso)
print('Date on Earth at journey end:', earth_time_after.iso)
print('Lorentz factor: {:.2f}'.format(gamma))
print('Time dilation factor: {:.2f}'.format(dilation_factor))
print('Time interval on spaceship: {:.2f}'.format(delta_t.to(u.yr)))

The output will look something like this:

Date on Earth departure: 2023-02-27
Date on spaceship at journey end: 2044-12-13 10:32:46.386
Date on Earth at journey end: 2073-02-26 12:00:19.883
Lorentz factor: 2.29
Time dilation factor: 0.44
Time interval on spaceship: 21.79 yr

You can see the full code on GitHub here.