diff options
Diffstat (limited to 'src/google/protobuf/util/time_util.cc')
-rw-r--r-- | src/google/protobuf/util/time_util.cc | 46 |
1 files changed, 13 insertions, 33 deletions
diff --git a/src/google/protobuf/util/time_util.cc b/src/google/protobuf/util/time_util.cc index c782d691..d4912837 100644 --- a/src/google/protobuf/util/time_util.cc +++ b/src/google/protobuf/util/time_util.cc @@ -30,13 +30,14 @@ #include <google/protobuf/util/time_util.h> -#include <google/protobuf/stubs/time.h> #include <google/protobuf/stubs/int128.h> -#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/stringprintf.h> +#include <google/protobuf/stubs/strutil.h> +#include <google/protobuf/stubs/time.h> #include <google/protobuf/duration.pb.h> #include <google/protobuf/timestamp.pb.h> + namespace google { namespace protobuf { namespace util { @@ -49,11 +50,9 @@ static const int kNanosPerSecond = 1000000000; static const int kMicrosPerSecond = 1000000; static const int kMillisPerSecond = 1000; static const int kNanosPerMillisecond = 1000000; -static const int kMicrosPerMillisecond = 1000; static const int kNanosPerMicrosecond = 1000; static const int kSecondsPerMinute = 60; // Note that we ignore leap seconds. static const int kSecondsPerHour = 3600; -static const char kTimestampFormat[] = "%E4Y-%m-%dT%H:%M:%S"; template <typename T> T CreateNormalized(int64 seconds, int64 nanos); @@ -142,6 +141,15 @@ int64 RoundTowardZero(int64 value, int64 divider) { } } // namespace +// Actually define these static const integers. Required by C++ standard (but +// some compilers don't like it). +#ifndef _MSC_VER +const int64 TimeUtil::kTimestampMinSeconds; +const int64 TimeUtil::kTimestampMaxSeconds; +const int64 TimeUtil::kDurationMaxSeconds; +const int64 TimeUtil::kDurationMinSeconds; +#endif // !_MSC_VER + string TimeUtil::ToString(const Timestamp& timestamp) { return FormatTime(timestamp.seconds(), timestamp.nanos()); } @@ -174,7 +182,7 @@ string TimeUtil::ToString(const Duration& duration) { seconds = -seconds; nanos = -nanos; } - result += StringPrintf("%" GOOGLE_LL_FORMAT "d", seconds); + result += SimpleItoa(seconds); if (nanos != 0) { result += "." + FormatNanos(nanos); } @@ -367,19 +375,6 @@ namespace { using google::protobuf::util::kNanosPerSecond; using google::protobuf::util::CreateNormalized; -// Convert a Timestamp to uint128. -void ToUint128(const Timestamp& value, uint128* result, bool* negative) { - if (value.seconds() < 0) { - *negative = true; - *result = static_cast<uint64>(-value.seconds()); - *result = *result * kNanosPerSecond - static_cast<uint32>(value.nanos()); - } else { - *negative = false; - *result = static_cast<uint64>(value.seconds()); - *result = *result * kNanosPerSecond + static_cast<uint32>(value.nanos()); - } -} - // Convert a Duration to uint128. void ToUint128(const Duration& value, uint128* result, bool* negative) { if (value.seconds() < 0 || value.nanos() < 0) { @@ -393,21 +388,6 @@ void ToUint128(const Duration& value, uint128* result, bool* negative) { } } -void ToTimestamp(const uint128& value, bool negative, Timestamp* timestamp) { - int64 seconds = static_cast<int64>(Uint128Low64(value / kNanosPerSecond)); - int32 nanos = static_cast<int32>(Uint128Low64(value % kNanosPerSecond)); - if (negative) { - seconds = -seconds; - nanos = -nanos; - if (nanos < 0) { - nanos += kNanosPerSecond; - seconds -= 1; - } - } - timestamp->set_seconds(seconds); - timestamp->set_nanos(nanos); -} - void ToDuration(const uint128& value, bool negative, Duration* duration) { int64 seconds = static_cast<int64>(Uint128Low64(value / kNanosPerSecond)); int32 nanos = static_cast<int32>(Uint128Low64(value % kNanosPerSecond)); |