shithub: ddate

ref: 0853197432f336576c487b57d3708516b3d6b0b0
dir: /ddate.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>

#define day_of_week( x ) ((x) == 1 ? "Sweetmorn" :\
                          (x) == 2 ? "Boomtime" :\
                          (x) == 3 ? "Pungenday" :\
                          (x) == 4 ? "Prickle-Prikle" :\
                          "Setting Orange")

#define season( x ) ((x) == 0 ? "Chaos" :\
                     (x) == 1 ? "Discord" :\
                     (x) == 2 ? "Confusion" : \
                     (x) == 3 ? "Bureaucracy" :\
                     "The Aftermath")

#define hollyday( d, s ) ((d) == 5 && (s) == 0 ? "Mungday" :\
                         (d) == 50 && (s) == 0 ? "Chaoflux" :\
                         (d) == 5 && (s) == 1 ? "Mojoday" :\
                         (d) == 50 && (s) == 1 ? "Discoflux" :\
                         (d) == 5 && (s) == 2 ? "Syaday" :\
                         (d) == 50 && (s) == 2 ? "Confuflux" :\
                         (d) == 5 && (s) == 3 ? "Zaraday" :\
                         (d) == 50 && (s) == 3 ? "Bureflux" :\
                         (d) == 5 && (s) == 4 ? "Maladay" :\
                         "Afflux")

#define date( x ) ((x)%73 == 0 ? 73 : (x)%73)

#define leap_year( x ) ((x)%400 == 0 || (((x) % 4) == 0 && (x) % 100))

int curyday(void);
int curyr(void);

static void
usage(void)
{
  fprint(2, "usage: ddate [day month year]\n");
  exits("usage");
}

void
main(int argc, char *argv[])
{
  int d, m, y, dd, dy, leap_day;
  char *dw;
  char *ds;

  if( argc == 1){
    /* Get the year and day*/
    d = curyday();
    y = curyr();
  } else if( argc == 4 ){
    d = atoi( argv[1] );
    m = atoi( argv[2] );
    y = atoi( argv[3] );
  } else {
    usage();
  }

  if( leap_year(y) ){
    if( d == 60 ){
      dw = "St. Tib's Day";
      leap_day = 1;
    } else if( d > 60) {
      d = d-1;
    }
  }

  dd = date(d);
  dy = y + 1166;
  ds = season(((d%73)==0?d-1:d)/73);

  if ( leap_day == 1) {
    print("%s of %s, YOLD %d\n", dw, ds, dy);
  } else if (dd == 5 || dd == 50) {
    print("%s, %d of %s, YOLD %d\n", hollyday(dd, ((d%73)==0?d-1:d)/73), dd, ds, dy);
  } else {
    print("%s, %d of %s, YOLD %d\n", day_of_week(d%5), dd, ds, dy);
  }
}

/*
 * system dependent
 * get current day of the year, month and year
 */
int
curyday(void)
{
    Tm *tm;

    tm = localtime(time(0));
    return tm->yday+1;
}

int
curyr(void)
{
	Tm *tm;

	tm = localtime(time(0));
	return tm->year+1900;
}