# include "basic.h"

int date (int argc, char **argv) {
  
  int N, SECONDS, REFTIME;
  struct timeval now;
  char *tstring, *varName;

  SECONDS = FALSE;
  if ((N = get_argument (argc, argv, "-seconds"))) {
    remove_argument (N, &argc, argv);
    SECONDS = TRUE;
  } else {
    ALLOCATE (tstring, char, 32);
  }

  REFTIME = 0;
  if ((N = get_argument (argc, argv, "-reftime"))) {
    remove_argument (N, &argc, argv);
    REFTIME = atoi (argv[N]);
    remove_argument (N, &argc, argv);
  } 

  varName = NULL;
  if ((N = get_argument (argc, argv, "-var"))) {
    remove_argument (N, &argc, argv);
    varName = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
  }

  if (argc != 1) {
    gprint (GP_ERR, "USAGE: date [-var variable] [-seconds] [-reftime seconds]\n");
    return (FALSE);
  }

  gettimeofday (&now, NULL);
  if (SECONDS) {
    if (varName) {
      set_int_variable (varName, now.tv_sec - REFTIME);
    } else {
      gprint (GP_ERR, "%d\n", now.tv_sec - REFTIME);
    }
  } else {
    ctime_r (&now.tv_sec, tstring);
    N = strlen (tstring) - 1;
    tstring[N] = 0;

    if (varName) {
      set_str_variable (varName, tstring);
    } else {
      gprint (GP_ERR, "%s\n", tstring);
    }
    free (tstring);
  }
  return (TRUE);
}
