forked from berthubert/galmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavmon.cc
66 lines (55 loc) · 1.28 KB
/
navmon.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
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
#include "navmon.hh"
#include <errno.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <stdexcept>
#include "fmt/format.h"
#include "fmt/printf.h"
using namespace std;
size_t readn2(int fd, void* buffer, size_t len)
{
size_t pos=0;
ssize_t res;
for(;;) {
res = read(fd, (char*)buffer + pos, len - pos);
if(res == 0)
throw EofException();
if(res < 0) {
throw runtime_error("failed in readn2: "+string(strerror(errno)));
}
pos+=(size_t)res;
if(pos == len)
break;
}
return len;
}
std::string humanTimeNow()
{
time_t now = time(0);
return humanTime(now);
}
std::string humanTime(time_t t)
{
static bool set_tz = false;
struct tm tm={0};
gmtime_r(&t, &tm);
if (!set_tz) {
setenv("TZ", "UTC", 1); // We think in UTC.
tzset();
set_tz = true;
}
char buffer[80];
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S %z", &tm);
// strftime(buffer, sizeof(buffer), "%F %T ", &tm);
return buffer;
}
std::string humanTime(time_t t, uint32_t nanoseconds)
{
struct tm tm={0};
gmtime_r(&t, &tm);
char buffer[80];
std::string fmt = "%a, %d %b %Y %H:%M:"+fmt::sprintf("%06.04f", tm.tm_sec + nanoseconds/1000000000.0) +" %z";
strftime(buffer, sizeof(buffer), fmt.c_str(), &tm);
return buffer;
}