1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Generate a header file for UTC time""" 15 16import argparse 17from datetime import datetime 18import sys 19 20HEADER = """// Copyright 2021 The Pigweed Authors 21// 22// Licensed under the Apache License, Version 2.0 (the "License"); you may not 23// use this file except in compliance with the License. You may obtain a copy of 24// the License at 25// 26// https://www.apache.org/licenses/LICENSE-2.0 27// 28// Unless required by applicable law or agreed to in writing, software 29// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 30// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 31// License for the specific language governing permissions and limitations under 32// the License. 33 34#include <stdint.h> 35 36""" 37 38 39def parse_args() -> None: 40 """Setup argparse.""" 41 parser = argparse.ArgumentParser() 42 parser.add_argument("out", help="path for output header file") 43 return parser.parse_args() 44 45 46def main() -> int: 47 """Main function""" 48 args = parse_args() 49 time_stamp = int(datetime.now().timestamp()) 50 print(time_stamp) 51 with open(args.out, "w") as header: 52 print(args.out) 53 header.write(HEADER) 54 55 # Add a comment in the generated header to show readable build time 56 string_date = datetime.fromtimestamp(time_stamp).strftime( 57 "%m/%d/%Y %H:%M:%S" 58 ) 59 header.write(f'// {string_date}\n') 60 61 # Write to the header. 62 header.write( 63 ''.join( 64 [ 65 'constexpr uint64_t kBuildTimeMicrosecondsUTC = ', 66 f'{int(time_stamp * 1e6)};\n', 67 ] 68 ) 69 ) 70 return 0 71 72 73if __name__ == "__main__": 74 sys.exit(main()) 75