1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2012 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmTimestamp.h"
16 #include <sys/types.h>
19 //----------------------------------------------------------------------------
20 std::string cmTimestamp::CurrentTime(
21 const std::string& formatString, bool utcFlag)
23 time_t currentTimeT = time(0);
24 if(currentTimeT == time_t(-1))
29 return CreateTimestampFromTimeT(currentTimeT, formatString, utcFlag);
32 //----------------------------------------------------------------------------
33 std::string cmTimestamp::FileModificationTime(const char* path,
34 const std::string& formatString, bool utcFlag)
37 memset(&info, 0, sizeof(info));
39 if(stat(path, &info) != 0)
44 return CreateTimestampFromTimeT(info.st_mtime, formatString, utcFlag);
47 //----------------------------------------------------------------------------
48 std::string cmTimestamp::CreateTimestampFromTimeT(time_t timeT,
49 std::string formatString, bool utcFlag)
51 if(formatString.empty())
53 formatString = "%Y-%m-%dT%H:%M:%S";
61 memset(&timeStruct, 0, sizeof(timeStruct));
63 struct tm* ptr = (struct tm*) 0;
70 ptr = localtime(&timeT);
81 for(std::string::size_type i = 0; i < formatString.size(); ++i)
83 char c1 = formatString[i];
84 char c2 = (i+1 < formatString.size()) ?
85 formatString[i+1] : static_cast<char>(0);
87 if(c1 == '%' && c2 != 0)
89 result += AddTimestampComponent(c2, timeStruct);
101 //----------------------------------------------------------------------------
102 std::string cmTimestamp::AddTimestampComponent(
103 char flag, struct tm& timeStruct)
105 std::string formatString = "%";
106 formatString += flag;
130 size_t size = strftime(buffer, sizeof(buffer),
131 formatString.c_str(), &timeStruct);
133 return std::string(buffer, size);