GetDate.cmake 4.43 KB
Newer Older
1
cmake_minimum_required(VERSION 2.8.11)
Zach's avatar
Zach committed
2

3
macro(GET_DATE)
Zach's avatar
Zach committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #
  # All macro arguments are optional.
  #   If there's an ARGV0, use it as GD_PREFIX. Default = 'GD_'
  #   If there's an ARGV1, use it as ${GD_PREFIX}VERBOSE. Default = '0'
  #
  # If the date can be retrieved and parsed successfully, this macro
  # will set the following CMake variables:
  #
  #   GD_PREFIX
  #   ${GD_PREFIX}PREFIX (if '${GD_PREFIX}' is not 'GD_'...!)
  #   ${GD_PREFIX}VERBOSE
  #
  #   ${GD_PREFIX}OV
  #
  #   ${GD_PREFIX}REGEX
  #   ${GD_PREFIX}YEAR
  #   ${GD_PREFIX}MONTH
  #   ${GD_PREFIX}DAY
  #   ${GD_PREFIX}HOUR
  #   ${GD_PREFIX}MINUTE
  #   ${GD_PREFIX}SECOND
  #
  # Caller can then use these variables to construct names based on
  # date and time stamps...
  #

  # If there's an ARGV0, use it as GD_PREFIX:
  #
32
33
34
  set(GD_PREFIX "GD_")
  if(NOT "${ARGV0}" STREQUAL "")
    set(GD_PREFIX "${ARGV0}")
35
  endif()
36
37
  if(NOT "${GD_PREFIX}" STREQUAL "GD_")
    set(${GD_PREFIX}PREFIX "${GD_PREFIX}")
38
  endif()
Zach's avatar
Zach committed
39
40
41

  # If there's an ARGV1, use it as ${GD_PREFIX}VERBOSE:
  #
42
43
44
  set(${GD_PREFIX}VERBOSE "0")
  if(NOT "${ARGV1}" STREQUAL "")
    set(${GD_PREFIX}VERBOSE "${ARGV1}")
45
  endif()
Zach's avatar
Zach committed
46
47
48

  # Retrieve the current date and time in the format:
  #
49
50
  # 01/12/2006  08:55:12
  # mm/dd/YYYY HH:MM:SS
Zach's avatar
Zach committed
51
  #
52
  unset(ENV{SOURCE_DATE_EPOCH})
53
  string(TIMESTAMP "${GD_PREFIX}OV" "%m/%d/%Y %H:%M:%S")
Zach's avatar
Zach committed
54

55
56
57
58
59
60
61
  if(${GD_PREFIX}VERBOSE)
    message(STATUS "")
    message(STATUS "<GET_DATE>")
    message(STATUS "")
    message(STATUS "GD_PREFIX='${GD_PREFIX}'")
    if(NOT "${GD_PREFIX}" STREQUAL "GD_")
      message(STATUS "${GD_PREFIX}PREFIX='${${GD_PREFIX}PREFIX}'")
62
    endif()
63
64
65
66
    message(STATUS "${GD_PREFIX}VERBOSE='${${GD_PREFIX}VERBOSE}'")
    message(STATUS "")
    message(STATUS "${GD_PREFIX}OV='${${GD_PREFIX}OV}'")
    message(STATUS "")
67
  endif()
68

69
70
71
72
73
  #
  # Extract six individual components by matching a regex with paren groupings.
  # Use the replace functionality and \\1 thru \\6 to extract components.
  #
  set(${GD_PREFIX}REGEX "([^/]+)/([^/]+)/([^ ]+) +([^:]+):([^:]+):([^\\.]+)")
Zach's avatar
Zach committed
74

75
76
77
78
79
80
  string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\1" ${GD_PREFIX}MONTH "${${GD_PREFIX}OV}")
  string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\2" ${GD_PREFIX}DAY "${${GD_PREFIX}OV}")
  string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\3" ${GD_PREFIX}YEAR "${${GD_PREFIX}OV}")
  string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\4" ${GD_PREFIX}HOUR "${${GD_PREFIX}OV}")
  string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\5" ${GD_PREFIX}MINUTE "${${GD_PREFIX}OV}")
  string(REGEX REPLACE "${${GD_PREFIX}REGEX}" "\\6" ${GD_PREFIX}SECOND "${${GD_PREFIX}OV}")
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  if(${GD_PREFIX}VERBOSE)
    message(STATUS "${GD_PREFIX}REGEX='${${GD_PREFIX}REGEX}'")
    message(STATUS "${GD_PREFIX}YEAR='${${GD_PREFIX}YEAR}'")
    message(STATUS "${GD_PREFIX}MONTH='${${GD_PREFIX}MONTH}'")
    message(STATUS "${GD_PREFIX}DAY='${${GD_PREFIX}DAY}'")
    message(STATUS "${GD_PREFIX}HOUR='${${GD_PREFIX}HOUR}'")
    message(STATUS "${GD_PREFIX}MINUTE='${${GD_PREFIX}MINUTE}'")
    message(STATUS "${GD_PREFIX}SECOND='${${GD_PREFIX}SECOND}'")
    message(STATUS "")
    message(STATUS "Counters that change...")
    message(STATUS "")
    message(STATUS "        every second : ${${GD_PREFIX}YEAR}${${GD_PREFIX}MONTH}${${GD_PREFIX}DAY}${${GD_PREFIX}HOUR}${${GD_PREFIX}MINUTE}${${GD_PREFIX}SECOND}")
    message(STATUS "               daily : ${${GD_PREFIX}YEAR}${${GD_PREFIX}MONTH}${${GD_PREFIX}DAY}")
    message(STATUS "             monthly : ${${GD_PREFIX}YEAR}${${GD_PREFIX}MONTH}")
    message(STATUS "            annually : ${${GD_PREFIX}YEAR}")
    message(STATUS "")
98
  endif()
99
100
101
102

  if(${GD_PREFIX}VERBOSE)
    message(STATUS "</GET_DATE>")
    message(STATUS "")
103
104
  endif()
endmacro()
105
106

macro(ADD_SECONDS sec)
Zach's avatar
Zach committed
107
108
109
  set(new_min ${${GD_PREFIX}MINUTE})
  set(new_hr ${${GD_PREFIX}HOUR})
  math(EXPR new_sec "${sec} + ${${GD_PREFIX}SECOND}")
110
  while(${new_sec} GREATER_EQUAL 60)
Zach's avatar
Zach committed
111
112
113
    math(EXPR new_sec "${new_sec} - 60")
    math(EXPR new_min "${${GD_PREFIX}MINUTE} + 1")
  endwhile()
114
  while(${new_min} GREATER_EQUAL 60)
Zach's avatar
Zach committed
115
116
117
118
    math(EXPR new_min "${new_min} - 60")
    math(EXPR new_hr "${${GD_PREFIX}HOUR} + 1")
  endwhile()
  math(EXPR new_hr "${new_hr} % 24")
Zach's avatar
Zach committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132

  # Pad the H, M, S if needed
  string(LENGTH ${new_sec} sec_len)
  string(LENGTH ${new_min} min_len)
  string(LENGTH ${new_hr} hr_len)
  if(${sec_len} EQUAL 1)
    set(new_sec "0${new_sec}")
  endif()
  if(${min_len} EQUAL 1)
    set(new_min "0${new_min}")
  endif()
  if(${hr_len} EQUAL 1)
    set(new_hr "0${new_hr}")
  endif()
133
endmacro()