//
//  Copyright © 2001 - 2002, RESON Inc. All Rights Reserved.
//
//  No part of this file may be reproduced or transmitted in any form or by
//  any means, electronic or mechanical, including photocopy, recording, or
//  information storage or retrieval system, without permission in writing
//  from RESON Inc.
//
//  Filename:   SystemTime.cpp
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    Defines the CSystemTime class.
//
//              This is a system helper class to allow the high resolution timer to be used.
//
//  Notes:      
//

#if !defined(AFX_SYSTEMTIME_H__55E41FD1_2FDF_4C0A_8A78_57B132C47C29__INCLUDED_)
#define AFX_SYSTEMTIME_H__55E41FD1_2FDF_4C0A_8A78_57B132C47C29__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#pragma warning( push, 3 )              // Microsoft's multi-media header is dirty at warning level 4.
#include <mmsystem.h>                   
#pragma warning( pop )

#include "Critical.h"
#include "7kProtocol.h"

class EXPORT_DLL CSystemTime
{
public:

    // Contstruction and destruction.

                            CSystemTime             (   void );

                            CSystemTime             (   const   WORD           &rwYear,
                                                        const   WORD           &rwMonth,
                                                        const   WORD           &rwDay,
                                                        const   WORD           &rwHour,
                                                        const   WORD           &rwMinute,
                                                        const   WORD           &rwSecond,
                                                        const   WORD           &rwMilliseconds );

    virtual                 ~CSystemTime             (   void );

    // Current time retrieval.

                            operator SYSTEMTIME     ()  const;

                            operator double         ()  const;

    // High resolution timer support.

    operator                TIMECAPS                ()  const;

    static DWORD            GetTickCount            (   void );

    static void             UpdateSystemTime        (   const   SYSTEMTIME     &rsRequestedTime,
                                                        const   DWORD          &rdwRecievedTimestampMilliseconds );

    static bool             TimeStampToSystemTime   (   const unsigned long    &rulTimestamp,
                                                        SYSTEMTIME             *psSystemTime );

    static bool             TimeStampTo7kTime       (   const unsigned long    &rulTimeStamp,
                                                        TIME7K                 *psTime7k );

    static int              ToJulianDay             (   const   int            &rwDay,
                                                        const   int            &riMonth,
                                                        const   int            &rwYear );

    static void             FromJulianDay           (   int                    &riDay,
                                                        int                    &riMonth,
                                                        int                    &riYear,
                                                        const int              &riJulianDay );

private:

    ///////////////
    // Attributes.

    static CCritical        m_StaticGuard;
    static int              m_iRefCount;
    static bool             m_bHiResTimerInitialized;
    static TIMECAPS         m_sCapabilities;
    static __int64          m_i64ClockFrequency;
    static __int64          m_i64InitialCount;
    static DWORD            m_dwOffset;

    ///////////////
    // Services.

    static double           TimeFromSystemTime      (   const   SYSTEMTIME     &rsSystemTime );
    static bool             IsValidSystemTime       (   const   SYSTEMTIME     &rsSystemTime );
    static void             InitializeHiResTimer    (   void );
    static int              DaysToEndOfMonth        (   const   int            &riMonth );
    static bool             IsLeapYear              (   const   int            &riYear );

    void                    InitializeTimer         (   void );
    void                    RestoreTimer            (   void ) const;

};

#endif // !defined(AFX_SYSTEMTIME_H__55E41FD1_2FDF_4C0A_8A78_57B132C47C29__INCLUDED_)
