Line data Source code
1 : // Copyright (c) 2012-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/eventdispatcher
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program 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 St, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : /** \file
21 : * \brief Various helper functions.
22 : *
23 : * These functions are useful for our event dispatcher environment.
24 : */
25 :
26 :
27 : // self
28 : //
29 : #include "eventdispatcher/utils.h"
30 :
31 : #include "eventdispatcher/exception.h"
32 :
33 :
34 : // snaplogger lib
35 : //
36 : #include <snaplogger/message.h>
37 :
38 :
39 : // snapdev lib
40 : //
41 : #include <snapdev/not_reached.h>
42 :
43 :
44 : // C++ lib
45 : //
46 : #include <cstring>
47 :
48 :
49 : // last include
50 : //
51 : #include <snapdev/poison.h>
52 :
53 :
54 :
55 : namespace ed
56 : {
57 :
58 :
59 :
60 : /** \brief Get the current date.
61 : *
62 : * This function retrieves the current date and time with a precision
63 : * of microseconds.
64 : *
65 : * \todo
66 : * This is also defined in snap_child::get_current_date() so we should
67 : * unify that in some way...
68 : *
69 : * \return The time in microseconds.
70 : */
71 10 : std::int64_t get_current_date()
72 : {
73 10 : timeval tv;
74 10 : if(gettimeofday(&tv, nullptr) != 0)
75 : {
76 0 : int const err(errno);
77 0 : SNAP_LOG_FATAL
78 0 : << "gettimeofday() failed with errno: "
79 : << err
80 : << " ("
81 0 : << strerror(err)
82 : << ")"
83 : << SNAP_LOG_SEND;
84 0 : throw event_dispatcher_runtime_error("gettimeofday() failed");
85 : }
86 :
87 10 : return static_cast<int64_t>(tv.tv_sec) * static_cast<int64_t>(1000000)
88 10 : + static_cast<int64_t>(tv.tv_usec);
89 : }
90 :
91 :
92 :
93 : /** \brief Get the current date.
94 : *
95 : * This function retrieves the current date and time with a precision
96 : * of nanoseconds.
97 : *
98 : * \return The time in nanoseconds.
99 : */
100 0 : std::int64_t get_current_date_ns()
101 : {
102 0 : timespec ts;
103 0 : if(clock_gettime(CLOCK_REALTIME_COARSE, &ts) != 0)
104 : {
105 0 : int const err(errno);
106 0 : SNAP_LOG_FATAL
107 0 : << "clock_gettime() failed with errno: "
108 : << err
109 : << " ("
110 0 : << strerror(err)
111 : << ")"
112 : << SNAP_LOG_SEND;
113 0 : throw event_dispatcher_runtime_error("clock_gettime() failed");
114 : }
115 :
116 0 : return static_cast<int64_t>(ts.tv_sec) * static_cast<int64_t>(1000000000)
117 0 : + static_cast<int64_t>(ts.tv_nsec);
118 : }
119 :
120 :
121 :
122 :
123 6 : } // namespace ed
124 : // vim: ts=4 sw=4 et
|