]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/time.cc
51ae3db5c070093b439e0106bab75b5454581715
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 21 Time Functions */
12 #include "SquidTime.h"
17 struct timeval current_time
;
19 time_t squid_curtime
= 0;
24 #if GETTIMEOFDAY_NO_TZP
25 gettimeofday(¤t_time
);
28 gettimeofday(¤t_time
, NULL
);
31 current_dtime
= (double) current_time
.tv_sec
+
32 (double) current_time
.tv_usec
/ 1000000.0;
33 return squid_curtime
= current_time
.tv_sec
;
37 tvSubMsec(struct timeval t1
, struct timeval t2
)
39 return (t2
.tv_sec
- t1
.tv_sec
) * 1000 +
40 (t2
.tv_usec
- t1
.tv_usec
) / 1000;
44 tvSub(struct timeval
&res
, struct timeval
const &t1
, struct timeval
const &t2
)
46 res
.tv_sec
= t2
.tv_sec
- t1
.tv_sec
;
47 if (t2
.tv_usec
>= t1
.tv_usec
)
48 res
.tv_usec
= t2
.tv_usec
- t1
.tv_usec
;
51 res
.tv_usec
= t2
.tv_usec
+ 1000000 - t1
.tv_usec
;
55 void tvAdd(struct timeval
&res
, struct timeval
const &t1
, struct timeval
const &t2
)
57 res
.tv_sec
= t1
.tv_sec
+ t2
.tv_sec
;
58 res
.tv_usec
= t1
.tv_usec
+ t2
.tv_usec
;
59 if (res
.tv_usec
>= 1000000) {
61 res
.tv_usec
-= 1000000;
65 void tvAssignAdd(struct timeval
&t
, struct timeval
const &add
)
67 t
.tv_sec
+= add
.tv_sec
;
68 t
.tv_usec
+= add
.tv_usec
;
69 if (t
.tv_usec
>= 1000000) {
75 TimeEngine::~TimeEngine()
85 operator <<(std::ostream
&os
, const timeval
&t
)
87 os
<< t
.tv_sec
<< ".";
88 const auto savedFill
= os
.fill('0');
89 os
<< std::setw(6) << t
.tv_usec
;
95 Time::FormatStrf(time_t t
)
99 static time_t last_t
= 0;
103 strftime(buf
, 127, "%Y/%m/%d %H:%M:%S", tm
);
111 Time::FormatHttpd(time_t t
)
113 static char buf
[128];
114 static time_t last_t
= 0;
117 struct tm
*gmt
= gmtime(&t
);
120 int gmt_min
, gmt_hour
, gmt_yday
, day_offset
;
125 /* localtime & gmtime may use the same static data */
126 gmt_min
= gmt
->tm_min
;
127 gmt_hour
= gmt
->tm_hour
;
128 gmt_yday
= gmt
->tm_yday
;
132 day_offset
= lt
->tm_yday
- gmt_yday
;
133 /* wrap round on end of year */
136 else if (day_offset
< -1)
139 min_offset
= day_offset
* 1440 + (lt
->tm_hour
- gmt_hour
) * 60
140 + (lt
->tm_min
- gmt_min
);
142 len
= strftime(buf
, 127 - 5, "%d/%b/%Y:%H:%M:%S ", lt
);
143 snprintf(buf
+ len
, 128 - len
, "%+03d%02d",
144 (min_offset
/ 60) % 24,
148 strftime(buf
, 127, "%d/%b/%Y:%H:%M:%S -000", gmt
);