]> git.tdb.fi Git - libs/core.git/blob - source/time/utils.cpp
Rename to libmspcore
[libs/core.git] / source / time / utils.cpp
1 /*
2 This file is part of libmspframework     
3 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #include <sys/resource.h>
7 #include <sys/time.h>
8 #include "timedelta.h"
9 #include "timestamp.h"
10 #include "units.h"
11 #include "utils.h"
12
13 namespace Msp {
14 namespace Time {
15
16 /**
17 Returns the current timestamp.
18 */
19 TimeStamp now()
20 {
21         timeval tv;
22         gettimeofday(&tv, 0);
23         return TimeStamp(tv.tv_sec*1000000LL+tv.tv_usec);
24 }
25
26 /**
27 Returns the CPU time used by the program so far.
28 */
29 TimeDelta get_cpu_time()
30 {
31         rusage ru;
32         getrusage(RUSAGE_SELF, &ru);
33         return (ru.ru_utime.tv_sec+ru.ru_stime.tv_sec)*sec + (ru.ru_utime.tv_usec+ru.ru_stime.tv_usec)*usec;
34 }
35
36 /**
37 Sleeps for the given time.
38 */
39 int sleep(const TimeDelta &d)
40 {
41 #ifndef WIN32
42         timespec ts;
43         d.fill_timespec(ts);
44         return nanosleep(&ts, 0);
45 #else
46         Sleep(d/msec);
47         return 0;
48 #endif
49 }
50
51 } // namespace Time
52 } // namespace Msp