1# BAREOS® - Backup Archiving REcovery Open Sourced
2#
3# Copyright (C) 2019-2020 Bareos GmbH & Co. KG
4#
5# This program is Free Software; you can redistribute it and/or modify it under
6# the terms of version three of the GNU Affero General Public License as
7# published by the Free Software Foundation and included in the file LICENSE.
8#
9# This program is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
12# details.
13#
14# You should have received a copy of the GNU Affero General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc., 51
16# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18function(timestamp_at at result format)
19  if(CMAKE_VERSION VERSION_GREATER 3.10.0)
20    set(old_epoch "$ENV{SOURCE_DATE_EPOCH}")
21    set(ENV{SOURCE_DATE_EPOCH} "${at}")
22    string(TIMESTAMP out "${format}" UTC)
23    set(ENV{SOURCE_DATE_EPOCH} "${old_epoch}")
24  else()
25    if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
26      set(DATECMD gdate)
27    else()
28      set(DATECMD date)
29    endif()
30    set(old_lang "$ENV{LC_ALL}")
31    set(ENV{LC_ALL} "C")
32    execute_process(
33      COMMAND ${DATECMD} --utc "--date=@${at}" "+${format}"
34      OUTPUT_VARIABLE out
35      OUTPUT_STRIP_TRAILING_WHITESPACE
36    )
37    set(ENV{LC_ALL} "${old_lang}")
38    if(out STREQUAL "")
39      message(
40        FATAL_ERROR
41          "Cannot use SOURCE_DATE_EPOCH (cmake < 3.10) and your 'date' command is not compatible with Bareos' timestamp_at()."
42      )
43    endif()
44  endif()
45  set("${result}"
46      "${out}"
47      PARENT_SCOPE
48  )
49endfunction()
50