]> git.ipfire.org Git - thirdparty/squid.git/blob - src/time.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / time.cc
1 /*
2 * Copyright (C) 1996-2015 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 "SquidTime.h"
13
14 struct timeval current_time;
15 double current_dtime;
16 time_t squid_curtime = 0;
17
18 time_t
19 getCurrentTime(void)
20 {
21 #if GETTIMEOFDAY_NO_TZP
22 gettimeofday(&current_time);
23 #else
24
25 gettimeofday(&current_time, NULL);
26 #endif
27
28 current_dtime = (double) current_time.tv_sec +
29 (double) current_time.tv_usec / 1000000.0;
30 return squid_curtime = current_time.tv_sec;
31 }
32
33 int
34 tvSubMsec(struct timeval t1, struct timeval t2)
35 {
36 return (t2.tv_sec - t1.tv_sec) * 1000 +
37 (t2.tv_usec - t1.tv_usec) / 1000;
38 }
39
40 void
41 tvSub(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
42 {
43 res.tv_sec = t2.tv_sec - t1.tv_sec;
44 if (t2.tv_usec >= t1.tv_usec)
45 res.tv_usec = t2.tv_usec - t1.tv_usec;
46 else {
47 res.tv_sec -= 1;
48 res.tv_usec = t2.tv_usec + 1000000 - t1.tv_usec;
49 }
50 }
51
52 void tvAdd(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
53 {
54 res.tv_sec = t1.tv_sec + t2.tv_sec;
55 res.tv_usec = t1.tv_usec + t2.tv_usec;
56 if (res.tv_usec >= 1000000) {
57 ++res.tv_sec;
58 res.tv_usec -= 1000000;
59 }
60 }
61
62 void tvAssignAdd(struct timeval &t, struct timeval const &add)
63 {
64 t.tv_sec += add.tv_sec;
65 t.tv_usec += add.tv_usec;
66 if (t.tv_usec >= 1000000) {
67 ++t.tv_sec;
68 t.tv_usec -= 1000000;
69 }
70 }
71
72 TimeEngine::~TimeEngine()
73 {}
74
75 void
76 TimeEngine::tick()
77 {
78 getCurrentTime();
79 }
80
81 const char *
82 Time::FormatStrf(time_t t)
83 {
84 struct tm *tm;
85 static char buf[128];
86 static time_t last_t = 0;
87
88 if (t != last_t) {
89 tm = localtime(&t);
90 strftime(buf, 127, "%Y/%m/%d %H:%M:%S", tm);
91 last_t = t;
92 }
93
94 return buf;
95 }
96
97 const char *
98 Time::FormatHttpd(time_t t)
99 {
100 static char buf[128];
101 static time_t last_t = 0;
102
103 if (t != last_t) {
104 struct tm *gmt = gmtime(&t);
105
106 #if !USE_GMT
107 int gmt_min, gmt_hour, gmt_yday, day_offset;
108 size_t len;
109 struct tm *lt;
110 int min_offset;
111
112 /* localtime & gmtime may use the same static data */
113 gmt_min = gmt->tm_min;
114 gmt_hour = gmt->tm_hour;
115 gmt_yday = gmt->tm_yday;
116
117 lt = localtime(&t);
118
119 day_offset = lt->tm_yday - gmt_yday;
120 /* wrap round on end of year */
121 if (day_offset > 1)
122 day_offset = -1;
123 else if (day_offset < -1)
124 day_offset = 1;
125
126 min_offset = day_offset * 1440 + (lt->tm_hour - gmt_hour) * 60
127 + (lt->tm_min - gmt_min);
128
129 len = strftime(buf, 127 - 5, "%d/%b/%Y:%H:%M:%S ", lt);
130 snprintf(buf + len, 128 - len, "%+03d%02d",
131 (min_offset / 60) % 24,
132 min_offset % 60);
133 #else /* USE_GMT */
134 buf[0] = '\0';
135 strftime(buf, 127, "%d/%b/%Y:%H:%M:%S -000", gmt);
136 #endif /* USE_GMT */
137
138 last_t = t;
139 }
140
141 return buf;
142 }
143