Skip to content

Commit

Permalink
Rework gigasecond to be slightly more challenging (#925)
Browse files Browse the repository at this point in the history
* Rework gigasecond to be slightly more challenging

* clang-format

* review suggestions
  • Loading branch information
glennj authored Nov 6, 2023
1 parent ede0600 commit b19ba9f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@
"uuid": "000340f6-e30d-4d49-a255-016237d6fe60",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"difficulty": 3,
"topics": [
"dates"
]
Expand Down
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);
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 sizeof("YYYY-mm-dd HH:MM:SS")

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

0 comments on commit b19ba9f

Please sign in to comment.