summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorpinkerton@google.com <pinkerton@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-11 23:07:19 +0000
committerpinkerton@google.com <pinkerton@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-11 23:07:19 +0000
commit0d6033c1c7a482b4152e365b0bf9fe82f446aa6f (patch)
treeb9ee477289ac3695a8d101ab2c12ca1e85a20481 /base
parent8bcab70c11559b92b87df23cf279e3f2e5229670 (diff)
downloadchromium_src-0d6033c1c7a482b4152e365b0bf9fe82f446aa6f.zip
chromium_src-0d6033c1c7a482b4152e365b0bf9fe82f446aa6f.tar.gz
chromium_src-0d6033c1c7a482b4152e365b0bf9fe82f446aa6f.tar.bz2
Fix unit tests for posix, fix up mac implementation to work correctly. Add prtime unit test to mac project.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@680 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/pr_time_test.cc25
-rw-r--r--base/third_party/nspr/prtime.cc46
2 files changed, 61 insertions, 10 deletions
diff --git a/base/pr_time_test.cc b/base/pr_time_test.cc
index e054164..f93b859 100644
--- a/base/pr_time_test.cc
+++ b/base/pr_time_test.cc
@@ -36,7 +36,11 @@
namespace {
+#if COMPILER_MSVC
const int64 kMicrosecondsPerSecond = 1000000i64;
+#else
+const int64 kMicrosecondsPerSecond = 1000000;
+#endif
// time_t representation of 15th Oct 2007 12:45:00 PDT
PRTime comparison_time_pdt = 1192477500 * kMicrosecondsPerSecond;
@@ -74,21 +78,28 @@ class PRTimeTest : public testing::Test {
// Tests the PR_ParseTimeString nspr helper function for
// a variety of time strings.
TEST_F(PRTimeTest, ParseTimeTest1) {
- //time_t current_time = 0;
- PRTime current_time = 0;
+ time_t current_time = 0;
time(&current_time);
+ const int BUFFER_SIZE = 64;
tm local_time = {0};
- localtime_s(&local_time,&current_time);
-
- char time_buf[MAX_PATH] = {0};
+#if defined(OS_WIN)
+ localtime_s(&local_time, &current_time);
+#elif defined(OS_POSIX)
+ localtime_r(&current_time, &local_time);
+#endif
+ char time_buf[BUFFER_SIZE] = {0};
+#if defined(OS_WIN)
asctime_s(time_buf, arraysize(time_buf), &local_time);
- current_time *= PR_USEC_PER_SEC;
+#elif defined(OS_POSIX)
+ asctime_r(&local_time, time_buf);
+#endif
+ PRTime current_time64 = static_cast<PRTime>(current_time) * PR_USEC_PER_SEC;
PRTime parsed_time = 0;
PRStatus result = PR_ParseTimeString(time_buf, PR_FALSE, &parsed_time);
EXPECT_EQ(PR_SUCCESS, result);
- EXPECT_EQ(current_time,parsed_time);
+ EXPECT_EQ(current_time64, parsed_time);
}
TEST_F(PRTimeTest, ParseTimeTest2) {
diff --git a/base/third_party/nspr/prtime.cc b/base/third_party/nspr/prtime.cc
index a5e851c..b3d6db6 100644
--- a/base/third_party/nspr/prtime.cc
+++ b/base/third_party/nspr/prtime.cc
@@ -62,9 +62,22 @@
* 3. prlong.h
*/
+#include "base/third_party/nspr/prtime.h"
+#include "build/build_config.h"
+
+#if defined(OS_WIN)
#include <windows.h>
+#elif defined(OS_MACOSX)
+#include <CoreFoundation/CoreFoundation.h>
+#endif
#include <time.h>
-#include "base/third_party/nspr/prtime.h"
+
+/* Implements the Unix localtime_r() function for windows */
+#if defined(OS_WIN)
+static void localtime_r(const time_t* secs, struct tm* time) {
+ (void) localtime_s(time, secs);
+}
+#endif
/*
*------------------------------------------------------------------------
@@ -79,6 +92,7 @@
PRTime
PR_ImplodeTime(const PRExplodedTime *exploded)
{
+#if defined(OS_WIN)
// Create the system struct representing our exploded time.
SYSTEMTIME st = {0};
FILETIME ft = {0};
@@ -108,6 +122,32 @@ PR_ImplodeTime(const PRExplodedTime *exploded)
uli.QuadPart -= 116444736000000000i64; // from Windows epoch to NSPR epoch
uli.QuadPart /= 10; // from 100-nanosecond to microsecond
return (PRTime)uli.QuadPart;
+#elif defined(OS_MACOSX)
+ // Create the system struct representing our exploded time.
+ CFGregorianDate gregorian_date;
+ gregorian_date.year = exploded->tm_year;
+ gregorian_date.month = exploded->tm_month + 1;
+ gregorian_date.day = exploded->tm_mday;
+ gregorian_date.hour = exploded->tm_hour;
+ gregorian_date.minute = exploded->tm_min;
+ gregorian_date.second = exploded->tm_sec;
+
+ // Compute |absolute_time| in seconds, correct for gmt and dst
+ // (note the combined offset will be negative when we need to add it), then
+ // convert to microseconds which is what PRTime expects.
+ CFAbsoluteTime absolute_time =
+ CFGregorianDateGetAbsoluteTime(gregorian_date, NULL);
+ PRTime result = static_cast<PRTime>(absolute_time);
+ result -= exploded->tm_params.tp_gmt_offset +
+ exploded->tm_params.tp_dst_offset;
+ result += kCFAbsoluteTimeIntervalSince1970; // PRTime epoch is 1970
+ result *= 1000000L; // Seconds to microseconds
+ result += exploded->tm_usec;
+ return result;
+#else
+ NOTREACHED();
+ memset(time, 0, sizeof(*time));
+#endif
}
/*
@@ -824,7 +864,7 @@ PR_ParseTimeString(
zone_offset for the date we are parsing is the same as
the zone offset on 00:00:00 2 Jan 1970 GMT. */
secs = 86400;
- (void) localtime_s(&localTime, &secs);
+ localtime_r(&secs, &localTime);
zone_offset = localTime.tm_min
+ 60 * localTime.tm_hour
+ 1440 * (localTime.tm_mday - 2);
@@ -835,4 +875,4 @@ PR_ParseTimeString(
*result = PR_ImplodeTime(&tm);
return PR_SUCCESS;
-} \ No newline at end of file
+}