Avatar of Chase-Lambert

Chase-Lambert's solution

to Gigasecond in the Clojure Track

Published at Jan 05 2021 · 0 comments
Test suite

Given a moment, determine the moment that would be after a gigasecond has passed.

A gigasecond is 10^9 (1,000,000,000) seconds.


Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


(ns gigasecond-test
  (:require [clojure.test :refer [deftest is]]

(deftest from-apr-25-2011
  (is (= [2043 1 1] (gigasecond/from 2011 4 25))))

(deftest from-jun-13-1977
  (is (= [2009 2 19] (gigasecond/from 1977 6 13))))

(deftest from-jul-19-1959
  (is (= [1991 3 27] (gigasecond/from 1959 7 19))))

;; customize this to test your birthday and find your gigasecond date:
;; (deftest your-birthday
;;   (is (= [year2 month2 day2] (gigasecond/from year1 month1 day1))))
(ns gigasecond
  (:require [tick.alpha.api :as t]))

(def giga->days
  (int (/ 1000000000 60 60 24)))

(defn from [year month day]
  (let [starting-date (format "%d-%02d-%02d" year month day)
        date (t/date starting-date)
        new-date  (str (t/+ date (t/new-period giga->days :days)))
        new-year  (Integer/parseInt (subs new-date 0 4))
        new-month (Integer/parseInt (subs new-date 5 7))
        new-day   (Integer/parseInt (subs new-date 8))]
    [new-year new-month new-day]))

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?