New repo for repo.or.cz
[The-Artvertiser.git] / artvertiser / FProfiler / FTime.cpp
blobc3f4407deec995b943b78c80b4d32be8c85e8783
1 /*
2 Copyright 2007, 2008, 2009, 2010 Damian Stewart <damian@frey.co.nz>.
3 Distributed under the terms of the GNU General Public License v3.
5 This file is part of The Artvertiser.
7 The Artvertiser is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 The Artvertiser is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with The Artvertiser. If not, see <http://www.gnu.org/licenses/>.
21 #include "FTime.h"
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
26 #ifdef OSX
27 #else
28 #include <time.h>
29 #endif
31 #include <assert.h>
33 double FTime::Update()
35 #ifdef OSX
36 uint64_t now = mach_absolute_time();
37 uint64_t diff = now - time;
39 static double conversion = 0.0;
40 if ( conversion == 0.0 )
42 mach_timebase_info_data_t info;
43 kern_return_t err = mach_timebase_info( &info );
45 if( err == 0 )
46 conversion = 1e-9*(double)info.numer / (double) info.denom;
49 time = now;
50 last_update_time = (float)(conversion * (double) diff);
52 #else
53 timespec now;
54 clock_gettime(CLOCK_REALTIME, &now);
56 // calculate diff
57 double diff = now.tv_sec - time.tv_sec;
58 diff += 1e-9*double(now.tv_nsec - time.tv_nsec);
60 time = now;
61 last_update_time = diff;
63 #endif
64 return last_update_time;
67 void FTime::SetSeconds( double seconds )
69 #ifdef OSX
70 assert( false && "implement me" );
71 #else
72 // truncate
73 time.tv_sec = (long long)seconds;
74 // nanoseconds
75 time.tv_nsec = (seconds - (long long)seconds)*1e9;
76 #endif
81 double FTime::ToMillis() const
83 #ifdef OSX
84 static double conversion = 0.0;
85 if ( conversion == 0.0 )
87 mach_timebase_info_data_t info;
88 kern_return_t err = mach_timebase_info( &info );
90 if( err == 0 )
91 conversion = 1e-6*(double)info.numer / (double) info.denom;
94 return (conversion * (double) time);
96 #else
98 double result = 1e3*double(time.tv_sec) + 1e-6*double(time.tv_nsec);
99 return result;
101 #endif