Panda3D
Loading...
Searching...
No Matches
time_base.h
1#ifndef __TIME_BASE_H__
2#define __TIME_BASE_H__
3/*
4 * Functions To support General Time Managment. And to allow for cross
5 * platform use. Today Really Two Base classes and one convience class
6 * Time_Clock = The clock time down to micro seconds.. Time_Span = Delta Time
7 * to the Mico Second.. Time_Out = Help timer ............count down a
8 * duration. I realize TimeClock is really an implied delta to EPOCH. I have
9 * chosen to implement it this way. it may be apropriate to convert it all to
10 * delta times with an EPOCk constant and functions that can handle the EPOCK
11 * to current time. All though this is probably the "right" implementation
12 * most coders do not think of clock time in this fashon. General
13 * Observation.. Windows 2k and Linux are really slow (~250k a sec) at
14 * returning the current system time ?? So use time functions that grab the
15 * current system time sparingly ??
16 */
17#ifdef WIN32
18#include <winsock2.h>
19#include <wtypes.h>
20#include <sys/types.h>
21#include <sys/timeb.h>
22#else
23#include <sys/time.h>
24#endif
25#include <time.h>
26#include <string>
27#include <assert.h>
28
29enum { USEC = 1000000 };
30/**
31 *
32 */
33inline void NormalizeTime(timeval &in)
34{
35 while (in.tv_usec >= USEC)
36 {
37 in.tv_usec -= USEC;
38 in.tv_sec++;
39 }
40
41 while (in.tv_usec < 0)
42 {
43 in.tv_usec += USEC;
44 in.tv_sec--;
45 }
46}
47
48/**
49 *
50 */
51inline void TimeDif(const struct timeval &start, const struct timeval &fin, struct timeval &answer)
52{
53 answer.tv_usec = fin.tv_usec - start.tv_usec;
54 answer.tv_sec = fin.tv_sec - start.tv_sec;
55 NormalizeTime(answer);
56}
57/**
58 *
59 */
60inline void TimeAdd(const struct timeval &start, const struct timeval &delta, struct timeval &answer)
61{
62 answer.tv_usec = start.tv_usec + delta.tv_usec;
63 answer.tv_sec = start.tv_sec + delta.tv_sec;
64 NormalizeTime(answer);
65}
66
67#ifdef WIN32
68// Lets make Windows think it is a unix machine :)
69
70/**
71 *
72 */
73inline int gettimeofday(struct timeval *tv, void * trash)
74{
75 struct timeb timeb;
76 ftime( &timeb);
77 tv->tv_sec = (long)timeb.time;
78 tv->tv_usec = (unsigned int)timeb.millitm * 1000;
79 return 0;
80}
81#endif
82
83#include "time_clock.h"
84#include "time_span.h"
85#include "time_general.h"
86#include "time_out.h"
87
88#endif //__TIME_BASE_H__