Calculate the moment when someone has lived for 10^9 seconds.
A gigasecond is 10^9 (1,000,000,000) seconds.
Execute the tests with:
$ elixir gigasecond_test.exs
In the test suites, all but the first test have been skipped.
Once you get a test passing, you can unskip the next one by
commenting out the relevant @tag :pending
with a #
symbol.
For example:
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
Or, you can enable all the tests by commenting out the
ExUnit.configure
line in the test suite.
# ExUnit.configure exclude: :pending, trace: true
For more detailed information about the Elixir track, please see the help page.
Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
Code.load_file("gigasecond.exs", __DIR__)
end
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule GigasecondTest do
use ExUnit.Case
# @tag :pending
test "from 4/25/2011" do
assert Gigasecond.from({{2011, 4, 25}, {0, 0, 0}}) == {{2043, 1, 1}, {1, 46, 40}}
end
@tag :pending
test "from 6/13/1977" do
assert Gigasecond.from({{1977, 6, 13}, {0, 0, 0}}) == {{2009, 2, 19}, {1, 46, 40}}
end
@tag :pending
test "from 7/19/1959" do
assert Gigasecond.from({{1959, 7, 19}, {0, 0, 0}}) == {{1991, 3, 27}, {1, 46, 40}}
end
@tag :pending
test "yourself" do
# customize these values for yourself
# your_birthday = {{year1, month1, day1}, {0, 0, 0}}
# assert Gigasecond.from(your_birthday) == {{year2, month2, day2}, {hours, minutes, seconds}}
end
end
defmodule Gigasecond do
@doc """
Calculate a date one billion seconds after an input date.
"""
@one_billion 1_000_000_000
@spec from({{pos_integer, pos_integer, pos_integer}, {pos_integer, pos_integer, pos_integer}}) :: :calendar.datetime
# def from({{year, month, day}, {hours, minutes, seconds}}) do
def from(time) do
((time|> :calendar.datetime_to_gregorian_seconds) + @one_billion)
|> :calendar.gregorian_seconds_to_datetime
end
end
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,104 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments