/*
 * TimeStamp.hh
 *
 * Copyright 2001, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
 * Copyright 2001, Bastiaan Bakker. All rights reserved.
 *
 * See the COPYING file for the terms of usage and distribution.
 */

#ifndef _LOG4CPP_TIMESTAMP_HH
#define _LOG4CPP_TIMESTAMP_HH

#if defined (WIN32)
#include "windows.h"
#include "math.h"
#elif defined (__linux)
typedef long long __int64;
#else
#error Unsupported target platform
#endif

#include <log4cpp/Portability.hh>

namespace log4cpp {

    /**
     * A simple TimeStamp abstraction
     **/
    class LOG4CPP_EXPORT TimeStamp {
        public:
        /**
           Constructs a TimeStamp representing 'now'.
        **/
        TimeStamp();

        /**
           Constructs a TimeStamp representing the given offset since the
           epoch ( 00:00:00 1970/1/1 UTC).
        **/
        TimeStamp(unsigned int seconds, unsigned int microSeconds = 0);

        /**
           Returns the 'seconds' part of the TimeStamp.
        **/
        inline __int64 getSeconds() const {
        #ifdef WIN32
            double dT;
            dT = (double)m_ActTime.QuadPart / (double)Freq.QuadPart;
            return (__int64) floor(dT);
        #else
            return _seconds;
        #endif
        };

        /** 
           Returns the 'subseconds' part of the TimeStamp in milliseconds,
           getMilliSeconds() == getMicroSeconds() / 1000. 
        **/
        inline __int64 getMilliSeconds() const {
            return getMicroSeconds() / 1000;
        };

        /**
           Returns the subsecond part of the TimeStamp in microseconds.
           The actual precision of this value depends on the platform and
           may be in the order of milliseconds rather than microseconds.
         **/
        inline __int64 getMicroSeconds() const {
        #ifdef WIN32
            double dT;
            dT = (double)m_ActTime.QuadPart / (double)Freq.QuadPart;
            dT = (dT - floor(dT)) * 1000000;
            return (__int64) dT;
        #else
            return _microSeconds;
        #endif
        };

        inline __int64 getElapsedTimeMS() const {
        #ifdef WIN32
            double dT;
            dT = (double)(m_StartTime.QuadPart - m_ActTime.QuadPart) / (double)Freq.QuadPart;
            dT *= 1000;
            return (__int64) dT;
        #else
            return _microSeconds;
        #endif
        };

        /**
           Returns a TimeStamp representing the time at which the application
           started.
        **/
        static inline const TimeStamp& getStartTime() {
            return _startStamp;
        };

        protected:
        static TimeStamp _startStamp;
      
      #ifdef WIN32
        static LARGE_INTEGER  Freq;
        static bool           bIsInit;
        LARGE_INTEGER         m_ActTime;
        static LARGE_INTEGER  m_StartTime;
      #else
        int _seconds;
        int _microSeconds;
      #endif
    };
}

#endif // _LOG4CPP_TIMESTAMP_HH

