]> git.ipfire.org Git - thirdparty/squid.git/blob - src/time.cc
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / time.cc
1 /*
2 * Copyright (C) 1996-2014 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 TimeEngine::~TimeEngine()
41 {}
42
43 void
44 TimeEngine::tick()
45 {
46 getCurrentTime();
47 }
48
49 const char *
50 Time::FormatStrf(time_t t)
51 {
52 struct tm *tm;
53 static char buf[128];
54 static time_t last_t = 0;
55
56 if (t != last_t) {
57 tm = localtime(&t);
58 strftime(buf, 127, "%Y/%m/%d %H:%M:%S", tm);
59 last_t = t;
60 }
61
62 return buf;
63 }
64
65 const char *
66 Time::FormatHttpd(time_t t)
67 {
68 static char buf[128];
69 static time_t last_t = 0;
70
71 if (t != last_t) {
72 struct tm *gmt = gmtime(&t);
73
74 #if !USE_GMT
75 int gmt_min, gmt_hour, gmt_yday, day_offset;
76 size_t len;
77 struct tm *lt;
78 int min_offset;
79
80 /* localtime & gmtime may use the same static data */
81 gmt_min = gmt->tm_min;
82 gmt_hour = gmt->tm_hour;
83 gmt_yday = gmt->tm_yday;
84
85 lt = localtime(&t);
86
87 day_offset = lt->tm_yday - gmt_yday;
88 /* wrap round on end of year */
89 if (day_offset > 1)
90 day_offset = -1;
91 else if (day_offset < -1)
92 day_offset = 1;
93
94 min_offset = day_offset * 1440 + (lt->tm_hour - gmt_hour) * 60
95 + (lt->tm_min - gmt_min);
96
97 len = strftime(buf, 127 - 5, "%d/%b/%Y:%H:%M:%S ", lt);
98 snprintf(buf + len, 128 - len, "%+03d%02d",
99 (min_offset / 60) % 24,
100 min_offset % 60);
101 #else /* USE_GMT */
102 buf[0] = '\0';
103 strftime(buf, 127, "%d/%b/%Y:%H:%M:%S -000", gmt);
104 #endif /* USE_GMT */
105
106 last_t = t;
107 }
108
109 return buf;
110 }