0

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
This commit is contained in:
mattm@chromium.org
2011-12-15 20:37:28 +00:00
parent b95b08d0da
commit ca929ed341
4 changed files with 51 additions and 15 deletions

@ -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',
],

@ -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);
return base::Time::FromInternalValue(
prtime + base::Time::UnixEpoch().ToInternalValue());
}
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);
PRTime BaseTimeToPRTime(base::Time time) {
return time.ToInternalValue() - base::Time::UnixEpoch().ToInternalValue();
}
PK11SlotInfo* GetPublicNSSKeySlot() {

@ -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

@ -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