/happycday.c Secret
Last active
January 14, 2021 18:20
Star
You must be signed in to star a gist
More better C version of happyCDay in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
struct tm c_day = { | |
.tm_sec=0, | |
.tm_min=0, | |
.tm_hour= 0, | |
.tm_mday= 10, | |
.tm_mon= 9, | |
.tm_year=118, | |
.tm_wday=0, | |
.tm_yday= 0, | |
.tm_isdst= 0 | |
}; | |
int isCDay(time_t current_time) { | |
struct tm current_tm = *localtime(¤t_time); | |
current_tm.tm_sec = current_tm.tm_min = current_tm.tm_hour = current_tm.tm_wday = current_tm.tm_yday = 0; | |
return difftime(mktime(¤t_tm), mktime(&c_day)) == 0.0; | |
} | |
int main() { | |
static const char months[][12] = { | |
"January", "February", "March", | |
"April", "May", "June", | |
"July", "August", "September", | |
"October", "November", "December" | |
}; | |
time_t now = time(NULL); | |
if (isCDay(now)) { | |
printf("Happy C Day!.\n"); | |
} else { | |
struct tm current_time = *localtime(&now); | |
printf("The current month is %s.\n", months[current_time.tm_mon]); | |
} | |
} |
and a link to the article that started it all:
https://blog.codacy.com/an-in-depth-explanation-of-code-complexity/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And his proposed C version: