]> git.ipfire.org Git - thirdparty/squid.git/blame - src/time.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / time.cc
CommitLineData
c21ad0f5 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
c21ad0f5 3 *
bbc27441
AJ
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.
c21ad0f5 7 */
bbc27441
AJ
8
9/* DEBUG: section 21 Time Functions */
10
f7f3304a 11#include "squid.h"
985c86bc 12#include "SquidTime.h"
c21ad0f5 13
cc192b50 14struct timeval current_time;
15double current_dtime;
985c86bc 16time_t squid_curtime = 0;
c21ad0f5 17
18time_t
19getCurrentTime(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}
8ff3fa2e 32
25f98340
AJ
33int
34tvSubMsec(struct timeval t1, struct timeval t2)
35{
36 return (t2.tv_sec - t1.tv_sec) * 1000 +
37 (t2.tv_usec - t1.tv_usec) / 1000;
8236d34f 38}
25f98340 39
01bd87d8
CT
40void
41tvSub(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
52void 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
62void 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
8ff3fa2e 72TimeEngine::~TimeEngine()
73{}
74
75void
76TimeEngine::tick()
77{
78 getCurrentTime();
79}
20efa1c2
AJ
80
81const char *
82Time::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
97const char *
98Time::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}
f53969cc 143