Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of EggmanQi

EggmanQi's solution

to Gigasecond in the Objective-C Track

Published at Dec 25 2018 · 0 comments
Test suite

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

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


There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/gigasecond/Gigasecond.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


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.


#import <XCTest/XCTest.h>

#if __has_include("GigasecondExample.h")
# import "GigasecondExample.h"
# else
# import "Gigasecond.h"

@interface GigasecondTest : XCTestCase

@property (nonatomic, strong) NSDateFormatter *dateFormatter;


@implementation GigasecondTest

- (void)setUp {
    [super setUp];
    self.dateFormatter = [[NSDateFormatter alloc] init];
    [self.dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [self.dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

- (void)test1 {
    NSDate *startDate = [self.dateFormatter dateFromString:@"2011-04-25T00:00:00"];
    Gigasecond *gigasecond = [[Gigasecond alloc] initWithStartDate:startDate];
    NSDate *expectedDate = [self.dateFormatter dateFromString:@"2043-01-01T01:46:40"];
    XCTAssertEqualObjects(expectedDate, [gigasecond gigasecondDate]);

- (void)test2 {
    NSDate *startDate = [self.dateFormatter dateFromString:@"1977-06-13T00:00:00"];
    Gigasecond *gigasecond = [[Gigasecond alloc] initWithStartDate:startDate];
    NSDate *expectedDate = [self.dateFormatter dateFromString:@"2009-02-19T01:46:40"];
    XCTAssertEqualObjects(expectedDate, [gigasecond gigasecondDate]);

- (void)test3 {
    NSDate *startDate = [self.dateFormatter dateFromString:@"1959-07-19T00:00:00"];
    Gigasecond *gigasecond = [[Gigasecond alloc] initWithStartDate:startDate];
    NSDate *expectedDate = [self.dateFormatter dateFromString:@"1991-03-27T01:46:40"];
    XCTAssertEqualObjects(expectedDate, [gigasecond gigasecondDate]);

- (void)test24HourTime {
    NSDate *startDate = [self.dateFormatter dateFromString:@"2015-01-24T22:00:00"];
    Gigasecond *gigasecond = [[Gigasecond alloc] initWithStartDate:startDate];
    NSDate *expectedDate = [self.dateFormatter dateFromString:@"2046-10-02T23:46:40"];
    XCTAssertEqualObjects(expectedDate, [gigasecond gigasecondDate]);

- (void)testTimeWithSeconds {
    NSDate *startDate = [self.dateFormatter dateFromString:@"1959-07-19T23:59:59"];
    Gigasecond *gigasecond = [[Gigasecond alloc] initWithStartDate:startDate];
    NSDate *expectedDate = [self.dateFormatter dateFromString:@"1991-03-28T01:46:39"];
    XCTAssertEqualObjects(expectedDate, [gigasecond gigasecondDate]);

- (void)testFullTimeWithDayRollOver {
    NSDate *startDate = [self.dateFormatter dateFromString:@"2015-01-24T23:59:59"];
    Gigasecond *gigasecond = [[Gigasecond alloc] initWithStartDate:startDate];
    NSDate *expectedDate = [self.dateFormatter dateFromString:@"2046-10-03T01:46:39"];
    XCTAssertEqualObjects(expectedDate, [gigasecond gigasecondDate]);

//  Gigasecond.m
//  exercismOC
//  Created by eggman qi on 2018/12/25.
//  Copyright © 2018 EBrainStudio. All rights reserved.

#import "Gigasecond.h"

@interface Gigasecond ()
@property (nonatomic, assign)NSDate	*myDate;

@implementation Gigasecond

- (Gigasecond *)initWithStartDate:(NSDate *)date
	_myDate = date;
	return self;

- (NSDate *)gigasecondDate
	NSTimeInterval t = [_myDate timeIntervalSince1970];
	t += pow(10, 9);
	return [NSDate dateWithTimeIntervalSince1970:t];


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?