ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# n0mn0m's solution

## to Gigasecond in the Swift Track

Published at Nov 22 2020 · 0 comments
Instructions
Test suite
Solution

Calculate the moment when someone has lived for 10^9 seconds.

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

## Setup

Go through the project setup instructions for Xcode using Swift:

http://exercism.io/languages/swift
http://exercism.io/languages/swift/tests

Notably from the source directory:

`swift test` runs tests
`swift package generate-xcodeproj` creates an Xcode project

## Source

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.

### GigasecondTests.swift

``````import XCTest
@testable import Gigasecond

class GigasecondTests: XCTestCase {

func test1 () {
let gs = Gigasecond(from: "2011-04-25T00:00:00")?.description
XCTAssertEqual("2043-01-01T01:46:40", gs)
}

func test2 () {
let gs = Gigasecond(from: "1977-06-13T00:00:00")?.description
XCTAssertEqual("2009-02-19T01:46:40", gs)
}

func test3 () {
let gs = Gigasecond(from: "1959-07-19T00:00:00")?.description
XCTAssertEqual("1991-03-27T01:46:40", gs)
}

func testTimeWithSeconds () {
let gs = Gigasecond(from: "1959-07-19T23:59:59")?.description
XCTAssertEqual("1991-03-28T01:46:39", gs)
}

func testFullTimeSpecified () {
let gs = Gigasecond(from: "2015-01-24T22:00:00")?.description
XCTAssertEqual("2046-10-02T23:46:40", gs)
}

func testFullTimeWithDayRollOver () {
let gs = Gigasecond(from: "2015-01-24T23:59:59")?.description
XCTAssertEqual("2046-10-03T01:46:39", gs)
}

static var allTests: [(String, (GigasecondTests) -> () throws -> Void)] {
return [
("test1 ", test1 ),
("test2 ", test2 ),
("test3 ", test3 ),
("testTimeWithSeconds ", testTimeWithSeconds ),
("testFullTimeSpecified ", testFullTimeSpecified ),
("testFullTimeWithDayRollOver ", testFullTimeWithDayRollOver ),
]
}
}``````

### LinuxMain.swift

``````import XCTest
@testable import GigasecondTests

XCTMain([
testCase(GigasecondTests.allTests),
])``````
``````//Solution goes in Sources
import Foundation

private var cachedFormatters = [String : DateFormatter]()

extension DateFormatter {
static func cached(withFormat format: String) -> DateFormatter {
if let cachedFormatter = cachedFormatters[format] { return cachedFormatter }
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(abbreviation: "UTC")
cachedFormatters[format] = formatter
return formatter
}
}

struct Gigasecond {
var description: Optional<String> = nil

init?(from: String) {
let giga: TimeInterval = 1_000_000_000
let formatter = DateFormatter.cached(withFormat: "yyyy-MM-dd'T'HH:mm:ss")
guard let d = formatter.date(from: from) else {
return nil
}
description = formatter.string(for: n)
}
}``````