packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmTimestamp.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2012 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
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"
13
14 #include <cstring>
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18
19 //----------------------------------------------------------------------------
20 std::string cmTimestamp::CurrentTime(
21   const std::string& formatString, bool utcFlag)
22 {
23   time_t currentTimeT = time(0);
24   if(currentTimeT == time_t(-1))
25     {
26     return std::string();
27     }
28
29   return CreateTimestampFromTimeT(currentTimeT, formatString, utcFlag);
30 }
31
32 //----------------------------------------------------------------------------
33 std::string cmTimestamp::FileModificationTime(const char* path,
34   const std::string& formatString, bool utcFlag)
35 {
36   struct stat info;
37   memset(&info, 0, sizeof(info));
38
39   if(stat(path, &info) != 0)
40     {
41     return std::string();
42     }
43
44   return CreateTimestampFromTimeT(info.st_mtime, formatString, utcFlag);
45 }
46
47 //----------------------------------------------------------------------------
48 std::string cmTimestamp::CreateTimestampFromTimeT(time_t timeT,
49     std::string formatString, bool utcFlag)
50 {
51   if(formatString.empty())
52     {
53     formatString = "%Y-%m-%dT%H:%M:%S";
54     if(utcFlag)
55       {
56       formatString += "Z";
57       }
58     }
59
60   struct tm timeStruct;
61   memset(&timeStruct, 0, sizeof(timeStruct));
62
63   struct tm* ptr = (struct tm*) 0;
64   if(utcFlag)
65     {
66     ptr = gmtime(&timeT);
67     }
68   else
69     {
70     ptr = localtime(&timeT);
71     }
72
73   if(ptr == 0)
74     {
75     return std::string();
76     }
77
78   timeStruct = *ptr;
79
80   std::string result;
81   for(std::string::size_type i = 0; i < formatString.size(); ++i)
82     {
83     char c1 = formatString[i];
84     char c2 = (i+1 < formatString.size()) ?
85       formatString[i+1] : static_cast<char>(0);
86
87     if(c1 == '%' && c2 != 0)
88       {
89       result += AddTimestampComponent(c2, timeStruct);
90       ++i;
91       }
92     else
93       {
94       result += c1;
95       }
96     }
97
98   return result;
99 }
100
101 //----------------------------------------------------------------------------
102 std::string cmTimestamp::AddTimestampComponent(
103   char flag, struct tm& timeStruct)
104 {
105   std::string formatString = "%";
106   formatString += flag;
107
108   switch(flag)
109     {
110     case 'd':
111     case 'H':
112     case 'I':
113     case 'j':
114     case 'm':
115     case 'M':
116     case 'S':
117     case 'U':
118     case 'w':
119     case 'y':
120     case 'Y':
121       break;
122     default:
123       {
124       return formatString;
125       }
126     }
127
128   char buffer[16];
129
130   size_t size = strftime(buffer, sizeof(buffer),
131     formatString.c_str(), &timeStruct);
132
133   return std::string(buffer, size);
134 }