🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of mbraak

mbraak's solution

to Gigasecond in the Objective-C Track

Published at Apr 16 2019 · 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

#import "Gigasecond.h"

@implementation Gigasecond
- (id) initWithStartDate: (NSDate*) startDate {
    self = [super init];
    
    if (self) {
        self.startDate = startDate;
    }
    
    return self;
}

- (NSDate*) gigasecondDate {
    int billion = pow(10, 9);

    return [NSDate dateWithTimeInterval: billion sinceDate: self.startDate];
}
@end

Gigasecond.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Gigasecond : NSObject
@property NSDate* startDate;

- (id) initWithStartDate: (NSDate*) startDate;
- (NSDate*) gigasecondDate;
@end

NS_ASSUME_NONNULL_END

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?