time.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 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 #include <iomanip>
15 #include <ostream>
16 
17 struct timeval current_time;
19 time_t squid_curtime = 0;
20 
21 time_t
23 {
24 #if GETTIMEOFDAY_NO_TZP
25  gettimeofday(&current_time);
26 #else
27 
28  gettimeofday(&current_time, NULL);
29 #endif
30 
31  current_dtime = (double) current_time.tv_sec +
32  (double) current_time.tv_usec / 1000000.0;
33  return squid_curtime = current_time.tv_sec;
34 }
35 
36 int
37 tvSubMsec(struct timeval t1, struct timeval t2)
38 {
39  return (t2.tv_sec - t1.tv_sec) * 1000 +
40  (t2.tv_usec - t1.tv_usec) / 1000;
41 }
42 
43 void
44 tvSub(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
45 {
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;
49  else {
50  res.tv_sec -= 1;
51  res.tv_usec = t2.tv_usec + 1000000 - t1.tv_usec;
52  }
53 }
54 
55 void tvAdd(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
56 {
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) {
60  ++res.tv_sec;
61  res.tv_usec -= 1000000;
62  }
63 }
64 
65 void tvAssignAdd(struct timeval &t, struct timeval const &add)
66 {
67  t.tv_sec += add.tv_sec;
68  t.tv_usec += add.tv_usec;
69  if (t.tv_usec >= 1000000) {
70  ++t.tv_sec;
71  t.tv_usec -= 1000000;
72  }
73 }
74 
76 {}
77 
78 void
80 {
82 }
83 
84 std::ostream &
85 operator <<(std::ostream &os, const timeval &t)
86 {
87  os << t.tv_sec << ".";
88  const auto savedFill = os.fill('0');
89  os << std::setw(6) << t.tv_usec;
90  os.fill(savedFill);
91  return os;
92 }
93 
94 const char *
96 {
97  struct tm *tm;
98  static char buf[128];
99  static time_t last_t = 0;
100 
101  if (t != last_t) {
102  tm = localtime(&t);
103  strftime(buf, 127, "%Y/%m/%d %H:%M:%S", tm);
104  last_t = t;
105  }
106 
107  return buf;
108 }
109 
110 const char *
112 {
113  static char buf[128];
114  static time_t last_t = 0;
115 
116  if (t != last_t) {
117  struct tm *gmt = gmtime(&t);
118 
119 #if !USE_GMT
120  int gmt_min, gmt_hour, gmt_yday, day_offset;
121  size_t len;
122  struct tm *lt;
123  int min_offset;
124 
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;
129 
130  lt = localtime(&t);
131 
132  day_offset = lt->tm_yday - gmt_yday;
133  /* wrap round on end of year */
134  if (day_offset > 1)
135  day_offset = -1;
136  else if (day_offset < -1)
137  day_offset = 1;
138 
139  min_offset = day_offset * 1440 + (lt->tm_hour - gmt_hour) * 60
140  + (lt->tm_min - gmt_min);
141 
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,
145  min_offset % 60);
146 #else /* USE_GMT */
147  buf[0] = '\0';
148  strftime(buf, 127, "%d/%b/%Y:%H:%M:%S -000", gmt);
149 #endif /* USE_GMT */
150 
151  last_t = t;
152  }
153 
154  return buf;
155 }
156 
double current_dtime
Definition: time.cc:18
void tvAdd(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
Definition: time.cc:55
time_t getCurrentTime(void)
Get current time.
Definition: time.cc:22
#define NULL
Definition: types.h:166
time_t squid_curtime
Definition: time.cc:19
int tvSubMsec(struct timeval t1, struct timeval t2)
Definition: time.cc:37
virtual ~TimeEngine()
Definition: time.cc:75
const char * FormatStrf(time_t t)
Definition: time.cc:95
void tvAssignAdd(struct timeval &t, struct timeval const &add)
Definition: time.cc:65
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
void tvSub(struct timeval &res, struct timeval const &t1, struct timeval const &t2)
Definition: time.cc:44
const char * FormatHttpd(time_t t)
Definition: time.cc:111
virtual void tick()
Definition: time.cc:79
struct timeval current_time
Definition: time.cc:17
std::ostream & operator<<(std::ostream &os, const timeval &t)
prints <seconds>.<microseconds>
Definition: time.cc:85
void const char * buf
Definition: stub_helper.cc:16

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors