cmTimestamp.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2012 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmTimestamp.h"
  11. #include <cstring>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. //----------------------------------------------------------------------------
  15. std::string cmTimestamp::CurrentTime(
  16. const std::string& formatString, bool utcFlag)
  17. {
  18. time_t currentTimeT = time(0);
  19. if(currentTimeT == time_t(-1)) return std::string();
  20. return CreateTimestampFromTimeT(currentTimeT, formatString, utcFlag);
  21. }
  22. std::string cmTimestamp::FileModificationTime(const char* path,
  23. const std::string& formatString, bool utcFlag)
  24. {
  25. #ifdef _WIN32
  26. struct _stat info;
  27. std::memset(&info, 0, sizeof(info));
  28. if(_stat(path, &info) != 0)
  29. return std::string();
  30. time_t currentTimeT = info.st_mtime;
  31. #else
  32. struct stat info;
  33. std::memset(&info, 0, sizeof(info));
  34. if(stat(path, &info) != 0)
  35. return std::string();
  36. time_t currentTimeT = info.st_mtime;
  37. #endif
  38. return CreateTimestampFromTimeT(currentTimeT, formatString, utcFlag);
  39. }
  40. std::string cmTimestamp::CreateTimestampFromTimeT(time_t timeT,
  41. std::string formatString, bool utcFlag)
  42. {
  43. if(formatString.empty())
  44. {
  45. formatString = "%Y-%m-%dT%H:%M:%S";
  46. if(utcFlag) formatString += "Z";
  47. }
  48. struct tm timeStruct;
  49. std::memset(&timeStruct, 0, sizeof(timeStruct));
  50. if(utcFlag)
  51. {
  52. tm* ptr = gmtime(&timeT);
  53. if(ptr == 0) return std::string();
  54. timeStruct = *ptr;
  55. }
  56. else
  57. {
  58. struct tm* ptr = localtime(&timeT);
  59. if(ptr == 0) return std::string();
  60. timeStruct = *ptr;
  61. }
  62. std::string result;
  63. for(std::string::size_type i = 0; i < formatString.size(); ++i)
  64. {
  65. char c1 = formatString[i];
  66. char c2 = (i+1 < formatString.size()) ?
  67. formatString[i+1] : 0;
  68. if(c1 == '%' && c2 != 0)
  69. {
  70. result += AddTimestampComponent(c2, timeStruct);
  71. ++i;
  72. }
  73. else
  74. {
  75. result += c1;
  76. }
  77. }
  78. return result;
  79. }
  80. //----------------------------------------------------------------------------
  81. std::string cmTimestamp::AddTimestampComponent(
  82. char flag, struct tm& timeStruct)
  83. {
  84. std::string formatString = "%";
  85. formatString += flag;
  86. switch(flag)
  87. {
  88. case 'd':
  89. case 'H':
  90. case 'I':
  91. case 'j':
  92. case 'm':
  93. case 'M':
  94. case 'S':
  95. case 'U':
  96. case 'w':
  97. case 'y':
  98. case 'Y':
  99. break;
  100. default:
  101. {
  102. return formatString;
  103. }
  104. }
  105. char buffer[16];
  106. size_t size = strftime(buffer, sizeof(buffer),
  107. formatString.c_str(), &timeStruct);
  108. return std::string(buffer, size);
  109. }