/*
  Copyright (c) 2005-2009 Gordon Gremme <gordon@gremme.org>
  Copyright (c) 2005-2008 Center for Bioinformatics, University of Hamburg

  Permission to use, copy, modify, and distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <time.h>
#include "core/ma.h"
#include "core/unused_api.h"
#include "gth/time.h"

#define TIMESTRINGLENGTH  80

char* gth_get_time(void)
{
  time_t utime;
  char *timestring;
  GT_UNUSED int rval;

  /* determine timestring */
  if (time(&utime) < 0) {
    timestring = gt_malloc(sizeof (char) * TIMESTRINGLENGTH);
    rval = snprintf(timestring, (size_t) TIMESTRINGLENGTH, "%s",
                    "time not available");
    gt_assert(rval < TIMESTRINGLENGTH);
    return timestring;
  }

  timestring = gt_malloc(sizeof (char) * TIMESTRINGLENGTH);
  if (strftime(timestring, (size_t) TIMESTRINGLENGTH, "%Y-%m-%d %H:%M:%S",
               localtime(&utime)) == 0) {
    rval = snprintf(timestring, (size_t) TIMESTRINGLENGTH, "%s",
                     "problem with time conversion");
    gt_assert(rval < TIMESTRINGLENGTH);
    return timestring;
  }

  return timestring;
}
