]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/time/gadgets.cc
3ab1b87bcfcbbcfe8ce6396ee252e50c06232a19
2 * Copyright (C) 1996-2023 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 "time/gadgets.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
, nullptr);
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 tvSubUsec(struct timeval t1
, struct timeval t2
)
39 return (t2
.tv_sec
- t1
.tv_sec
) * 1000000 +
40 (t2
.tv_usec
- t1
.tv_usec
);
44 tvSubDsec(struct timeval t1
, struct timeval t2
)
46 return (double) (t2
.tv_sec
- t1
.tv_sec
) +
47 (double) (t2
.tv_usec
- t1
.tv_usec
) / 1000000.0;
51 tvSubMsec(struct timeval t1
, struct timeval t2
)
53 return (t2
.tv_sec
- t1
.tv_sec
) * 1000 +
54 (t2
.tv_usec
- t1
.tv_usec
) / 1000;
58 tvSub(struct timeval
&res
, struct timeval
const &t1
, struct timeval
const &t2
)
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
;
65 res
.tv_usec
= t2
.tv_usec
+ 1000000 - t1
.tv_usec
;
69 void tvAdd(struct timeval
&res
, struct timeval
const &t1
, struct timeval
const &t2
)
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) {
75 res
.tv_usec
-= 1000000;
79 void tvAssignAdd(struct timeval
&t
, struct timeval
const &add
)
81 t
.tv_sec
+= add
.tv_sec
;
82 t
.tv_usec
+= add
.tv_usec
;
83 if (t
.tv_usec
>= 1000000) {
90 operator <<(std::ostream
&os
, const timeval
&t
)
92 os
<< t
.tv_sec
<< ".";
93 const auto savedFill
= os
.fill('0');
94 os
<< std::setw(6) << t
.tv_usec
;
100 Time::FormatStrf(time_t t
)
103 static char buf
[128];
104 static time_t last_t
= 0;
108 strftime(buf
, 127, "%Y/%m/%d %H:%M:%S", tm
);
116 Time::FormatHttpd(time_t t
)
118 static char buf
[128];
119 static time_t last_t
= 0;
122 struct tm
*gmt
= gmtime(&t
);
125 int gmt_min
, gmt_hour
, gmt_yday
, day_offset
;
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
;
137 day_offset
= lt
->tm_yday
- gmt_yday
;
138 /* wrap round on end of year */
141 else if (day_offset
< -1)
144 min_offset
= day_offset
* 1440 + (lt
->tm_hour
- gmt_hour
) * 60
145 + (lt
->tm_min
- gmt_min
);
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,
153 strftime(buf
, 127, "%d/%b/%Y:%H:%M:%S -000", gmt
);