/*
 *
 * timecvt.c
 *
 * Time conversion routines.
 *
 * $Id: timecvt.C,v 1.3 2006/11/14 02:19:01 vonkorff Exp $
 *
 */

#include <stdlib.h>
#include <time.h>
#ifdef unix
#include <stdio.h>
#include <unistd.h>
#endif
#ifdef _WIN32
#include <io.h>
#include <stdio.h>
#endif
#include <string.h>    /* hiramc - moved out of the _WIN32 ifdef. 99/06/25 */
#include <math.h>
#include "db_table.h"
#include "sqlapi.h"
#include "ap_schema.h"
#include "setilib.h"
#include "timecvt.h"

void timecvt_start(){}

static void trim_newline(char*p) {
    int n = strlen(p);
    if (n && (p[n-1]=='\n')) {
	p[n-1] = 0;
    }
}

char* jd_string(double jd) {
    static char buf[256], datestr[64];
    time_t tmptime;

	if ( jd > JD1970.uval() )
	{
		tmptime=jd_to_time_t(jd);
#ifdef macintosh
  		strcpy(datestr, asctime(localtime(&tmptime)));
#else
		strcpy(datestr, asctime(gmtime(&tmptime)));
#endif
		trim_newline(datestr);
		sprintf(buf, "%14.5f (%s)", jd, datestr);
	}
	else
	{
		sprintf(buf,"%14.5f",jd);
	}
    return buf;
}

char* short_jd_string(double jd) {
    static char buf[256], datestr[64];
    time_t tmptime;

	if ( jd > 0.00001 )
	{
		tmptime=jd_to_time_t(jd);
#ifdef macintosh
  		strcpy(datestr, asctime(localtime(&tmptime)));
#else
		strcpy(datestr, asctime(gmtime(&tmptime)));
#endif
		trim_newline(datestr);
		sprintf(buf, "%s", datestr);
	}
	else
	{
		strcpy(buf,"0.0");
	}
    return buf;
}

void st_to_ut(TIME *st) {
  double hour=((double)st->h)+((double)st->m)/60+((double)st->s)/3600
       +((double)st->c)/360000;
  if (st->tz !=0) {
    hour-=st->tz;
    st->tz=0;
    if (hour<0) {
      hour+=24;
      st->d--;
      if (st->d<1) {
	st->y--;
	st->d+=NUM_DAYS(st->y);
      }
    }
    if (hour>=24) {
      hour-=24;
      st->d++;
      if (st->d>NUM_DAYS(st->y)) {
	st->d-=NUM_DAYS(st->y);
	st->y++;
      }
    }
    st->h=(int)hour;
    hour-=st->h;
    hour*=60;
    st->m=(int)hour;
    hour-=st->m;
    hour*=60;
    st->s=(int)hour;
    hour-=st->s;
    hour*=100;
    st->c=(int)hour;
  }
}

double ut_to_jd(long year, long month, long day, double hour) {
  double jd;
  long l=(month-14)/12;
  jd=(double)(day-32075L+1461L*(year+4800L+l)/4+367l*(month-2-l*12)/12-
	       3*((year+4900l+l)/100)/4);
  jd+=(hour/24-0.5);
  return(jd);
}

double time_t_to_jd(time_t unix_time) {
  return (((double)unix_time)/SECONDS_PER_DAY+JD1970.uval());
}

time_t jd_to_time_t(double jd) {
  return ((time_t)((jd-JD1970.uval())*SECONDS_PER_DAY));
}

void st_time_convert(TIME *st) {
/* Fix Y2K Bug */
  if (st->y < 90) {
    st->y+=2000;
  } else if (st->y < 100) {
    st->y+=1900;
  }

/* Convert time zome time into UT */
  st_to_ut(st);

/* Fill in julian date field */
  {
    double hour=((double)st->h)+((double)st->m)/60+((double)st->s)/3600
                 +((double)st->c)/360000;
    st->jd=ut_to_jd(st->y,1,st->d,hour);
  }

/* Fill in unix_time field */
   st->unix_time=jd_to_time_t(st->jd);

}

char* hr_min_sec (double x) {
    static char buf[256];
    int hr = (int)(x / 3600);
    int min = (int)((x/3600-(double)hr)*60);
    float s = (float)(x - ((double)hr)*3600 - ((double)min)*60); 
    sprintf(buf, "%d hr %02d min %04.1f sec", hr, min, s);
    return buf;
}


void timecvt_end(){}

/*
 * $Log: timecvt.C,v $
 * Revision 1.3  2006/11/14 02:19:01  vonkorff
 * Fixed a number of bugs in astropulse Makefile.am and configure.ac
 * Fixed bugs in makefiles
 *
 * Revision 1.2  2003/06/23 22:52:28  boincadm
 * *** empty log message ***
 *
 * Revision 1.1  2003/02/26 00:04:07  pauld
 * Initial revision
 *
 * Revision 4.0  2000/10/05 18:12:12  korpela
 * Synchronized versions to 4.0 following release of 3.0 client
 *
 * Revision 3.8  2000/09/14 01:01:31  korpela
 * *** empty log message ***
 *
 * Revision 3.7  2000/04/24 08:39:06  charlief
 * eliminate compiler warning in hr_min_sec() function.
 *
 * Revision 3.6  2000/01/19 08:32:32  davea
 * *** empty log message ***
 *
 * Revision 3.5  1999/10/19 08:07:24  davea
 * *** empty log message ***
 *
 * Revision 3.4  1999/06/26 06:56:44  hiramc
 * Hiram 99/06/25 23:59 moved the include <string.h> out of the
 * ifdef _WIN32 to define strcpy() for all compiles
 *
 * Revision 3.3  1999/06/22 09:40:36  charlief
 * Reverse last change:restore jd_string() as it was before.
 * Add new function short_jd_string(), which does not print
 * Julian Date as a floating point value.
 *
 * Revision 3.1  1999/06/10 00:44:11  korpela
 * *** empty log message ***
 *
 * Revision 3.0  1999/05/14 19:17:35  korpela
 * 1.0(Win/Mac) 1.1(Unix) Release Version
 *
 * Revision 2.11  1999/03/14 00:23:02  davea
 * *** empty log message ***
 *
 * Revision 2.10  1999/03/11 21:46:58  korpela
 * Fixed rounding error in st_to_ut().
 *
 * Revision 2.9  1999/03/05  09:15:07  kyleg
 * *** empty log message ***
 *
 * Revision 2.8  1999/02/18  19:36:49  korpela
 * *** empty log message ***
 *
 * Revision 2.7  1999/02/13  23:54:44  kyleg
 * *** empty log message ***
 *
 * Revision 2.6  1999/02/11 08:36:54  davea
 * *** empty log message ***
 *
 * Revision 2.5  1998/11/17 21:47:02  korpela
 * *** empty log message ***
 *
 * Revision 2.4  1998/11/05  21:25:21  davea
 * replace floor() with (int)
 *
 * Revision 2.3  1998/11/05 02:00:06  kyleg
 * *** empty log message ***
 *
 * Revision 2.2  1998/11/02 17:59:06  korpela
 * *** empty log message ***
 *
 * Revision 2.1  1998/11/02  17:23:29  korpela
 * .`
 *
 * Revision 2.0  1998/10/30  22:00:04  korpela
 * Conversion to C++ and merger with client source tree.
 *
 * Revision 1.2  1998/10/27  00:58:56  korpela
 * Bug fixes.
 *
 * Revision 1.1  1998/10/19  18:57:56  korpela
 * Initial revision
 *
 *
 */
