# EggmanQi's solution

## to Gigasecond in the Objective-C Track

Published at Dec 25 2018 · 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

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.

## 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.

### GigasecondTest.m

``````#import <XCTest/XCTest.h>

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

@interface GigasecondTest : XCTestCase

@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@end

@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]);
}

@end``````
``````//
//  Gigasecond.m
//  exercismOC
//
//  Created by eggman qi on 2018/12/25.
//

#import "Gigasecond.h"

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

@implementation Gigasecond

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

return self;
}

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

@end``````