Created
July 2, 2013 21:24
-
-
Save tylerneylon/5913287 to your computer and use it in GitHub Desktop.
7date.c Print out today's date in the form: <day-of-year base 7 (0-indexed)>.<year>
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
// 7date.c | |
// | |
// Print out today's date in the form | |
// <day-of-year base 7 (0-indexed)>.<year> | |
// | |
// Examples: | |
// | |
// Jan 1, 1925 == 0.1925 | |
// | |
// Feb 1, 2025 == 43.2025 | |
// | |
#include <stdio.h> | |
#include <time.h> | |
#define BUF_SIZE 64 | |
// Returns a base 7 string representation of the given number. | |
// The returned value should be copied unless used before the | |
// next call to base_7_str. | |
char *const base_7_str(int n) { | |
if (n < 0) return ""; | |
if (n == 0) return "0"; | |
static char buf[BUF_SIZE]; | |
char *c = buf + (BUF_SIZE - 1); | |
*c-- = '\0'; | |
while (n > 0 && c >= buf) { | |
*c-- = '0' + n % 7; | |
n /= 7; | |
} | |
if (c < buf) return ""; | |
return ++c; | |
} | |
int main(int argc, char **argv) { | |
time_t time_sec; | |
time(&time_sec); | |
struct tm *t = localtime(&time_sec); | |
printf("%s.%d\n", base_7_str(t->tm_yday), t->tm_year + 1900); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I gave this small program some little upgrades and its own repo:
https://github.com/tylerneylon/7date_C