Skip to content

Instantly share code, notes, and snippets.

@alorence
Last active September 6, 2020 16:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alorence/59d18896aaad5188b7b4 to your computer and use it in GitHub Desktop.
Save alorence/59d18896aaad5188b7b4 to your computer and use it in GitHub Desktop.
Two CMake macros returning the current date (format yyyy-mm-dd) and the current time (hh:mm:ss). These are completely useless since CMake 3.0 and the introduction of `string(TIMESTAMP)` command.
cmake_minimum_required(VERSION 2.8)
# Return the date (yyyy-mm-dd)
macro(DATE RESULT)
if(WIN32)
execute_process(COMMAND "cmd" " /C date /T" OUTPUT_VARIABLE ${RESULT})
string(REGEX REPLACE "(..)/(..)/(....).*" "\\3-\\2-\\1" ${RESULT} ${${RESULT}})
elseif(UNIX)
execute_process(COMMAND "date" "+%Y-%m-%d" OUTPUT_VARIABLE ${RESULT})
else()
message(SEND_ERROR "Unable to detect date")
set(${RESULT} UNKNOWN)
endif()
endmacro()
# Return the time (hh:mm:ss)
macro(TIME RESULT)
if(WIN32)
execute_process(COMMAND "cmd" " /C echo %TIME%" OUTPUT_VARIABLE ${RESULT})
string(REGEX REPLACE "(..:..:..),(..)" "\\1" ${RESULT} ${${RESULT}})
elseif(UNIX)
execute_process(COMMAND "date" "+%H:%M:%S" OUTPUT_VARIABLE ${RESULT})
else()
message(SEND_ERROR "Unable to detect time")
set(${RESULT} UNKNOWN)
endif()
endmacro()
@tigerlei010
Copy link

the macro is wonderful . and I found a defect in string(REGEX REPLACE) which is from cmake.

@sawatts
Copy link

sawatts commented Sep 3, 2020

string(TIMESTAMP)?

@alorence
Copy link
Author

alorence commented Sep 4, 2020

@sawatts you are right, but this code was written on 2013 when CMake current version was 2.8 and didn't offer such a command. Starting from CMake 3.0, these macros are useless and should be replaced by string(TIMESTAMP) indeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment