gadgets.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 21 Time Functions */
10
11#include "squid.h"
12#include "time/gadgets.h"
13
14#include <iomanip>
15#include <ostream>
16
17struct timeval current_time;
19time_t squid_curtime = 0;
20
21time_t
23{
24#if GETTIMEOFDAY_NO_TZP
25 gettimeofday(&current_time);
26#else
27
28 gettimeofday(&current_time, nullptr);
29#endif
30
32 (double) current_time.tv_usec / 1000000.0;
33 return squid_curtime = current_time.tv_sec;
34}
35
36int
37tvSubUsec(struct timeval t1, struct timeval t2)
38{
39 return (t2.tv_sec - t1.tv_sec) * 1000000 +
40 (t2.tv_usec - t1.tv_usec);
41}
42
43double
44tvSubDsec(struct timeval t1, struct timeval t2)
45{
46 return (double) (t2.tv_sec - t1.tv_sec) +
47 (double) (t2.tv_usec - t1.tv_usec) / 1000000.0;
48}
49
50int
51tvSubMsec(struct timeval t1, struct timeval t2)
52{
53 return (t2.tv_sec - t1.tv_sec) * 1000 +
54 (t2.tv_usec - t1.tv_usec) / 1000;
55}
56
57void
58tvSub(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
59{
60 res.tv_sec = t2.tv_sec - t1.tv_sec;
61 if (t2.tv_usec >= t1.tv_usec)
62 res.tv_usec = t2.tv_usec - t1.tv_usec;
63 else {
64 res.tv_sec -= 1;
65 res.tv_usec = t2.tv_usec + 1000000 - t1.tv_usec;
66 }
67}
68
69void tvAdd(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
70{
71 res.tv_sec = t1.tv_sec + t2.tv_sec;
72 res.tv_usec = t1.tv_usec + t2.tv_usec;
73 if (res.tv_usec >= 1000000) {
74 ++res.tv_sec;
75 res.tv_usec -= 1000000;
76 }
77}
78
79void tvAssignAdd(struct timeval &t, struct timeval const &add)
80{
81 t.tv_sec += add.tv_sec;
82 t.tv_usec += add.tv_usec;
83 if (t.tv_usec >= 1000000) {
84 ++t.tv_sec;
85 t.tv_usec -= 1000000;
86 }
87}
88
89std::ostream &
90operator <<(std::ostream &os, const timeval &t)
91{
92 os << t.tv_sec << ".";
93 const auto savedFill = os.fill('0');
94 os << std::setw(6) << t.tv_usec;
95 os.fill(savedFill);
96 return os;
97}
98
99const char *
101{
102 struct tm *tm;
103 static char buf[128];
104 static time_t last_t = 0;
105
106 if (t != last_t) {
107 tm = localtime(&t);
108 strftime(buf, 127, "%Y/%m/%d %H:%M:%S", tm);
109 last_t = t;
110 }
111
112 return buf;
113}
114
115const char *
117{
118 static char buf[128];
119 static time_t last_t = 0;
120
121 if (t != last_t) {
122 struct tm *gmt = gmtime(&t);
123
124#if !USE_GMT
125 int gmt_min, gmt_hour, gmt_yday, day_offset;
126 size_t len;
127 struct tm *lt;
128 int min_offset;
129
130 /* localtime & gmtime may use the same static data */
131 gmt_min = gmt->tm_min;
132 gmt_hour = gmt->tm_hour;
133 gmt_yday = gmt->tm_yday;
134
135 lt = localtime(&t);
136
137 day_offset = lt->tm_yday - gmt_yday;
138 /* wrap round on end of year */
139 if (day_offset > 1)
140 day_offset = -1;
141 else if (day_offset < -1)
142 day_offset = 1;
143
144 min_offset = day_offset * 1440 + (lt->tm_hour - gmt_hour) * 60
145 + (lt->tm_min - gmt_min);
146
147 len = strftime(buf, 127 - 5, "%d/%b/%Y:%H:%M:%S ", lt);
148 snprintf(buf + len, 128 - len, "%+03d%02d",
149 (min_offset / 60) % 24,
150 min_offset % 60);
151#else /* USE_GMT */
152 buf[0] = '\0';
153 strftime(buf, 127, "%d/%b/%Y:%H:%M:%S -000", gmt);
154#endif /* USE_GMT */
155
156 last_t = t;
157 }
158
159 return buf;
160}
161
const char * FormatStrf(time_t)
Definition: gadgets.cc:100
const char * FormatHttpd(time_t)
Definition: gadgets.cc:116
void EVH void double
Definition: stub_event.cc:16
void tvAdd(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
Definition: gadgets.cc:69
time_t squid_curtime
the current UNIX time in seconds
Definition: gadgets.cc:19
double tvSubDsec(struct timeval t1, struct timeval t2)
Definition: gadgets.cc:44
time_t getCurrentTime()
Definition: gadgets.cc:22
void tvSub(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
Definition: gadgets.cc:58
std::ostream & operator<<(std::ostream &os, const timeval &t)
prints <seconds>.<microseconds>
Definition: gadgets.cc:90
int tvSubUsec(struct timeval t1, struct timeval t2)
Definition: gadgets.cc:37
struct timeval current_time
the current UNIX time in timeval {seconds, microseconds} format
Definition: gadgets.cc:17
double current_dtime
the current UNIX time in seconds (with microsecond precision)
Definition: gadgets.cc:18
void tvAssignAdd(struct timeval &t, struct timeval const &add)
Definition: gadgets.cc:79
int tvSubMsec(struct timeval t1, struct timeval t2)
Definition: gadgets.cc:51

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors