summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authormattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-15 20:37:28 +0000
committermattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-15 20:37:28 +0000
commitca929ed34121d8f90788f16dfd1fe555bb5701ae (patch)
treed4aedc59332c18e21c2368f9006fa92e1bf44b65 /crypto
parentb95b08d0da727df948ecbd8d3bebe4ecb09b97d5 (diff)
downloadchromium_src-ca929ed34121d8f90788f16dfd1fe555bb5701ae.zip
chromium_src-ca929ed34121d8f90788f16dfd1fe555bb5701ae.tar.gz
chromium_src-ca929ed34121d8f90788f16dfd1fe555bb5701ae.tar.bz2
Reland: Simplify PRTimeToBaseTime implementation, add BaseTimeToPRTime.
BUG=107047 TEST=crypto_unittests TBR=wtc@chromium.org Review URL: http://codereview.chromium.org/8956003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114693 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'crypto')
-rw-r--r--crypto/crypto.gyp2
-rw-r--r--crypto/nss_util.cc22
-rw-r--r--crypto/nss_util.h4
-rw-r--r--crypto/nss_util_unittest.cc40
4 files changed, 52 insertions, 16 deletions
diff --git a/crypto/crypto.gyp b/crypto/crypto.gyp
index fb3935f..2b76403 100644
--- a/crypto/crypto.gyp
+++ b/crypto/crypto.gyp
@@ -209,6 +209,7 @@
'ec_private_key_unittest.cc',
'encryptor_unittest.cc',
'hmac_unittest.cc',
+ 'nss_util_unittest.cc',
'p224_unittest.cc',
'p224_spake_unittest.cc',
'rsa_private_key_unittest.cc',
@@ -253,6 +254,7 @@
}],
[ 'use_openssl==1', {
'sources!': [
+ 'nss_util_unittest.cc',
'openpgp_symmetric_encryption_unittest.cc',
'rsa_private_key_nss_unittest.cc',
],
diff --git a/crypto/nss_util.cc b/crypto/nss_util.cc
index 53602c3..2540c0c 100644
--- a/crypto/nss_util.cc
+++ b/crypto/nss_util.cc
@@ -769,23 +769,13 @@ SymmetricKey* GetSupplementalUserKey() {
}
#endif // defined(OS_CHROMEOS)
-// TODO(port): Implement this more simply. We can convert by subtracting an
-// offset (the difference between NSPR's and base::Time's epochs).
base::Time PRTimeToBaseTime(PRTime prtime) {
- PRExplodedTime prxtime;
- PR_ExplodeTime(prtime, PR_GMTParameters, &prxtime);
-
- base::Time::Exploded exploded;
- exploded.year = prxtime.tm_year;
- exploded.month = prxtime.tm_month + 1;
- exploded.day_of_week = prxtime.tm_wday;
- exploded.day_of_month = prxtime.tm_mday;
- exploded.hour = prxtime.tm_hour;
- exploded.minute = prxtime.tm_min;
- exploded.second = prxtime.tm_sec;
- exploded.millisecond = prxtime.tm_usec / 1000;
-
- return base::Time::FromUTCExploded(exploded);
+ return base::Time::FromInternalValue(
+ prtime + base::Time::UnixEpoch().ToInternalValue());
+}
+
+PRTime BaseTimeToPRTime(base::Time time) {
+ return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue();
}
PK11SlotInfo* GetPublicNSSKeySlot() {
diff --git a/crypto/nss_util.h b/crypto/nss_util.h
index a617224..746691d 100644
--- a/crypto/nss_util.h
+++ b/crypto/nss_util.h
@@ -149,6 +149,10 @@ CRYPTO_EXPORT SymmetricKey* GetSupplementalUserKey();
// We use a int64 instead of PRTime here to avoid depending on NSPR headers.
CRYPTO_EXPORT base::Time PRTimeToBaseTime(int64 prtime);
+// Convert a base::Time object into a PRTime value.
+// We use a int64 instead of PRTime here to avoid depending on NSPR headers.
+CRYPTO_EXPORT int64 BaseTimeToPRTime(base::Time time);
+
#if defined(USE_NSS)
// Exposed for unittests only. |path| should be an existing directory under
// which the DB files will be placed. |description| is a user-visible name for
diff --git a/crypto/nss_util_unittest.cc b/crypto/nss_util_unittest.cc
new file mode 100644
index 0000000..cc24b61
--- /dev/null
+++ b/crypto/nss_util_unittest.cc
@@ -0,0 +1,40 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "crypto/nss_util.h"
+
+#include <prtime.h>
+
+#include "base/time.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace crypto {
+
+TEST(NSSUtilTest, PRTimeConversion) {
+ EXPECT_EQ(base::Time::UnixEpoch(), PRTimeToBaseTime(0));
+ EXPECT_EQ(0, BaseTimeToPRTime(base::Time::UnixEpoch()));
+
+ PRExplodedTime prxtime;
+ prxtime.tm_params.tp_gmt_offset = 0;
+ prxtime.tm_params.tp_dst_offset = 0;
+ base::Time::Exploded exploded;
+ prxtime.tm_year = exploded.year = 2011;
+ exploded.month = 12;
+ prxtime.tm_month = 11;
+ prxtime.tm_wday = exploded.day_of_week = 0; // Should be unusued.
+ prxtime.tm_mday = exploded.day_of_month = 10;
+ prxtime.tm_hour = exploded.hour = 2;
+ prxtime.tm_min = exploded.minute = 52;
+ prxtime.tm_sec = exploded.second = 19;
+ exploded.millisecond = 342;
+ prxtime.tm_usec = 342000;
+
+ PRTime pr_time = PR_ImplodeTime(&prxtime);
+ base::Time base_time = base::Time::FromUTCExploded(exploded);
+
+ EXPECT_EQ(base_time, PRTimeToBaseTime(pr_time));
+ EXPECT_EQ(pr_time, BaseTimeToPRTime(base_time));
+}
+
+} // namespace crypto