/*
 * W-NEWS       A simple NEWS processing package.
 *		xtime - convert a time_t * into a decent date format
 *
 * Version:     @(#)xtime.c	4.50	03-Feb-1995
 *
 * Author:      Fred N. van Kempen, <waltje@Infomagic.COM>
 *		Miquel van Smoorenburg, <miquels@drinkel.ow.org>
 *
 *		Copyright 1988-1993 MicroWalt Corporation
 *		Copyright 1994,1995 Cistron Software C.V.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 */
#include "libwnews.h"


extern char *months[];		/* Is in logmsg.c */
char *days[] = {
  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};


/*
 * Make a decent DATE/TIME string.
 * There are many different formats, but some newsreaders only accept
 * numeric timezones. We add the ASCII timezone as comment, so
 * that we get:
 *
 * Fri, 2 Sep 1994 14:47:00 +0200 (MET DST)
 *
 * On systems that don't have timezones we leave out the last part.
 *
 */
char *
xtime(salt)
time_t *salt;
{
  static char buff[64];
  struct tm *tm;
#ifdef __linux__
  char tzone[32];
  int isdst = 0;

  tm = localtime(salt);
  tzone[0] = 0;
  if (tm->tm_isdst >= 0) isdst = tm->tm_isdst;

  /* Only add time difference if we're not in UTC */
  if (timezone) {
	tzone[0] = ' ';

	/* + and - reversed in header (?) */
	if (timezone < 0) {
		tzone[1] = '+';
		timezone = -timezone;
	} else
		tzone[1] = '-';

	/* Add one hour for Daylight Saving Time. */
	if (isdst) timezone += 3600;

	sprintf(tzone + 2, "%02d%02d",
		(int)(timezone / 3600L),
		(int)((timezone / 60) % 60));
  }

  /* Can add timezone name as comment. */
  if (tzname[0]) {
	strcat(tzone, " (");
	strcat(tzone, tzname[isdst]);
	strcat(tzone, ")");
  }

  sprintf(buff, "%s, %d %s %4d %02d:%02d:%02d%s",
	days[tm->tm_wday], tm->tm_mday, months[tm->tm_mon], 
	    1900 + tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec,
		tzone);
#else
  /* Systems without timezone info. */
  tm = localtime(salt);
  sprintf(buff, "%s, %d %s %4d %02d:%02d:%02d",
	days[tm->tm_wday], tm->tm_mday, months[tm->tm_mon], 
	    1900 + tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
#endif
  return(buff);
}
