Skip to content

Instantly share code, notes, and snippets.

@antaranyan
Last active October 28, 2022 11:28
Show Gist options
  • Select an option

  • Save antaranyan/a7b35c5865a5b312b4287e1191c70bbf to your computer and use it in GitHub Desktop.

Select an option

Save antaranyan/a7b35c5865a5b312b4287e1191c70bbf to your computer and use it in GitHub Desktop.
Space Age

Given an age in seconds, calculate how old someone would be on:

Earth: orbital period 365.25 Earth days, or 31557600 seconds.

Planet Orbital period in Earth years
Mercury 0.2408467
Venus 0.61519726
Mars 1.8808158
Jupiter 11.862615
Saturn 29.447498
Uranus 84.016846
Neptune 164.79132

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

Feature: Space age
Background: Planets' relative time unit system
Given Earth's orbital period (a year) is 365.25 Earth days
And a year in Earth is 31557600 seconds
And other planets orbital periods in Earth years are:
| planet | years |
| Mercury | 0.2408467 |
| Venus | 0.61519726 |
| Mars | 1.8808158 |
| Jupiter | 11.862615 |
| Saturn | 29.447498 |
| Uranus | 84.016846 |
| Neptune | 164.79132 |
Scenario: Space Age
Given someone's age is <age in seconds>
When calculating how old someone would be on <planet>
Then the answer should be <age in years>
Example:
| planet | age in seconds | age in years |
| Earth | 1_000_000_000 | 31.59 |
module SpaceAge
// settings
[<Measure>] type Second
[<Measure>] type Year
[<Literal>]
let EarthOrbitalPeriod = 31557600.0<Second/Year>
type Planet =
| Earth
| Mars
| Mercury
| Venus
| Jupiter
| Saturn
| Uranus
| Neptune
let orbitalPeriodInYearsFor (planet: Planet) =
match planet with
| Earth -> 1.0<Year>
| Mercury -> 0.2408467<Year>
| Mars -> 1.8808158<Year>
| Venus -> 0.61519726<Year>
| Jupiter -> 11.862615<Year>
| Saturn -> 29.447498<Year>
| Uranus -> 84.016846<Year>
| Neptune -> 164.79132<Year>
// helpers
let numberAsSeconds (secondsCount): float<Second> =
(float secondsCount) * 1.0<Second>
let secondsInYears (seconds: float<Second>): float<Year> =
seconds / EarthOrbitalPeriod
// expose
let age (planet: Planet) (secondsCount: int64): float =
let inYears = numberAsSeconds >> secondsInYears
(inYears secondsCount) / (orbitalPeriodInYearsFor planet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment