forked from keithw/datagrump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimestamp.cc
37 lines (28 loc) · 793 Bytes
/
timestamp.cc
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
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <string>
#include "timestamp.hh"
using namespace std;
using namespace Network;
/* nanoseconds per millisecond */
static const uint64_t MILLION = 1000000;
/* nanoseconds per second */
static const uint64_t BILLION = 1000 * MILLION;
/* 6.829 epoch */
static const uint64_t EPOCH = 1362700000000;
/* Current time in milliseconds since the epoch */
uint64_t Network::timestamp( void )
{
struct timespec ts;
if ( clock_gettime( CLOCK_REALTIME, &ts ) < 0 ) {
perror( "clock_gettime" );
throw string( "clock_gettime error" );
}
return timestamp( ts );
}
uint64_t Network::timestamp( const struct timespec & ts )
{
const uint64_t nanos = ts.tv_sec * BILLION + ts.tv_nsec;
return nanos / MILLION - EPOCH;
}