Skip to content

Instantly share code, notes, and snippets.

@tylerneylon
Created July 2, 2013 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerneylon/5913287 to your computer and use it in GitHub Desktop.
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>
// 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;
}
@tylerneylon
Copy link
Author

You can compile and run this from bash with the command:

make 7date ; ./7date

You can install it as an always-available command by running this from the same dir that you ran make in:

sudo ln -s `pwd`/7date /usr/local/bin/7date

The above commands assume you have some (usually installed) things like make and a C compiler, and that /usr/local/bin is in your $PATH.

@tylerneylon
Copy link
Author

I gave this small program some little upgrades and its own repo:

https://github.com/tylerneylon/7date_C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment