CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmFileTime.cxx
blob3d103d3785cb66e7029b5826d7edfe48227925ef
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmFileTime.h"
5 #include <string>
7 // Use a platform-specific API to get file times efficiently.
8 #if !defined(_WIN32) || defined(__CYGWIN__)
9 # include "cm_sys_stat.h"
10 #else
11 # include <windows.h>
13 # include "cmsys/Encoding.hxx"
14 #endif
16 bool cmFileTime::Load(std::string const& fileName)
18 #if !defined(_WIN32) || defined(__CYGWIN__)
19 // POSIX version. Use the stat function.
20 struct stat fst;
21 if (::stat(fileName.c_str(), &fst) != 0) {
22 return false;
24 # if CMake_STAT_HAS_ST_MTIM
25 // Nanosecond resolution
26 this->Time = fst.st_mtim.tv_sec * UtPerS + fst.st_mtim.tv_nsec;
27 # elif CMake_STAT_HAS_ST_MTIMESPEC
28 // Nanosecond resolution
29 this->Time = fst.st_mtimespec.tv_sec * UtPerS + fst.st_mtimespec.tv_nsec;
30 # else
31 // Second resolution
32 this->Time = fst.st_mtime * UtPerS;
33 # endif
34 #else
35 // Windows version. Get the modification time from extended file attributes.
36 WIN32_FILE_ATTRIBUTE_DATA fdata;
37 if (!GetFileAttributesExW(cmsys::Encoding::ToWide(fileName).c_str(),
38 GetFileExInfoStandard, &fdata)) {
39 return false;
42 // Copy the file time to the output location.
43 using uint64 = unsigned long long;
45 this->Time = static_cast<TimeType>(
46 (uint64(fdata.ftLastWriteTime.dwHighDateTime) << 32) +
47 fdata.ftLastWriteTime.dwLowDateTime);
48 #endif
49 return true;