1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _PICO_UTIL_DATETIME_H
8 #define _PICO_UTIL_DATETIME_H
9 
10 #include "pico.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /** \file datetime.h
17  * \defgroup util_datetime datetime
18  * \brief Date/Time formatting
19  * \ingroup pico_util
20  */
21 
22 #include <time.h>
23 #include <sys/time.h>
24 
25 #if PICO_INCLUDE_RTC_DATETIME
26 
27 /*! \brief  Convert a datetime_t structure to a string
28  *  \ingroup util_datetime
29  *
30  * \param buf character buffer to accept generated string
31  * \param buf_size The size of the passed in buffer
32  * \param t The datetime to be converted.
33  */
34 void datetime_to_str(char *buf, uint buf_size, const datetime_t *t);
35 
36 bool time_to_datetime(time_t time, datetime_t *dt);
37 bool datetime_to_time(const datetime_t *dt, time_t *time);
38 
39 void datetime_to_tm(const datetime_t *dt, struct tm *tm);
40 void tm_to_datetime(const struct tm *tm, datetime_t *dt);
41 
42 #endif
43 
44 uint64_t timespec_to_ms(const struct timespec *ts);
45 uint64_t timespec_to_us(const struct timespec *ts);
46 void ms_to_timespec(uint64_t ms, struct timespec *ts);
47 void us_to_timespec(uint64_t ms, struct timespec *ts);
48 
49 /*! \brief  localtime_r implementation for use by the pico_util datetime functions
50  *  \ingroup util_datetime
51  *
52  * This method calls localtime_r from the C library by default,
53  * but is declared as a weak implementation to allow user code to override it
54  */
55 struct tm *pico_localtime_r(const time_t *time, struct tm *tm);
56 
57 /*! \brief  mktime implementation for use by the pico_util datetime functions
58 *  \ingroup util_datetime
59 *
60 * This method calls mktime from the C library by default,
61 * but is declared as a weak implementation to allow user code to override it
62 */
63 time_t pico_mktime(struct tm *tm);
64 
65 #ifdef __cplusplus
66 }
67 #endif
68 #endif
69