time_util.c
Go to the documentation of this file.
1 /*!**********************************************************************
2 *
3 * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4 * All rights reserved.
5 *
6 *************************************************************************
7 *
8 * @file time_util.c
9 * @date Dec 21, 2016
10 * @brief Time utility module implementation file.
11 *
12 ************************************************************************/
13 
14 #include "time_util.h"
15 #include "definitions.h"
16 #include "log_util.h"
17 #include "memory.h"
18 
19 E_MCL_ERROR_CODE time_util_convert_to_iso_8601_format(const time_t *time_value, string_t **iso8601_formatted_time)
20 {
21  DEBUG_ENTRY("const time_t *time_value = <%p>, string_t **iso8601_formatted_time = <%p>", time_value, iso8601_formatted_time)
22 
23  E_MCL_ERROR_CODE return_code;
24  struct tm *time_structure;
25 
26 #if defined(WIN32) || defined(WIN64)
27  errno_t result;
28 #else
29  struct tm *temp;
30 #endif
31 
32  // Populate time structure for the given time value.
33  MCL_NEW(time_structure);
34  ASSERT_CODE(MCL_NULL != time_structure, MCL_OUT_OF_MEMORY);
35 
36 #if defined(WIN32) || defined(WIN64)
37  result = gmtime_s(time_structure, time_value);
38  ASSERT_STATEMENT_CODE_MESSAGE(0 == result, MCL_FREE(time_structure), MCL_FAIL, "gmtime_s() function returns error.");
39 #else
40  temp = gmtime_r(time_value, time_structure);
41  ASSERT_STATEMENT_CODE_MESSAGE(MCL_NULL != temp, MCL_FREE(time_structure), MCL_FAIL, "gmtime_r() function returns null.");
42 #endif
43 
44  // Initialize a string for ISO 8601 date and time format.
45  return_code = string_initialize_new(MCL_NULL, 25, iso8601_formatted_time);
46  ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == return_code, MCL_FREE(time_structure), return_code, "Memory allocation failed for ISO 8601 formatted date and time.");
47 
48  // Compose the string in ISO 8601 date and time format.
49  if (0 == strftime((*iso8601_formatted_time)->buffer, (*iso8601_formatted_time)->length, "%Y-%m-%dT%H:%M:%S.000Z", time_structure))
50  {
51  string_destroy(iso8601_formatted_time);
52  MCL_DEBUG("File creation time can not be converted to ISO-8601 date and time format.");
53  return_code = MCL_FAIL;
54  }
55 
56  MCL_FREE(time_structure);
57  DEBUG_LEAVE("retVal = <%d>", return_code);
58  return return_code;
59 }
60 
61 void time_util_get_time(mcl_time_t *current_time)
62 {
63  DEBUG_ENTRY("mcl_time_t *current_time = <%p>", current_time)
64 
65  time(current_time);
66 
67  DEBUG_LEAVE("retVal = void");
68 }
69 
70 // YYYY-MM-DDThh:mm:ss.sssZ Ex:2016-04-26T08:06:25.317Z
72 {
73  DEBUG_ENTRY("const char *timestamp = <%p>", timestamp)
74 
75  const char characters_to_check[7] = { '-', '-', 'T', ':', ':', '.', 'Z' };
76  const uint16_t maximum_values[7] = { 2999, 12, 31, 23, 59, 59, 999 };
77  const uint8_t character_indexes_to_check[7] = { 4, 7, 10, 13, 16, 19, 23 };
78  const char *temporary_buffer = timestamp;
79  char *end_pointer = MCL_NULL;
80  mcl_bool_t ok;
81  uint8_t index;
82 
83  // Check timestamp length.
84  const mcl_size_t expected_timestamp_length = 24;
85  mcl_size_t actual_timestamp_length = 0;
86 
87  actual_timestamp_length = string_util_strlen(timestamp);
88  ok = expected_timestamp_length == actual_timestamp_length;
89 
90  // Check timestamp format.
91  for (index = 0; index < 7; ++index)
92  {
93  ok = ok && characters_to_check[index] == timestamp[character_indexes_to_check[index]];
94  }
95 
96  // Check date and time values.
97  for (index = 0; index < 7; ++index)
98  {
99  // 10 is base.
100  mcl_size_t temporary_value = string_util_strtol(temporary_buffer, 10, &end_pointer);
101 
102  ok = ok && maximum_values[index] >= temporary_value;
103 
104  if (index < 3)
105  {
106  // Date values can not be zero.
107  ok = ok && temporary_value > 0;
108  }
109 
110  // To show the address after the special character.
111  temporary_buffer = end_pointer + 1;
112  }
113 
114  DEBUG_LEAVE("retVal = <%d>", ok);
115  return ok;
116 }
void string_destroy(string_t **string)
Destroys the allocated resources of the string.
Definition: string_type.c:326
Internal failure in MCL.
Definition: mcl_common.h:141
Memory module header file.
long string_util_strtol(const char *source, int base, char **end_pointer)
Returns the first occurrence of an integral value in source string.
Definition: string_util.c:232
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
#define MCL_NEW(p)
Definition: memory.h:121
E_MCL_ERROR_CODE string_initialize_new(const char *value, mcl_size_t value_length, string_t **string)
Initializes a new string_t object with the given value and length.
Definition: string_type.c:46
#define MCL_DEBUG(...)
Definition: log_util.h:70
Log utility module header file.
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
time_t mcl_time_t
Definition: mcl_common.h:48
#define MCL_FREE(p)
Definition: memory.h:125
#define ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
Definition: definitions.h:121
E_MCL_ERROR_CODE time_util_convert_to_iso_8601_format(const time_t *time_value, string_t **iso8601_formatted_time)
Definition: time_util.c:19
Definitions module header file.
mcl_bool_t time_util_validate_timestamp(const char *timestamp)
Definition: time_util.c:71
#define ASSERT_CODE(condition, return_code)
Definition: definitions.h:97
size_t mcl_size_t
Definition: mcl_common.h:38
mcl_uint8_t mcl_bool_t
Definition: mcl_common.h:47
Success.
Definition: mcl_common.h:140
void time_util_get_time(mcl_time_t *current_time)
Definition: time_util.c:61
Memory allocation fail.
Definition: mcl_common.h:143
#define MCL_NULL
Definition: definitions.h:24
mcl_size_t string_util_strlen(const char *buffer)
Standard library strlen wrapper.
Definition: string_util.c:24
Time utility module header file.