0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Time.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
24 
25 #include <Common/Compat.h>
26 
27 #include "Time.h"
28 
29 #include <cassert>
30 #include <chrono>
31 #include <ctime>
32 #include <iomanip>
33 #include <iostream>
34 #include <ratio>
35 
36 using namespace std;
37 
38 namespace Hypertable {
39 
40 int64_t get_ts64() {
41  assert((ratio_less_equal<chrono::system_clock::duration::period, chrono::nanoseconds::period>::value));
42  return (int64_t)chrono::duration_cast<chrono::nanoseconds>(chrono::system_clock::now().time_since_epoch()).count();
43 }
44 
45 ostream &hires_ts(ostream &out) {
46  auto now = chrono::system_clock::now();
47  return out << chrono::duration_cast<chrono::seconds>(now.time_since_epoch()).count() <<'.'<< setw(9) << setfill('0') << (chrono::duration_cast<chrono::nanoseconds>(now.time_since_epoch()).count() % 1000000000LL);
48 }
49 
50 #if defined(__sun__)
51 time_t timegm(struct tm *t) {
52  time_t tl, tb;
53  struct tm *tg;
54 
55  tl = mktime (t);
56  if (tl == -1)
57  {
58  t->tm_hour--;
59  tl = mktime (t);
60  if (tl == -1)
61  return -1; /* can't deal with output from strptime */
62  tl += 3600;
63  }
64  tg = gmtime (&tl);
65  tg->tm_isdst = 0;
66  tb = mktime (tg);
67  if (tb == -1)
68  {
69  tg->tm_hour--;
70  tb = mktime (tg);
71  if (tb == -1)
72  return -1; /* can't deal with output from gmtime */
73  tb += 3600;
74  }
75  return (tl - (tb - tl));
76 }
77 #endif
78 
79 } // namespace Hypertable
STL namespace.
Compatibility Macros for C/C++.
Time related declarations.
ostream & hires_ts(ostream &out)
Prints the current time as seconds and nanoseconds, delimited by '.'.
Definition: Time.cc:45
Hypertable definitions
int64_t get_ts64()
Returns the current time in nanoseconds as a 64bit number.
Definition: Time.cc:40