1 /*
2
3 HyPhy - Hypothesis Testing Using Phylogenies.
4
5 Copyright (C) 1997-now
6 Core Developers:
7 Sergei L Kosakovsky Pond (sergeilkp@icloud.com)
8 Art FY Poon (apoon42@uwo.ca)
9 Steven Weaver (sweaver@temple.edu)
10
11 Module Developers:
12 Lance Hepler (nlhepler@gmail.com)
13 Martin Smith (martin.audacis@gmail.com)
14
15 Significant contributions from:
16 Spencer V Muse (muse@stat.ncsu.edu)
17 Simon DW Frost (sdf22@cam.ac.uk)
18
19 Permission is hereby granted, free of charge, to any person obtaining a
20 copy of this software and associated documentation files (the
21 "Software"), to deal in the Software without restriction, including
22 without limitation the rights to use, copy, modify, merge, publish,
23 distribute, sublicense, and/or sell copies of the Software, and to
24 permit persons to whom the Software is furnished to do so, subject to
25 the following conditions:
26
27 The above copyright notice and this permission notice shall be included
28 in all copies or substantial portions of the Software.
29
30 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
34 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
35 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
36 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37
38 */
39
40 #include "time_difference.h"
41
42 #if defined(__APPLE__) && defined(__MACH__)
43 #include <mach/mach.h>
44 #include <mach/mach_time.h>
45 mach_timebase_info_data_t sTimebaseInfo;
46 #endif
47
48
TimeDifference(void)49 TimeDifference::TimeDifference (void) {
50 this->Start();
51 }
52
Start(void)53 void TimeDifference::Start (void) {
54 #if defined(__APPLE__) && defined(__MACH__)
55 base_time = mach_absolute_time();
56 #elif defined __UNIX__
57 gettimeofday (&base_time,NULL);
58 #endif
59 }
60
TimeSinceStart(void) const61 double TimeDifference::TimeSinceStart (void) const {
62
63 #if defined(__APPLE__) && defined(__MACH__)
64 if ( sTimebaseInfo.denom == 0 ) {
65 (void) mach_timebase_info(&sTimebaseInfo);
66 }
67 uint64_t diff = mach_absolute_time()- base_time;
68 return diff * 1e-9 * sTimebaseInfo.numer / sTimebaseInfo.denom;
69 #elif defined __UNIX__
70 timeval current_time;
71 gettimeofday (¤t_time,NULL);
72 return (current_time.tv_sec-base_time.tv_sec) + (current_time.tv_usec-base_time.tv_usec)*0.000001;
73 #endif
74
75 return 0.0;
76 }
77