Skip to content

Instantly share code, notes, and snippets.

@paulghaddad
Last active August 29, 2015 14:10
Show Gist options
  • Save paulghaddad/160b2b4329e2e7a4ff38 to your computer and use it in GitHub Desktop.
Save paulghaddad/160b2b4329e2e7a4ff38 to your computer and use it in GitHub Desktop.
Exercism: Gigasecond
# Gigasecond
Write a program that will calculate the date that someone turned or will celebrate their 1 Gs anniversary.
A gigasecond is one billion (10**9) seconds.
## Source
Chapter 9 in Chris Pine's online Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=09)
require 'date'
require 'time'
class Gigasecond
GIGASECONDs_IN_DAYS = 10**9 / (60 * 60 * 24) # 1 day = 86,400 seconds
def self.from(date)
date + GIGASECONDs_IN_DAYS
end
end
require 'minitest/autorun'
require 'date'
require 'time'
require_relative 'gigasecond'
class GigasecondTest < MiniTest::Unit::TestCase
def test_1
gs = Gigasecond.from(Date.new(2011, 4, 25))
assert_equal Date.new(2043, 1, 1), gs
end
def test_2
gs = Gigasecond.from(Date.new(1977, 6, 13))
assert_equal Date.new(2009, 2, 19), gs
end
def test_3
gs = Gigasecond.from(Date.new(1959, 7, 19))
assert_equal Date.new(1991, 3, 27), gs
end
def test_yourself
your_birthday = Date.new(1982, 10, 16)
gs = Gigasecond.from(your_birthday)
assert_equal Date.new(2014, 6, 24), gs
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment