[CMake] How to get the system date in a CMake Script ?

Philippe Poilbarbe Philippe.Poilbarbe at cls.fr
Fri Jun 16 06:44:25 EDT 2006


Filipe Sousa a écrit :
> On Friday 16 June 2006 09:49, Sylvain Benner wrote:
>   
>> Is there a simple way to get the system date in a CMake script ?
>>     
> The only way is calling an external program through EXECUTE_PROCESS. Under 
> Unix you have /usr/bin/date.
Each time something cannot be done by CMake and need
external program/command I always use perl since it works
on each platform (provided it is installed) and introduce
only one more external dependency.
So here is the macro I use to get current time (in an
ISO 8601 format):

INCLUDE(FindPerl)

MACRO (NOW RESULT)
  IF (PERL_FOUND)
    EXECUTE_PROCESS(COMMAND "${PERL_EXECUTABLE}" "-le" "@T=localtime; 
print sprintf 
\"%04d-%02d-%02dT%02d:%02d:%02d\",$T[5]+1900,$T[4]+1,$T[3],$T[2],$T[1],$T[0]"
            OUTPUT_VARIABLE ${RESULT})
  ELSE (PERL_FOUND)
    MESSAGE(SEND_ERROR "Perl needed for some operations")
  ENDIF (PERL_FOUND)
ENDMACRO (NOW)

Notice that the second argument to perl (from
@T to T[0]) must be on the same line even if
it is broken by this mail)

Philippe.



More information about the CMake mailing list