Commit b32fb150 authored by derat@chromium.org's avatar derat@chromium.org

chromeos: Add gdata::util::FormatTimeAsString().

This adds a function for formatting a base::Time as an RFC
3339 date/time, e.g. "2012-07-03T12:30:34.318Z".  This
function complements the existing
gdata::util::GetTimeFromString(), and is needed for a later
contacts-related change.

BUG=128805
TEST=added


Review URL: https://chromiumcodereview.appspot.com/10790124

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147998 0039d316-1c4b-4281-b951-d872f2087c98
parent c447acda
......@@ -570,5 +570,14 @@ bool GetTimeFromString(const base::StringPiece& raw_value,
return true;
}
std::string FormatTimeAsString(const base::Time& time) {
base::Time::Exploded exploded;
time.UTCExplode(&exploded);
return base::StringPrintf(
"%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
exploded.year, exploded.month, exploded.day_of_month,
exploded.hour, exploded.minute, exploded.second, exploded.millisecond);
}
} // namespace util
} // namespace gdata
......@@ -11,6 +11,7 @@
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/platform_file.h"
#include "base/time.h"
#include "chrome/browser/chromeos/gdata/gdata_errorcode.h"
#include "googleurl/src/gurl.h"
......@@ -101,11 +102,14 @@ bool IsDriveV2ApiEnabled();
// Returns a PlatformFileError that corresponds to the GDataFileError provided.
base::PlatformFileError GDataFileErrorToPlatformError(GDataFileError error);
// Returns true when time string is successfully parsed and output as |time|.
// The time string must be in format yyyy-mm-ddThh:mm:ss.dddTZ (TZ is either
// '+hh:mm', '-hh:mm', 'Z' (representing UTC) or empty string).
// Parses an RFC 3339 date/time into a base::Time, returning true on success.
// The time string must be in the format "yyyy-mm-ddThh:mm:ss.dddTZ" (TZ is
// either '+hh:mm', '-hh:mm', 'Z' (representing UTC), or an empty string).
bool GetTimeFromString(const base::StringPiece& raw_value, base::Time* time);
// Formats a base::Time as an RFC 3339 date/time (in UTC).
std::string FormatTimeAsString(const base::Time& time);
} // namespace util
} // namespace gdata
......
......@@ -6,6 +6,7 @@
#include "base/file_path.h"
#include "base/i18n/time_formatting.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/system/timezone_settings.h"
......@@ -14,7 +15,6 @@
namespace gdata {
namespace util {
namespace {
std::string FormatTime(const base::Time& time) {
......@@ -171,5 +171,11 @@ TEST(GDataUtilTest, GetTimeFromString) {
FormatTime(test_time));
}
TEST(GDataUtilTest, FormatTimeAsString) {
base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123};
base::Time time = base::Time::FromUTCExploded(exploded_time);
EXPECT_EQ("2012-07-19T15:59:13.123Z", FormatTimeAsString(time));
}
} // namespace util
} // namespace gdata
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment