xref: /openbsd-src/usr.bin/make/timestamp.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: timestamp.c,v 1.3 2007/09/16 12:09:36 espie Exp $ */
3 
4 /*
5  * Copyright (c) 2001 Marc Espie.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
20  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include "config.h"
29 #include "defines.h"
30 #include "timestamp.h"
31 
32 #ifndef USE_TIMESPEC
33 #include <sys/types.h>
34 #include <utime.h>
35 #endif
36 
37 TIMESTAMP now;		/* The time at the start of this whole process */
38 
39 int
40 set_times(const char *f)
41 {
42 #ifdef USE_TIMESPEC
43     struct timeval tv[2];
44 
45     TIMESPEC_TO_TIMEVAL(&tv[0], &now);
46     TIMESPEC_TO_TIMEVAL(&tv[1], &now);
47     return utimes(f, tv);
48 #else
49     struct utimbuf times;
50 
51     times.actime = times.modtime = now;
52     return utime(f, &times);
53 #endif
54 }
55 
56 char *
57 time_to_string(TIMESTAMP time)
58 {
59 	struct tm *parts;
60 	static char buf[128];
61 	time_t t;
62 
63 	t = timestamp2time_t(time);
64 
65 	parts = localtime(&t);
66 	strftime(buf, sizeof buf, "%H:%M:%S %b %d, %Y", parts);
67 	buf[sizeof(buf) - 1] = '\0';
68 	return buf;
69 }
70 
71