1
0

GetDate.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. cmake_minimum_required(VERSION 2.8.11)
  2. macro(GET_DATE)
  3. #
  4. # All macro arguments are optional.
  5. # If there's an ARGV0, use it as GD_PREFIX. Default = 'GD_'
  6. # If there's an ARGV1, use it as ${GD_PREFIX}VERBOSE. Default = '0'
  7. #
  8. # If the date can be retrieved and parsed successfully, this macro
  9. # will set the following CMake variables:
  10. #
  11. # GD_PREFIX
  12. # ${GD_PREFIX}PREFIX (if '${GD_PREFIX}' is not 'GD_'...!)
  13. # ${GD_PREFIX}VERBOSE
  14. #
  15. # ${GD_PREFIX}OV
  16. #
  17. # ${GD_PREFIX}REGEX
  18. # ${GD_PREFIX}YEAR
  19. # ${GD_PREFIX}MONTH
  20. # ${GD_PREFIX}DAY
  21. # ${GD_PREFIX}HOUR
  22. # ${GD_PREFIX}MINUTE
  23. # ${GD_PREFIX}SECOND
  24. #
  25. # Caller can then use these variables to construct names based on
  26. # date and time stamps...
  27. #
  28. # If there's an ARGV0, use it as GD_PREFIX:
  29. #
  30. set(GD_PREFIX "GD_")
  31. if(NOT "${ARGV0}" STREQUAL "")
  32. set(GD_PREFIX "${ARGV0}")
  33. endif()
  34. if(NOT "${GD_PREFIX}" STREQUAL "GD_")
  35. set(${GD_PREFIX}PREFIX "${GD_PREFIX}")
  36. endif()
  37. # If there's an ARGV1, use it as ${GD_PREFIX}VERBOSE:
  38. #
  39. set(${GD_PREFIX}VERBOSE "0")
  40. if(NOT "${ARGV1}" STREQUAL "")
  41. set(${GD_PREFIX}VERBOSE "${ARGV1}")
  42. endif()
  43. # Retrieve the current date and time in the format:
  44. #
  45. # 01/12/2006 08:55:12
  46. # mm/dd/YYYY HH:MM:SS
  47. #
  48. string(TIMESTAMP "${GD_PREFIX}OV" "%m/%d/%Y %H:%M:%S")
  49. if(${GD_PREFIX}VERBOSE)
  50. message(STATUS "")
  51. message(STATUS "<GET_DATE>")
  52. message(STATUS "")
  53. message(STATUS "GD_PREFIX='${GD_PREFIX}'")
  54. if(NOT "${GD_PREFIX}" STREQUAL "GD_")
  55. message(STATUS "${GD_PREFIX}PREFIX='${${GD_PREFIX}PREFIX}'")
  56. endif()
  57. message(STATUS "${GD_PREFIX}VERBOSE='${${GD_PREFIX}VERBOSE}'")
  58. message(STATUS "")
  59. message(STATUS "${GD_PREFIX}OV='${${GD_PREFIX}OV}'")
  60. message(STATUS "")
  61. endif()
  62. #
  63. # Extract six individual components by matching a regex with paren groupings.
  64. # Use the replace functionality and \\1 thru \\6 to extract components.
  65. #
  66. set(${GD_PREFIX}REGEX "([^/]+)/([^/]+)/([^ ]+) +([^:]+):([^:]+):([^\\.]+)")
  67. string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\1" ${GD_PREFIX}MONTH "${${GD_PREFIX}OV}")
  68. string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\2" ${GD_PREFIX}DAY "${${GD_PREFIX}OV}")
  69. string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\3" ${GD_PREFIX}YEAR "${${GD_PREFIX}OV}")
  70. string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\4" ${GD_PREFIX}HOUR "${${GD_PREFIX}OV}")
  71. string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\5" ${GD_PREFIX}MINUTE "${${GD_PREFIX}OV}")
  72. string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\6" ${GD_PREFIX}SECOND "${${GD_PREFIX}OV}")
  73. if(${GD_PREFIX}VERBOSE)
  74. message(STATUS "${GD_PREFIX}REGEX='${${GD_PREFIX}REGEX}'")
  75. message(STATUS "${GD_PREFIX}YEAR='${${GD_PREFIX}YEAR}'")
  76. message(STATUS "${GD_PREFIX}MONTH='${${GD_PREFIX}MONTH}'")
  77. message(STATUS "${GD_PREFIX}DAY='${${GD_PREFIX}DAY}'")
  78. message(STATUS "${GD_PREFIX}HOUR='${${GD_PREFIX}HOUR}'")
  79. message(STATUS "${GD_PREFIX}MINUTE='${${GD_PREFIX}MINUTE}'")
  80. message(STATUS "${GD_PREFIX}SECOND='${${GD_PREFIX}SECOND}'")
  81. message(STATUS "")
  82. message(STATUS "Counters that change...")
  83. message(STATUS "")
  84. message(STATUS " every second : ${${GD_PREFIX}YEAR}${${GD_PREFIX}MONTH}${${GD_PREFIX}DAY}${${GD_PREFIX}HOUR}${${GD_PREFIX}MINUTE}${${GD_PREFIX}SECOND}")
  85. message(STATUS " daily : ${${GD_PREFIX}YEAR}${${GD_PREFIX}MONTH}${${GD_PREFIX}DAY}")
  86. message(STATUS " monthly : ${${GD_PREFIX}YEAR}${${GD_PREFIX}MONTH}")
  87. message(STATUS " annually : ${${GD_PREFIX}YEAR}")
  88. message(STATUS "")
  89. endif()
  90. if(${GD_PREFIX}VERBOSE)
  91. message(STATUS "</GET_DATE>")
  92. message(STATUS "")
  93. endif()
  94. endmacro()
  95. macro(ADD_SECONDS sec)
  96. set(new_min ${${GD_PREFIX}MINUTE})
  97. set(new_hr ${${GD_PREFIX}HOUR})
  98. math(EXPR new_sec "${sec} + ${${GD_PREFIX}SECOND}")
  99. while(${new_sec} GREATER 60 OR ${new_sec} EQUAL 60)
  100. math(EXPR new_sec "${new_sec} - 60")
  101. math(EXPR new_min "${${GD_PREFIX}MINUTE} + 1")
  102. endwhile()
  103. while(${new_min} GREATER 60 OR ${new_min} EQUAL 60)
  104. math(EXPR new_min "${new_min} - 60")
  105. math(EXPR new_hr "${${GD_PREFIX}HOUR} + 1")
  106. endwhile()
  107. math(EXPR new_hr "${new_hr} % 24")
  108. # Pad the H, M, S if needed
  109. string(LENGTH ${new_sec} sec_len)
  110. string(LENGTH ${new_min} min_len)
  111. string(LENGTH ${new_hr} hr_len)
  112. if(${sec_len} EQUAL 1)
  113. set(new_sec "0${new_sec}")
  114. endif()
  115. if(${min_len} EQUAL 1)
  116. set(new_min "0${new_min}")
  117. endif()
  118. if(${hr_len} EQUAL 1)
  119. set(new_hr "0${new_hr}")
  120. endif()
  121. endmacro()