Coverage Report

Created: 2025-06-13 07:22

/src/libewf/libewf/libewf_date_time.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Date and time functions
3
 *
4
 * Copyright (C) 2006-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
#include <memory.h>
24
#include <types.h>
25
26
#if defined( HAVE_SYS_TIME_H )
27
#include <sys/time.h>
28
#endif
29
30
#include <time.h>
31
32
#include "libewf_date_time.h"
33
#include "libewf_libcerror.h"
34
35
/* Returns a structured representation of a time using the local timezone, or NULL on error
36
 */
37
int libewf_date_time_localtime(
38
     const time_t *timestamp,
39
     struct tm *time_elements,
40
     libcerror_error_t **error )
41
1.71k
{
42
#if ( defined( HAVE_LOCALTIME ) && !defined( HAVE_LOCALTIME_R ) ) || ( defined( WINAPI ) && !defined( _MSC_VER ) )
43
  struct tm *static_time_elements = NULL;
44
#endif
45
1.71k
  static char *function           = "libewf_date_time_localtime";
46
47
1.71k
  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 time stamp.",
54
0
     function );
55
56
0
    return( -1 );
57
0
  }
58
1.71k
  if( time_elements == NULL )
59
0
  {
60
0
    libcerror_error_set(
61
0
     error,
62
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
63
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
64
0
     "%s: invalid time elements.",
65
0
     function );
66
67
0
    return( -1 );
68
0
  }
69
#if defined( _MSC_VER )
70
  if( localtime_s(
71
       time_elements,
72
       timestamp ) != 0 )
73
  {
74
    libcerror_error_set(
75
     error,
76
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
77
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
78
     "%s: unable to set time elements.",
79
     function );
80
81
    return( -1 );
82
  }
83
#elif defined( HAVE_LOCALTIME_R )
84
1.71k
  if( localtime_r(
85
1.71k
       timestamp,
86
1.71k
       time_elements ) == NULL )
87
15
  {
88
15
    libcerror_error_set(
89
15
     error,
90
15
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
91
15
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
92
15
     "%s: unable to set time elements.",
93
15
     function );
94
95
15
    return( -1 );
96
15
  }
97
#elif defined( HAVE_LOCALTIME ) || defined( WINAPI )
98
  static_time_elements = localtime(
99
                          timestamp );
100
101
  if( static_time_elements == NULL )
102
  {
103
    libcerror_error_set(
104
     error,
105
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
106
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
107
     "%s: unable to create static time elements.",
108
     function );
109
110
    return( -1 );
111
  }
112
  if( memory_copy(
113
       time_elements,
114
       static_time_elements,
115
       sizeof( struct tm ) ) == NULL )
116
  {
117
    libcerror_error_set(
118
     error,
119
     LIBCERROR_ERROR_DOMAIN_MEMORY,
120
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
121
     "%s: unable to set time elements.",
122
     function );
123
124
    return( -1 );
125
  }
126
#endif
127
1.69k
  return( 1 );
128
1.71k
}
129