Debian Codegraph @

libfcache/libfcache_date_time.c

raw symbols SCIP
1 /*
2 * Date and time functions
3 *
4 * Copyright (C) 2010-2024, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23
24 #if defined( HAVE_SYS_TIME_H )
25 #include <sys/time.h>
26 #endif
27
28 #include <time.h>
29
30 #include "libfcache_date_time.h"
31 #include "libfcache_libcerror.h"
32 #include "libfcache_types.h"
33
34 /* Retrieves the cache value timestamp
35 * Returns 1 if successful or -1 on error
36 */
37 int libfcache_date_time_get_timestamp(
38 int64_t *timestamp,
39 libcerror_error_t **error )
40 {
41 #if defined( HAVE_CLOCK_GETTIME )
42 struct timespec time_structure;
43 #endif
44
45 static char *function = "libfcache_date_time_get_timestamp";
46
47 if( timestamp == NULL )
48 {
49 libcerror_error_set(
50 error,
51 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
52 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
53 "%s: invalid timestamp.",
54 function );
55
56 return( -1 );
57 }
58 #if defined( HAVE_CLOCK_GETTIME )
59 if( clock_gettime(
60 CLOCK_REALTIME,
61 &time_structure ) != 0 )
62 {
63 libcerror_error_set(
64 error,
65 LIBCERROR_ERROR_DOMAIN_RUNTIME,
66 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
67 "%s: unable to retrieve current time structure.",
68 function );
69
70 return( -1 );
71 }
72 *timestamp = ( (int64_t) time_structure.tv_sec * 1000000000 ) + time_structure.tv_nsec;
73
74 #else
75 *timestamp = (int64_t) time( NULL );
76
77 if( *timestamp == (time_t) -1 )
78 {
79 libcerror_error_set(
80 error,
81 LIBCERROR_ERROR_DOMAIN_RUNTIME,
82 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
83 "%s: unable to retrieve current time.",
84 function );
85
86 return( -1 );
87 }
88 #endif /* defined( HAVE_CLOCK_GETTIME ) */
89
90 return( 1 );
91 }
92