shithub: ddate

ref: 2ee5654130eb7579fc539f0021a6250baee10800
dir: ddate/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 day_of_year(int d, int m, int y);
int curyday(void);
int curmo(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 y, m, d, dy, dd, leap_day;
  char *dw;
  char *ds;

  leap_day = 0;
  
  if( argc == 1 ){
    /* Get the year, day and month of today*/
    d = curyday();
    //m = curmo();
    y = curyr();
  } else if( argc == 4 ) {
    d = atoi( argv[1] );
    m = atoi( argv[2] );
    y = atoi( argv[3] );
    d = day_of_year(d, m, y);
  } 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);
  }

}

int
day_of_year( int d, int m, int y)
{
  int ml[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  for( ; m > 1; m --){
    d = d + ml[ m - 2 ];
    if( m == 3 && leap_year(y)){
      d = d + 1;
    }
  }
  return d;
}

/*
 * 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
curmo(void)
{
	Tm *tm;

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

int
curyr(void)
{
	Tm *tm;

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