File: Time.h

package info (click to toggle)
primrose 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,304 kB
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 92; perl: 67
file content (112 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Modification History
 *
 * 2001-October-29		Jason Rohrer
 * Created.
 *
 * 2004-October-14		Jason Rohrer
 * Fixed sign bug.
 *
 * 2005-February-10		Jason Rohrer
 * Added function to get time in floating point format.
 */

#include "minorGems/common.h"



#ifndef TIME_INCLUDED
#define TIME_INCLUDED 



/**
 * Interface for platform-independent, high-resolution time access.
 *
 * @author Jason Rohrer
 */
class Time {
	public:


		
		/**
		 * Gets the current time in seconds and milliseconds.
		 *
		 * No guarentee about when absolute 0 of this time
		 * scale is for particular systems.
		 *
		 * @param outSeconds pointer to where the time in seconds
		 *   will be returned.
		 * @param outMilliseconds pointer to where the extra
		 *   milliseconds will be returned.  Value returned is in [0,999].
		 */
		static void getCurrentTime( unsigned long *outSeconds,
									unsigned long *outMilliseconds );

        

        /**
         * Gets the current time in fractional (double) seconds.
         *
         * @return the current time in seconds.
         */
        static double getCurrentTime();

        

		/**
		 * Gets the number of milliseconds that have passed
		 * since a time in seconds and milliseconds.
		 *
		 * @param inSeconds the start time, in seconds.
		 * @param inMilliseconds the start time's additional milliseconds.
		 *
		 * @return the number of milliseconds that have passed
		 *   since inSeconds:inMilliseconds.  May overflow if
		 *   more than 49 days have passed (assuming 32-bit longs).
		 */
		static unsigned long getMillisecondsSince(
			unsigned long inSeconds, unsigned long inMilliseconds );


		
	};



inline double Time::getCurrentTime() {
    unsigned long currentTimeS;
	unsigned long currentTimeMS;
	getCurrentTime( &currentTimeS, &currentTimeMS );

    return currentTimeS + currentTimeMS / 1000.0;
    }



inline unsigned long Time::getMillisecondsSince(
	unsigned long inSeconds, unsigned long inMilliseconds ) {

	unsigned long currentTimeS;
	unsigned long currentTimeMS;
	getCurrentTime( &currentTimeS, &currentTimeMS );

	
	unsigned long deltaS = ( currentTimeS - inSeconds );
	long deltaMS = ( (long)currentTimeMS - (long)inMilliseconds );

	// carry, if needed
	if( deltaMS < 0 ) {
		deltaS--;
		deltaMS += 1000;
		}

	return 1000 * deltaS + deltaMS;
	}



#endif