/****************************************************************************/
/* Copyright 2003 MBARI														*/
/****************************************************************************/
/* Summary	: Main Routine for OASIS Mooring Controller						*/
/* Filename : oasis.c														*/
/* Author	: Robert Herlien (rah)											*/
/* Project	: OASIS Mooring Replacement (OASIS3)							*/
/* Revision : 0.1															*/
/* Created	: 01/13/2003													*/
/*																			*/
/* MBARI provides this documentation and code "as is", with no warranty,	*/
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium	*/
/* Research Institute to assist in its use, correction, modification, or	*/
/* enhancement. This information should not be published or distributed to	*/
/* third parties without specific written permission from MBARI.			*/
/*																			*/
/****************************************************************************/
/* Modification History:													*/
/* 13jan2003 rah - created from OASIS clock.c								*/
/* $Log: clock.c,v $
/* Revision 1.1	 2011/10/25 22:39:21  bobh
/* Initial oasis4 revision
/*
/* Revision 1.1	 2009/03/25 20:34:41  bobh
/* Initial checkin
/*
/* Revision 1.3	 2003/09/03 17:48:16  bobh
/* Further mods for config file changes
/*
 */
/****************************************************************************/

#include <mbariTypes.h>					/* MBARI type definitions			*/
#include <mbariConst.h>					/* MBARI constants					*/
#include <oasis.h>						/* OASIS controller definitions		*/
#include <clock.h>						/* OASIS Clock module				*/
#include <io.h>							/* OASIS I/O definitions			*/
#include <utils.h>						/* OASIS Utility routines			*/

#include <cfxpico.h>					/* Persistor PicoDOS definitions	*/
#include <string.h>
#include <time.h>


/********************************/
/*		External Data			*/
/********************************/

Extern Int16	gmtOffset;


/********************************/
/*		Global Data				*/
/********************************/

Global	TimeOfDay		tod;			/* Current time of day			*/


/************************************************************************/
/* Function	   : clk_read												*/
/* Purpose	   : Read Time of Day from the RTC chip						*/
/* Input	   : None													*/
/* Outputs	   : Time of Day											*/
/* Side Effects: Fills in tod with TimeOfDay with DateTime				*/
/************************************************************************/
		TimeOfDay
clk_read( Void )
{
  return(RTCtime(&tod));				/* Get accurate current time*/

} /* clk_read() */


/************************************************************************/
/* Function	   : clk_write												*/
/* Purpose	   : Set Date and Time in CF2 RTC							*/
/* Input	   : Ptr to DateTime struct									*/
/* Outputs	   : None													*/
/************************************************************************/
		Void
clk_write( struct tm *ptm )
{
  time_t		newtime;

  newtime = mktime(ptm);
  RTCSetTime(newtime, 0L);

} /* clk_write() */


/************************************************************************/
/* Function	   : clkLocalHour											*/
/* Purpose	   : Get hour in local time zone							*/
/* Input	   : None													*/
/* Outputs	   : Hour (0 - 23)											*/
/************************************************************************/
		Nat16
clkLocalHour( Void )
{
  struct tm		*nowtm;

  RTCtime(&tod);
  nowtm = gmtime(&tod);
  return((nowtm->tm_hour + gmtOffset + 24) % 24);

} /* clkLocalHour() */


/************************************************************************/
/* Function	   : clk_printtime											*/
/* Purpose	   : Print date & time to serial port						*/
/* Input	   : Ptr to DateTime struct									*/
/* Outputs	   : None													*/
/************************************************************************/
		Void
clk_printtime(TimeOfDay ptime)
{
  struct tm		*tmptr;

  tmptr = gmtime(&ptime);

  xprintf("%04d/%02d/%02d %02d:%02d:%02d ", tmptr->tm_year + 1900,
		  tmptr->tm_mon+1, tmptr->tm_mday, tmptr-> tm_hour,
		  tmptr->tm_min, tmptr->tm_sec);

} /* clk_printtime() */
