/src/libpff/libfcache/libfcache_date_time.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Date and time functions |
3 | | * |
4 | | * Copyright (C) 2010-2024, Joachim Metz <joachim.metz@gmail.com> |
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 | 13.0M | { |
41 | 13.0M | #if defined( HAVE_CLOCK_GETTIME ) |
42 | 13.0M | struct timespec time_structure; |
43 | 13.0M | #endif |
44 | | |
45 | 13.0M | static char *function = "libfcache_date_time_get_timestamp"; |
46 | | |
47 | 13.0M | if( timestamp == NULL ) |
48 | 0 | { |
49 | 0 | libcerror_error_set( |
50 | 0 | error, |
51 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
52 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
53 | 0 | "%s: invalid timestamp.", |
54 | 0 | function ); |
55 | |
|
56 | 0 | return( -1 ); |
57 | 0 | } |
58 | 13.0M | #if defined( HAVE_CLOCK_GETTIME ) |
59 | 13.0M | if( clock_gettime( |
60 | 13.0M | CLOCK_REALTIME, |
61 | 13.0M | &time_structure ) != 0 ) |
62 | 0 | { |
63 | 0 | libcerror_error_set( |
64 | 0 | error, |
65 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
66 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
67 | 0 | "%s: unable to retrieve current time structure.", |
68 | 0 | function ); |
69 | |
|
70 | 0 | return( -1 ); |
71 | 0 | } |
72 | 13.0M | *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 | 13.0M | return( 1 ); |
91 | 13.0M | } |
92 | | |