#define DISPLAY_TIME_C

#include "main.h"



/* these first ones are for twinkles... just get things one at a time */
char *get_time_cmp_string(time_t atime)
{
 char *tmp = GET_TIME_STR();
 /* NOTE: %m = (01-12) OR (localtime()->tm_mon + 1) */
 strftime(tmp, MAX_TIME_STR - 2, "%Y/%m.%d-%H:%M+%S", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_ampm_string(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%p", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_second_number(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%S", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_minute_number(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%M", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_hour_number_24(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%H", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_hour_number_12(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%I", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_day_name(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%A", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_day_number(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%w", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_month_name(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%B", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_month_number(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%m", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_month_day_number(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%d", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_week_number(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%U", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_year_number(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%Y", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_year_number_short(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%y", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

char *get_year_day_number(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%j", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

/* returns a string for scripting to use ... */
char *scripting_time(time_t t)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2,  "%d%m%y%H%M%S", localtime(&t));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

/* returns converted user time */
char *convert_time(time_t t)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%I.%M:%S %p - %a, %d %B", localtime(&t));
 tmp[MAX_TIME_STR - 1] = 0;
   
 return (tmp);
}

/* return a string of the time, in 24 hour notation */
char *hour_time24(time_t atime)
{
 char *tmp = GET_TIME_STR();

 strftime(tmp, MAX_TIME_STR - 2, "%H:%M", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
 
 return (tmp);
}

/* return a string of the time, in 12 hour notation */
char *hour_time12(time_t atime)
{
 char *tmp = GET_TIME_STR();

 strftime(tmp, MAX_TIME_STR - 2, "%I:%M:%p", localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
 
 return (tmp);
}

/* return a string of the system time */
char *sys_time(void)
{
 time_t now = time(0);
 char *tmp = GET_TIME_STR();

 strftime(tmp, MAX_TIME_STR - 2, "%H:%M:%S - %d/%m/%y", localtime(&now));
 tmp[MAX_TIME_STR - 1] = 0;
 
 return (tmp);
}

char *return_date(time_t atime)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%a, %d %B" , localtime(&atime));
 tmp[MAX_TIME_STR - 1] = 0;
 
 return (tmp);
}

char *return_date_with_year(time_t t)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%a, %d %B, %Y" , localtime(&t));
 tmp[MAX_TIME_STR - 1] = 0;
 
 return (tmp);
}

char *return_date_and_time(time_t t)
{
 char *tmp = GET_TIME_STR();
 
 strftime(tmp, MAX_TIME_STR - 2, "%a, %d %B at %I:%M:%S" , localtime(&t));

 tmp[MAX_TIME_STR - 1] = 0;
 
 return (tmp);
}

/* print up birthday */
char *birthday_string(time_t bday, unsigned int age)
{
 struct tm *t;
 char *tmp = GET_TIME_STR();

 t = localtime(&bday);
 
 if ((t->tm_mday) > 10 && (t->tm_mday) < 20)
   strftime(tmp, MAX_TIME_STR - 2,
	    (age ? ("%dth of %B %Y") : ("%dth of %B")),
	    t);
 else
   switch ((t->tm_mday) % 10)
   {
   case 1:
     strftime(tmp, MAX_TIME_STR - 2,
	      (age ? ("%dst of %B %Y") : ("%dst of %B")),
	      t);
     break;
     
     
   case 2:
     strftime(tmp, MAX_TIME_STR - 2,
	      (age ? ("%dnd of %B %Y") : ("%dnd of %B")),
	      t);
     break;
     
   case 3:
     strftime(tmp, MAX_TIME_STR - 2,
	      (age ? ("%drd of %B %Y") : ("%drd of %B")),
	      t);
     break;
     
   default:
     strftime(tmp, MAX_TIME_STR - 2,
	      (age ? ("%dth of %B %Y") : ("%dth of %B")),
	      t);
   }

 tmp[MAX_TIME_STR - 1] = 0;
 
 return (tmp);
}

/* return a string of the system time */
char *short_word_time(long atime, unsigned int flags)
{
 char *tmp = GET_TIME_STR();
 char *fill = tmp;
 int years = 0, weeks = 0, days = 0;
 int hours = 0, minutes = 0, seconds = 0;
 unsigned int done_before = 0;
 
 if (!atime)
 {
  strcpy(fill, "no time at all");
  return (tmp);
 }
 
 if ((flags & USE_YEARS) && ((years = (atime / (60 * 60 * 24 * 365))) ||
			     (flags & USE_STICK_YEARS)))
 {
  ++done_before;
  fill += sprintf(fill, "%d", years);
  years = TRUE;
  
  atime %= (60 * 60 * 24 * 365);
 }
 
 if ((flags & USE_WEEKS) && ((weeks = (atime / (60 * 60 * 24 * 7))) ||
			     (flags & USE_STICK_WEEKS)))
 {
  if (done_before)
    fill = stpcpy(fill, ", ");
  ++done_before;
  
  if (USE_MOD_WEEKS & flags)
    weeks %= 52;

  fill += sprintf(fill, "%d", weeks);
  weeks = TRUE;
  
  atime %= (60 * 60 * 24 * 7);
 }

 if ((flags & USE_DAYS) && ((days = (atime / (60 * 60 * 24))) ||
			    (flags & USE_STICK_DAYS)))
 {
  if (done_before)
    fill = stpcpy(fill, ", ");
  ++done_before;
  
  if (USE_MOD_DAYS & flags)
    days %= 7;

  fill += sprintf(fill, "%d", days);

  atime %= (60 * 60 * 24);
 }
 
 if ((flags & USE_HOURS) && ((hours = (atime / (60 * 60))) ||
			     (flags & USE_STICK_HOURS)))
 {
  if (done_before)
    fill = stpcpy(fill, ", ");
  ++done_before;

  if (USE_MOD_HOURS & flags)
    hours %= 24;

  fill += sprintf(fill, "%02d", hours);
  hours = TRUE;/* for stick flag */
  
  atime %= (60 * 60);
 }
 
 if ((flags & USE_MINUTES) && ((minutes = (atime / 60)) ||
			       (flags & USE_STICK_MINUTES)))
 {
  if (done_before)
    if (hours)
      *fill++ = ':';
    else
      fill = stpcpy(fill, ", ");
  ++done_before;
  
  if (USE_MOD_MINUTES & flags)
    minutes %= 60;
  
  fill += sprintf(fill, "%02d", minutes);
  minutes = TRUE;/* in case of stick flag */
  
  atime %= 60;
 }
 
 if ((flags & USE_SECONDS) && ((seconds = atime) ||
			       (flags & USE_STICK_SECONDS)))
 {
  if (done_before)
    if (hours || minutes)
      *fill++ = ':';
    else
      fill = stpcpy(fill, ", ");
  ++done_before;

  if (USE_MOD_SECONDS & flags)
    seconds %= 60;
  
  fill += sprintf(fill, "%02d", seconds);
 }
 
  
 *fill = 0; /* in case we are returning an empty string */
 tmp[MAX_TIME_STR - 1] = 0;
 return (tmp);
}

/* converts time into words */
char *word_time(long atime, unsigned int flags)
{
 char *tmp =  GET_TIME_STR();
 char *fill = tmp;
 int hours = 0, minutes = 0, seconds = 0;
 int years = 0, weeks = 0, days = 0;
 unsigned int done_before = 0;
 
 if (!atime)
 {
  strcpy(fill, "no time at all");
  return (tmp);
 }

 if ((flags & USE_YEARS) && ((years = (atime / (60 * 60 * 24 * 365))) ||
			     (flags & USE_STICK_YEARS)))
 {
  ++done_before;
  fill += sprintf(fill, "%d year%s", years, (years != 1) ? "s" : "" );
  years = TRUE;
  
  atime %= (60 * 60 * 24 * 365);
 }
 
 if ((flags & USE_WEEKS) && ((weeks = (atime / (60 * 60 * 24 * 7))) ||
			     (flags & USE_STICK_WEEKS)))
 {
  if (done_before)
    fill = stpcpy(fill, ", ");
  ++done_before;
  
  if (USE_MOD_WEEKS & flags)
    weeks %= 52;

  fill += sprintf(fill, "%d week%s", weeks, (weeks != 1) ? "s" : "" );
  weeks = TRUE;
  
  atime %= (60 * 60 * 24 * 7);
 }

 if ((flags & USE_DAYS) && ((days = (atime / (60 * 60 * 24))) ||
			    (flags & USE_STICK_DAYS)))
 {
  if (done_before)
    fill = stpcpy(fill, ", ");
  ++done_before;
  
  if (USE_MOD_DAYS & flags)
    days %= 7;

  fill += sprintf(fill, "%d day%s", days, (days != 1) ? "s" : "" );

  atime %= (60 * 60 * 24);
 }

 if ((flags & USE_HOURS) && ((hours = (atime / (60 * 60))) ||
			     (flags & USE_STICK_HOURS)))
 {
  if (done_before)
    fill = stpcpy(fill, ", ");
  ++done_before;
  
 if (USE_MOD_HOURS & flags)
    hours %= 24;

  fill += sprintf(fill, "%d hour%s", hours, (hours != 1) ? "s" : "");
  
  atime %= (60 * 60);
 }
 
 if ((flags & USE_MINUTES) && ((minutes = (atime / 60)) ||
			       (flags & USE_STICK_MINUTES)))
 {
  atime %= 60;

  if (done_before)
    if (!atime)
      fill = stpcpy(fill, " and ");
    else
      fill = stpcpy(fill, ", ");
  ++done_before;

  if (USE_MOD_MINUTES & flags)
    minutes %= 60;

  fill += sprintf(fill, "%d minute%s",
                  minutes, (minutes != 1) ? "s" : "");
  minutes = TRUE;/* in case of stick flag */
 }
  
 if ((flags & USE_SECONDS) && ((seconds = atime) ||
			       (flags & USE_STICK_SECONDS)))
{
  if (done_before)
    fill = stpcpy(fill, " and ");

  if (USE_MOD_SECONDS & flags)
    seconds %= 60;
  
  fill += sprintf(fill, "%d second%s",
                  seconds, (seconds != 1) ? "s" : "");
 }

 *fill = 0; /* in case we are returning an empty string */
 tmp[MAX_TIME_STR - 1] = 0;
 
 return (tmp);
}
