time.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/sync/base/time.h"
  5. #include <memory>
  6. #include "base/check.h"
  7. #include "base/i18n/unicodestring.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "third_party/icu/source/common/unicode/utypes.h"
  10. #include "third_party/icu/source/i18n/unicode/smpdtfmt.h"
  11. namespace syncer {
  12. int64_t TimeToProtoTime(const base::Time& t) {
  13. return (t - base::Time::UnixEpoch()).InMilliseconds();
  14. }
  15. base::Time ProtoTimeToTime(int64_t proto_t) {
  16. return base::Time::UnixEpoch() + base::Milliseconds(proto_t);
  17. }
  18. std::string GetTimeDebugString(const base::Time& t) {
  19. // Note: We don't use some helper from base/i18n/time_formatting.h here,
  20. // because those are all locale-dependent which we explicitly don't want.
  21. UErrorCode status = U_ZERO_ERROR;
  22. icu::SimpleDateFormat formatter(icu::UnicodeString("yyyy-MM-dd HH:mm:ss X"),
  23. status);
  24. DCHECK(U_SUCCESS(status));
  25. icu::UnicodeString date_string;
  26. formatter.format(static_cast<UDate>(t.ToDoubleT() * 1000), date_string);
  27. return base::UTF16ToUTF8(base::i18n::UnicodeStringToString16(date_string));
  28. }
  29. } // namespace syncer