source: nrEngine/src/GetTime.cpp @ 1

Revision 1, 2.6 KB checked in by art, 13 years ago (diff)
Line 
1/***************************************************************************
2 *                                                                         *
3 *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
4 *       mailto: <tevs@mpi-sb.mpg.de>                                      *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 ***************************************************************************/
12
13//----------------------------------------------------------------------------------
14// Includes
15//----------------------------------------------------------------------------------
16#include "GetTime.h"
17
18//namespace nrEngine{
19
20        //------------------------------------------------------------------------
21#if NR_PLATFORM == NR_PLATFORM_WIN32
22#if 0
23   int gettimeofday(struct timeval* tv, void* placeholder)
24   {
25                union {
26                        long long ns100;
27                        FILETIME ft;
28                } now;
29
30                GetSystemTimeAsFileTime (&now.ft);
31                tv->tv_usec = (long) ((now.ns100 / 10LL) % 1000000LL);
32                tv->tv_sec = (long) ((now.ns100 - 116444736000000000LL) / 10000000LL);
33printf("%d\n", tv->tv_sec);
34
35                return 0;
36   }
37#endif
38#if 1
39/*
40 * Number of micro-seconds between the beginning of the Windows epoch
41 * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
42 *
43 * This assumes all Win32 compilers have 64-bit support.
44 */
45#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__)
46  #define DELTA_EPOCH_IN_USEC  11644473600000000Ui64
47#else
48  #define DELTA_EPOCH_IN_USEC  11644473600000000ULL
49#endif
50
51static nrEngine::uint64 filetime_to_unix_epoch (const FILETIME *ft)
52{
53    nrEngine::uint64 res = (nrEngine::uint64) ft->dwHighDateTime << 32;
54
55    res |= ft->dwLowDateTime;
56    res /= 10;                   /* from 100 nano-sec periods to usec */
57    res -= DELTA_EPOCH_IN_USEC;  /* from Win epoch to Unix epoch */
58    return (res);
59};
60
61int gettimeofday (struct timeval *tv, void *tz)
62{
63    FILETIME  ft;
64    nrEngine::uint64 tim;
65
66    if (!tv) {
67        errno = EINVAL;
68        return (-1);
69    }
70    GetSystemTimeAsFileTime (&ft);
71    tim = filetime_to_unix_epoch (&ft);
72    tv->tv_sec  = (long) (tim / 1000000L);
73    tv->tv_usec = (long) (tim % 1000000L);
74
75    return (0);
76}
77
78#endif
79
80#endif
81               
82//}; // end namespace   
83
Note: See TracBrowser for help on using the repository browser.