Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework gigasecond to be slightly more challenging #925

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions exercises/practice/gigasecond/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Use UTC

To solve this problem, you'll need to convert a `time_t` value into a `struct tm` value.
The [`time.h`][time.h] library gives you two functions to do this.
Make sure you use the one that results in a calendar time expressed in **UTC**.

[time.h]: https://en.cppreference.com/w/c/chrono
8 changes: 6 additions & 2 deletions exercises/practice/gigasecond/.meta/example.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "gigasecond.h"

time_t gigasecond_after(time_t start)
#define GIGASECOND 1000000000

void gigasecond(time_t start, char *buffer, size_t size)
{
return start + 1000000000;
time_t after = start + GIGASECOND;
struct tm *t = gmtime(&after);
wolf99 marked this conversation as resolved.
Show resolved Hide resolved
strftime(buffer, size, "%Y-%m-%d %H:%M:%S", t);
}
2 changes: 1 addition & 1 deletion exercises/practice/gigasecond/.meta/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include <time.h>

time_t gigasecond_after(time_t);
void gigasecond(time_t input, char *output, size_t size);

#endif
2 changes: 1 addition & 1 deletion exercises/practice/gigasecond/gigasecond.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include <time.h>

time_t gigasecond_after(time_t);
void gigasecond(time_t input, char *output, size_t size);

#endif
42 changes: 25 additions & 17 deletions exercises/practice/gigasecond/test_gigasecond.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "test-framework/unity.h"
#include "gigasecond.h"

#define BUFFER_SIZE 20 // "YYYY-mm-dd HH:MM:SS"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define BUFFER_SIZE 20 // "YYYY-mm-dd HH:MM:SS"
#define BUFFER_SIZE (strlen("YYYY-mm-dd HH:MM:SS") + 1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively: #define BUFFER_SIZE sizeof("YYYY-mm-dd HH:MM:SS")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively: #define BUFFER_SIZE sizeof("YYYY-mm-dd HH:MM:SS")

That's much better! I don't know why that never occurred to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be a static size_t instead of a #define so it only needs to be calculated once?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be calculated at compile time even if it's a #define.


static inline size_t is_leap_year(int year)
{
if (year % 400 == 0)
Expand Down Expand Up @@ -49,49 +51,55 @@ void tearDown(void)

static void test_date(void)
{
time_t expected = construct_date(2043, 1, 1, 1, 46, 40);
time_t actual = gigasecond_after(construct_date(2011, 4, 25, 0, 0, 0));
TEST_ASSERT(expected == actual);
time_t before = construct_date(2011, 4, 25, 0, 0, 0);
char after[BUFFER_SIZE] = { 0 };
gigasecond(before, after, BUFFER_SIZE);
TEST_ASSERT_EQUAL_STRING("2043-01-01 01:46:40", after);
}

static void test_another_date(void)
{
TEST_IGNORE(); // delete this line to run test
time_t expected = construct_date(2009, 2, 19, 1, 46, 40);
time_t actual = gigasecond_after(construct_date(1977, 6, 13, 0, 0, 0));
TEST_ASSERT(expected == actual);
time_t before = construct_date(1977, 6, 13, 0, 0, 0);
char after[BUFFER_SIZE] = { 0 };
gigasecond(before, after, BUFFER_SIZE);
TEST_ASSERT_EQUAL_STRING("2009-02-19 01:46:40", after);
}

static void test_third_date(void)
{
TEST_IGNORE();
time_t expected = construct_date(1991, 3, 27, 1, 46, 40);
time_t actual = gigasecond_after(construct_date(1959, 7, 19, 0, 0, 0));
TEST_ASSERT(expected == actual);
time_t before = construct_date(1959, 7, 19, 0, 0, 0);
char after[BUFFER_SIZE] = { 0 };
gigasecond(before, after, BUFFER_SIZE);
TEST_ASSERT_EQUAL_STRING("1991-03-27 01:46:40", after);
}

static void test_date_and_time(void)
{
TEST_IGNORE();
time_t expected = construct_date(2046, 10, 2, 23, 46, 40);
time_t actual = gigasecond_after(construct_date(2015, 1, 24, 22, 0, 0));
TEST_ASSERT(expected == actual);
time_t before = construct_date(2015, 1, 24, 22, 0, 0);
char after[BUFFER_SIZE] = { 0 };
gigasecond(before, after, BUFFER_SIZE);
TEST_ASSERT_EQUAL_STRING("2046-10-02 23:46:40", after);
}

static void test_date_and_time_with_day_rollover(void)
{
TEST_IGNORE();
time_t expected = construct_date(2046, 10, 3, 1, 46, 39);
time_t actual = gigasecond_after(construct_date(2015, 1, 24, 23, 59, 59));
TEST_ASSERT(expected == actual);
time_t before = construct_date(2015, 1, 24, 23, 59, 59);
char after[BUFFER_SIZE] = { 0 };
gigasecond(before, after, BUFFER_SIZE);
TEST_ASSERT_EQUAL_STRING("2046-10-03 01:46:39", after);
}

/*
static void test_your_birthday(void)
{
time_t birthday = construct_date(1989, 1, 1, 1, 1, 1);
time_t gigday = gigasecond_after(birthday);
printf("%s", ctime(&gigday));
char gigday[BUFFER_SIZE] = {0};
gigasecond(birthday, gigday, BUFFER_SIZE);
printf("%s", gigday);
}
*/

Expand Down