# NoLongerBreathedIn's solution

## to Gigasecond in the Objective-C Track

Published at Oct 23 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.h

``````#import <objc/NSObject.h>
@interface Gigasecond : NSObject {
@private
NSDate *gdate;
}
- (Gigasecond *) initWithStartDate : (NSDate *) date;
- (NSDate *) gigasecondDate;
@end``````

### Gigasecond.m

``````#import "Gigasecond.h"
@implementation Gigasecond
- (Gigasecond *) initWithStartDate : (NSDate *) date {
self = [super init];
return(self);
}
- (NSDate *)gigasecondDate {
return(gdate);
}
@end``````