exercism

Exercism - Gigasecond

This post shows you how to get Gigasecond exercise of Exercism.

Stevinator Stevinator
6 min read
SHARE
exercism dart flutter gigasecond

Preparation

Before we click on our next exercise, let’s see what concepts of DART we need to consider

Gigasecond Exercise

So we need to use the following concepts.

DateTime

DateTime represents a point in time. You can create DateTime objects and perform operations on them.

void main() {
  // Create a DateTime
  DateTime date = DateTime(2015, 1, 24, 22, 0, 0);
  print(date); // 2015-01-24 22:00:00.000
  
  // Get current date and time
  DateTime now = DateTime.now();
  print(now);
}

Duration

Duration represents a span of time. You can add durations to DateTime objects to get a new DateTime.

void main() {
  DateTime start = DateTime(2015, 1, 24, 22, 0, 0);
  
  // Create a duration
  Duration duration = Duration(seconds: 1000000000);
  
  // Add duration to DateTime
  DateTime result = start.add(duration);
  print(result); // 2046-10-02 23:46:40.000
}

Const Constructors

The const keyword creates compile-time constants. When used with constructors, it ensures the object is created at compile time.

void main() {
  // Const duration - created at compile time
  const Duration gigasecond = Duration(seconds: 1000000000);
  
  DateTime date = DateTime(2015, 1, 24, 22, 0, 0);
  DateTime result = date.add(gigasecond);
  print(result);
}

Expression Bodied Methods

When we have a method with one code line, we can use this concept to keep our code tidy.

class Example {
  DateTime addTime(DateTime date) => date.add(Duration(seconds: 1000));
}

Method Chaining

You can chain method calls together. The add method returns a new DateTime, which can be used directly.

void main() {
  DateTime date = DateTime(2015, 1, 24, 22, 0, 0);
  
  // Method chaining
  DateTime result = date.add(const Duration(seconds: 1000000000));
  print(result);
}

Introduction

The way we measure time is kind of messy. We have 60 seconds in a minute, and 60 minutes in an hour. This comes from ancient Babylon, where they used 60 as the basis for their number system. We have 24 hours in a day, 7 days in a week, and how many days in a month? Well, for days in a month it depends not only on which month it is, but also on what type of calendar is used in the country you live in.

What if, instead, we only use seconds to express time intervals? Then we can use metric system prefixes for writing large numbers of seconds in more easily comprehensible quantities.

A food recipe might explain that you need to let the brownies cook in the oven for two kiloseconds (that’s two thousand seconds).

Perhaps you and your family would travel to somewhere exotic for two megaseconds (that’s two million seconds).

And if you and your spouse were married for a thousand million seconds, you would celebrate your one gigasecond anniversary.

Instructions

Your task is to determine the date and time one gigasecond after a certain date.

A gigasecond is one thousand million seconds. That is a one with nine zeros after it.

If you were born on January 24th, 2015 at 22:00 (10:00:00pm), then you would be a gigasecond old on October 2nd, 2046 at 23:46:40 (11:46:40pm).

What is a gigasecond?

A gigasecond is one billion (1,000,000,000) seconds. The term uses the metric prefix “giga-” which means one billion. This is approximately 31.69 years.

The concept of using metric prefixes for time measurements comes from science fiction, particularly Vernor Vinge’s novel “A Deepness in the Sky”, where the metric system is used as the basis for time measurements. This approach simplifies time calculations by using a consistent base unit (seconds) with standard prefixes.

— Science Fiction & Time Measurement

How can we calculate a gigasecond?

To calculate a date one gigasecond after a given date:

  1. Create a Duration object with 1,000,000,000 seconds
  2. Add this duration to the given DateTime
  3. Return the new DateTime

For example, starting from January 24th, 2015 at 22:00:00:

  • Add 1,000,000,000 seconds
  • Result: October 2nd, 2046 at 23:46:40

The DateTime class handles all the complexity of days, months, years, and leap years automatically.

⚠️ Old Solution (No Longer Works)

Previously, the solution used an unnecessary nested function structure. Here’s what the old solution looked like:

DateTime addGigasecondTo(final DateTime dateTime) {
  DateTime add(final DateTime birthDate) {
    return birthDate.add(Duration(seconds: 1000000000));
  }
}

Why This Solution Doesn’t Work Anymore

The old solution has several issues:

  1. Unnecessary nested function: The function add is defined inside addGigasecondTo but is never called. This creates dead code that serves no purpose.

  2. Missing return statement: The outer function addGigasecondTo doesn’t return anything. It defines an inner function but never uses it or returns a result.

  3. Overcomplicated structure: The nested function structure adds unnecessary complexity without providing any benefit. The solution can be much simpler.

  4. Not using const: The Duration object is created every time the function is called, even though it’s a constant value. Using const would be more efficient.

The exercise now requires:

  • A direct, simple implementation that adds the duration to the DateTime
  • Using const for the Duration since it never changes
  • Expression-bodied methods for concise code
  • No unnecessary nested functions or complexity

Solution

DateTime addGigasecondTo(DateTime dateTime) =>
    dateTime.add(const Duration(seconds: 1000000000));

Let’s break down the solution:

  1. DateTime addGigasecondTo(DateTime dateTime) - Function signature:

    • Takes a DateTime parameter
    • Returns a DateTime representing one gigasecond later
  2. => - Expression-bodied function syntax:

    • Allows writing the function body in a single line
    • Automatically returns the result of the expression
  3. dateTime.add(...) - Adds a duration to the DateTime:

    • The add method creates a new DateTime with the duration added
    • Handles all date/time arithmetic including leap years, month boundaries, etc.
  4. const Duration(seconds: 1000000000) - Creates a constant duration:

    • const ensures the Duration is created at compile time (more efficient)
    • 1000000000 is one billion seconds (one gigasecond)
    • The Duration object is reused across all function calls

The solution is clean, efficient, and directly solves the problem without unnecessary complexity. The DateTime class automatically handles all the edge cases like leap years, different month lengths, and time zone considerations.


You can watch this tutorial on YouTube. So don’t forget to like and subscribe. 😉

Watch on YouTube
Stevinator

Stevinator

Stevinator is a software engineer passionate about clean code and best practices. Loves sharing knowledge with the developer community.