/*
 * $Header: /san01/cvs/ashpool/csrc/continuity/mecha/Attic/w32.c,v 1.5 2004/04/30 13:41:56 aleigh Exp $
 */

/*
 * Copyright (c) 2004 Alex Leigh, All Rights Reserved
 * 
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ALEX LEIGH The copyright
 * notice above does not evidence any actual or intended publication of such
 * source code.
 * 
 * Use, disclosure, copying, distribution, or transmission of this code without
 * express permission is prohibited.
 */

#include "mecha.h"

MECHA_UNUSED static int not_empty;

#ifdef WIN32


int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
{
    static struct timeval starttime = { 0, 0 };
    static __int64 lasttime = 0;
    static __int64 freq = 0;
    __int64 timer;
    LARGE_INTEGER li;
    BOOL b;
    double dt;

    /* Get the time, if they want it */
    if (time_Info != NULL) {
        if (starttime.tv_sec == 0) {
            b = QueryPerformanceFrequency(&li);
            if (!b) {
                starttime.tv_sec = -1;
            } else {
                freq = li.QuadPart;
                b = QueryPerformanceCounter(&li);
                if (!b) {
                    starttime.tv_sec = -1;
                } else {
                    getfilesystemtime(&starttime);
                    timer = li.QuadPart;
                    dt = (double) timer / freq;
                    starttime.tv_usec -= (int) ((dt - (int) dt) * 1000000);
                    if (starttime.tv_usec < 0) {
                        starttime.tv_usec += 1000000;
                        --starttime.tv_sec;
                    }
                    starttime.tv_sec -= (int) dt;
                }
            }
        }
        if (starttime.tv_sec > 0) {
            b = QueryPerformanceCounter(&li);
            if (!b) {
                starttime.tv_sec = -1;
            } else {
                timer = li.QuadPart;
                if (timer < lasttime) {
                    getfilesystemtime(time_Info);
                    dt = (double) timer / freq;
                    starttime = *time_Info;
                    starttime.tv_usec -= (int) ((dt - (int) dt) * 1000000);
                    if (starttime.tv_usec < 0) {
                        starttime.tv_usec += 1000000;
                        --starttime.tv_sec;
                    }
                    starttime.tv_sec -= (int) dt;
                } else {
                    lasttime = timer;
                    dt = (double) timer / freq;
                    time_Info->tv_sec = starttime.tv_sec + (int) dt;
                    time_Info->tv_usec =
                        starttime.tv_usec +
                        (int) ((dt - (int) dt) * 1000000);
                    if (time_Info->tv_usec > 1000000) {
                        time_Info->tv_usec -= 1000000;
                        ++time_Info->tv_sec;
                    }
                }
            }
        }
        if (starttime.tv_sec < 0) {
            getfilesystemtime(time_Info);
        }
    }
    /* Get the timezone, if they want it */
    if (timezone_Info != NULL) {
        _tzset();
        timezone_Info->tz_minuteswest = _timezone;
        timezone_Info->tz_dsttime = _daylight;
    }
    /* And return */
    return 0;
}


#endif
