/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : FastTime.cc                                                    */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 03/07/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/

#include <time.h>
#include <stdlib.h>
#include "FastTime.h"
#ifdef FASTTIME

/*
CLASS 
FastTime

DESCRIPTION
Artificial time for use in simulation 

AUTHOR
Henthorn
*/

FastTime::FastTime()
{
    Boolean debug = True;
    dprintf("\n**** FastTime ctor [%p] ****\n",this);
}

FastTime::~FastTime()
{
    Boolean debug = True;
    dprintf("\n**** FastTime dtor [%p] ****\n",this);
}

// Only called when fastsim option is used in a simulation
//
void FastTime::initialize(TimeIF::TimeSpec *ts)
{
    Boolean debug=False;
    
    // If no time is passed in, use the current system time
    if (!ts)
    {
        struct timespec timeNow;
        clock_gettime(CLOCK_REALTIME, &timeNow);
        ts = (TimeIF::TimeSpec*)&timeNow;
    }
    _time.data.ts.seconds     = ts->seconds;
    _time.data.ts.nanoSeconds = ts->nanoSeconds;
    
    _time.write();
    dprintf("\n\n\t\t\t\tFastTime inititalized to epoch %ld.%ld\n\n",
           _time.data.ts.seconds, _time.data.ts.nanoSeconds);
    
}

void FastTime::getFastTime(struct timespec *ts)
{
    if (ts) {
        _time.read();
        ts->tv_sec  = _time.data.ts.seconds;
        ts->tv_nsec = _time.data.ts.nanoSeconds;
    }else{
        Syslog::write("FastTime::getFastTime - Invalid arg: ts[%p] ****\n",ts);
    }
}

void FastTime::incrFastTime(int milliseconds)
{
    long GIGASECS = 1000000000L;
    if( (milliseconds > 0) ){
        // Increment the artificial clock
        _time.read();
        long ns = _time.data.ts.nanoSeconds + milliseconds*1000000L;
        _time.data.ts.seconds += ns/GIGASECS;
        _time.data.ts.nanoSeconds = ns % GIGASECS;
        
        _time.write();
    }else{
        Syslog::write("FastTime::incrFastTime - Invalid arg: millis[%p] ****\n",milliseconds);
    }
}

#endif