Retrial of the first step to port file_util::CountFilesCreatedAfter()

Submitting http://codereview.chromium.org/75033 on behalf of hamaji


Review URL: http://codereview.chromium.org/67276

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13993 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 85786f935163dd4c46dc2931111189e8d119d4d8
diff --git a/base/file_util.h b/base/file_util.h
index 6c916dd..20d98b0 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -27,6 +27,10 @@
 #include "base/scoped_ptr.h"
 #include "base/file_path.h"
 
+namespace base {
+class Time;
+}
+
 namespace file_util {
 
 //-----------------------------------------------------------------------------
@@ -135,10 +139,9 @@
 
 #if defined(OS_WIN)
 // Returns the number of files matching the current path that were
-// created on or after the given FILETIME.  Doesn't count ".." or ".".
-// Filetime is UTC filetime, not LocalFiletime.
-int CountFilesCreatedAfter(const std::wstring& path,
-                           const FILETIME& file_time);
+// created on or after the given |file_time|.  Doesn't count ".." or ".".
+int CountFilesCreatedAfter(const FilePath& path,
+                           const base::Time& file_time);
 #endif  // defined(OS_WIN)
 
 // Deletes the given path, whether it's a file or a directory.
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 7f24fc5..02ddc0a 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -20,6 +20,7 @@
 #include "base/logging.h"
 #include "base/path_service.h"
 #include "base/string_util.h"
+#include "base/time.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "testing/platform_test.h"
 
@@ -316,30 +317,26 @@
 #if defined OS_WIN
 TEST_F(FileUtilTest, CountFilesCreatedAfter) {
   // Create old file (that we don't want to count)
-  FilePath old_file_name = test_dir_.Append(L"Old File.txt");
+  FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
   CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
 
   // Age to perfection
   Sleep(100);
 
   // Establish our cutoff time
-  FILETIME test_start_time;
-  GetSystemTimeAsFileTime(&test_start_time);
-  EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
-                                                 test_start_time));
+  base::Time now(base::Time::NowFromSystemTime());
+  EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
 
   // Create a new file (that we do want to count)
-  FilePath new_file_name = test_dir_.Append(L"New File.txt");
+  FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
   CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
 
   // We should see only the new file.
-  EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_.value(),
-                                                 test_start_time));
+  EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
 
   // Delete new file, we should see no files after cutoff now
   EXPECT_TRUE(file_util::Delete(new_file_name, false));
-  EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
-                                                 test_start_time));
+  EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
 }
 #endif
 
diff --git a/base/time.h b/base/time.h
index a2d145c..0625bfb 100644
--- a/base/time.h
+++ b/base/time.h
@@ -207,6 +207,12 @@
   // times are increasing, or that two calls to Now() won't be the same.
   static Time Now();
 
+  // Returns the current time. Same as Now() except that this function always
+  // uses system time so that there are no discrepancies between the returned
+  // time and system time even on virtual environments including our test bot.
+  // For timing sensitive unittests, this function should be used.
+  static Time NowFromSystemTime();
+
   // Converts to/from time_t in UTC and a Time class.
   // TODO(brettw) this should be removed once everybody starts using the |Time|
   // class.
diff --git a/base/time_posix.cc b/base/time_posix.cc
index 6885cad..8b04be9 100644
--- a/base/time_posix.cc
+++ b/base/time_posix.cc
@@ -41,6 +41,12 @@
 }
 
 // static
+Time Time::NowFromSystemTime() {
+  // Just use Now() because Now() returns the system time.
+  return Now();
+}
+
+// static
 Time Time::FromExploded(bool is_local, const Exploded& exploded) {
   struct tm timestruct;
   timestruct.tm_sec    = exploded.second;