[gutil] remove unused code from walltime.{h,cc}
While working on the `kudu diagnose parse_metrics` CLI tool, I noticed
some functions in walltime were unused. With that and questioning
why to re-implement libc's functionality at all, I removed the unused
code: WallTime_Parse_Timezone() and a few other local functions.
There are no functional changes in this patch.
Change-Id: Ieddb6d34c047450f4e5cf445d290e965580c4ed5
Reviewed-on: http://gerrit.cloudera.org:8080/18972
Tested-by: Kudu Jenkins
Reviewed-by: Yifan Zhang <chinazhangyifan@163.com>
diff --git a/src/kudu/gutil/walltime.cc b/src/kudu/gutil/walltime.cc
index c577191..e9afe78 100644
--- a/src/kudu/gutil/walltime.cc
+++ b/src/kudu/gutil/walltime.cc
@@ -26,9 +26,6 @@
#include "kudu/gutil/walltime.h"
-#include <stdio.h>
-#include <string.h>
-
#if defined(__APPLE__)
#include <mach/clock.h>
#include <mach/mach.h>
@@ -47,16 +44,6 @@
} // namespace walltime_internal
#endif
-// This is exactly like mktime() except it is guaranteed to return -1 on
-// failure. Some versions of glibc allow mktime() to return negative
-// values which the standard says are undefined. See the standard at
-// http://www.opengroup.org/onlinepubs/007904875/basedefs/xbd_chap04.html
-// under the heading "Seconds Since the Epoch".
-static inline time_t gmktime(struct tm *tm) {
- time_t rt = mktime(tm);
- return rt < 0 ? time_t(-1) : rt;
-}
-
static void StringAppendStrftime(std::string* dst,
const char* format,
const struct tm* tm) {
@@ -90,87 +77,6 @@
return;
}
-// Convert a "struct tm" interpreted as *GMT* into a time_t (technically
-// a long since we can't include header files in header files bla bla bla).
-// This is basically filling a hole in the standard library.
-//
-// There are several approaches to mkgmtime() implementation on the net,
-// many of them wrong. Simply reimplementing the logic seems to be the
-// simplest and most efficient, though it does reimplement calendar logic.
-// The calculation is mostly straightforward; leap years are the main issue.
-//
-// Like gmktime() this method returns -1 on failure. Negative results
-// are considered undefined by the standard so these cases are
-// considered failures and thus return -1.
-time_t mkgmtime(const struct tm *tm) {
- // Month-to-day offset for non-leap-years.
- static const int month_day[12] =
- {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
-
- // Most of the calculation is easy; leap years are the main difficulty.
- int month = tm->tm_mon % 12;
- int year = tm->tm_year + tm->tm_mon / 12;
- if (month < 0) { // Negative values % 12 are still negative.
- month += 12;
- --year;
- }
-
- // This is the number of Februaries since 1900.
- const int year_for_leap = (month > 1) ? year + 1 : year;
-
- time_t rt = tm->tm_sec // Seconds
- + 60 * (tm->tm_min // Minute = 60 seconds
- + 60 * (tm->tm_hour // Hour = 60 minutes
- + 24 * (month_day[month] + tm->tm_mday - 1 // Day = 24 hours
- + 365 * (year - 70) // Year = 365 days
- + (year_for_leap - 69) / 4 // Every 4 years is leap...
- - (year_for_leap - 1) / 100 // Except centuries...
- + (year_for_leap + 299) / 400))); // Except 400s.
- return rt < 0 ? -1 : rt;
-}
-
-bool WallTime_Parse_Timezone(const char* time_spec,
- const char* format,
- const struct tm* default_time,
- bool local,
- WallTime* result) {
- struct tm split_time;
- if (default_time) {
- split_time = *default_time;
- } else {
- memset(&split_time, 0, sizeof(split_time));
- }
- const char* parsed = strptime(time_spec, format, &split_time);
- if (parsed == nullptr) return false;
-
- // If format ends with "%S", match fractional seconds
- double fraction = 0.0;
- char junk;
- if ((*parsed == '.') &&
- (strcmp(format + strlen(format) - 2, "%S") == 0) &&
- (sscanf(parsed, "%lf%c", // NOLINT(runtime/printf)
- &fraction, &junk) == 1)) {
- parsed = format + strlen(format); // Parsed it all!
- }
- if (*parsed != '\0') return false;
-
- // Convert into seconds since epoch. Adjust so it is interpreted
- // w.r.t. the daylight-saving-state at the specified time.
- split_time.tm_isdst = -1; // Ask gmktime() to find dst imfo
- time_t ptime;
- if (local) {
- ptime = gmktime(&split_time);
- } else {
- ptime = mkgmtime(&split_time); // Returns time in GMT instead of local.
- }
-
- if (ptime == -1) return false;
-
- *result = ptime;
- *result += fraction;
- return true;
-}
-
WallTime WallTime_Now() {
#if defined(__APPLE__)
mach_timespec_t ts;
diff --git a/src/kudu/gutil/walltime.h b/src/kudu/gutil/walltime.h
index 1850973..93a9536 100644
--- a/src/kudu/gutil/walltime.h
+++ b/src/kudu/gutil/walltime.h
@@ -16,10 +16,7 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
-#ifndef GUTIL_WALLTIME_H_
-#define GUTIL_WALLTIME_H_
-
-#include <sys/time.h>
+#pragma once
#include <ctime>
#include <string>
@@ -52,16 +49,6 @@
// Return the local time as a string suitable for user display.
std::string LocalTimeAsString();
-// Similar to the WallTime_Parse, but it takes a boolean flag local as
-// argument specifying if the time_spec is in local time or UTC
-// time. If local is set to true, the same exact result as
-// WallTime_Parse is returned.
-bool WallTime_Parse_Timezone(const char* time_spec,
- const char* format,
- const struct tm* default_time,
- bool local,
- WallTime* result);
-
// Return current time in seconds as a WallTime.
WallTime WallTime_Now();
@@ -213,4 +200,3 @@
// inline method bodies
#include "kudu/gutil/cycleclock-inl.h" // IWYU pragma: export
-#endif // GUTIL_WALLTIME_H_