[v3,5/7] y2038: Provide conversion helpers for struct __timeval64

Message ID 20200129125914.11221-5-lukma@denx.de
State Committed
Headers

Commit Message

Lukasz Majewski Jan. 29, 2020, 12:59 p.m. UTC
  Those functions allow easy conversion between Y2038 safe, glibc internal
struct __timeval64 and other time related data structures (like struct timeval
or struct __timespec64).

Build tests:
./src/scripts/build-many-glibcs.py glibcs

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

---
Chanes for v3:
- Provide new helper function - valid_timeval64_to_timeval

Changes for v2:
- None
---
 include/time.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
  

Comments

Adhemerval Zanella Netto Feb. 4, 2020, 6:47 p.m. UTC | #1
On 29/01/2020 09:59, Lukasz Majewski wrote:
> Those functions allow easy conversion between Y2038 safe, glibc internal
> struct __timeval64 and other time related data structures (like struct timeval
> or struct __timespec64).
> 
> Build tests:
> ./src/scripts/build-many-glibcs.py glibcs
> 
> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>

> 
> ---
> Chanes for v3:
> - Provide new helper function - valid_timeval64_to_timeval
> 
> Changes for v2:
> - None
> ---
>  include/time.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 49 insertions(+)
> 
> diff --git a/include/time.h b/include/time.h
> index 99492a1577..8617114052 100644
> --- a/include/time.h
> +++ b/include/time.h
> @@ -304,6 +304,43 @@ valid_timeval_to_timespec64 (const struct timeval tv)
>    return ts64;
>  }
>  
> +/* Convert a known valid struct timeval into a struct __timeval64.  */
> +static inline struct __timeval64
> +valid_timeval_to_timeval64 (const struct timeval tv)
> +{
> +  struct __timeval64 tv64;
> +
> +  tv64.tv_sec = tv.tv_sec;
> +  tv64.tv_usec = tv.tv_usec;
> +
> +  return tv64;
> +}
> +

Ok.

> +/* Convert a valid and within range of struct timeval, struct
> +   __timeval64 into a struct timeval.  */
> +static inline struct timeval
> +valid_timeval64_to_timeval (const struct __timeval64 tv64)
> +{
> +  struct timeval tv;
> +
> +  tv.tv_sec = (time_t) tv64.tv_sec;
> +  tv.tv_usec = (suseconds_t) tv64.tv_usec;
> +
> +  return tv;
> +}
> +

Ok.

> +/* Convert a struct __timeval64 into a struct __timespec64.  */
> +static inline struct __timespec64
> +timeval64_to_timespec64 (const struct __timeval64 tv64)
> +{
> +  struct __timespec64 ts64;
> +
> +  ts64.tv_sec = tv64.tv_sec;
> +  ts64.tv_nsec = tv64.tv_usec * 1000;
> +
> +  return ts64;
> +}
> +

Ok.

>  /* Convert a known valid struct timespec into a struct __timespec64.  */
>  static inline struct __timespec64
>  valid_timespec_to_timespec64 (const struct timespec ts)
> @@ -342,6 +379,18 @@ valid_timespec64_to_timeval (const struct __timespec64 ts64)
>    return tv;
>  }
>  
> +/* Convert a struct __timespec64 into a struct __timeval64.  */
> +static inline struct __timeval64
> +timespec64_to_timeval64 (const struct __timespec64 ts64)
> +{
> +  struct __timeval64 tv64;
> +
> +  tv64.tv_sec = ts64.tv_sec;
> +  tv64.tv_usec = ts64.tv_nsec / 1000;
> +
> +  return tv64;
> +}
> +
>  /* Check if a value is in the valid nanoseconds range. Return true if
>     it is, false otherwise.  */
>  static inline bool
> 

Ok.
  

Patch

diff --git a/include/time.h b/include/time.h
index 99492a1577..8617114052 100644
--- a/include/time.h
+++ b/include/time.h
@@ -304,6 +304,43 @@  valid_timeval_to_timespec64 (const struct timeval tv)
   return ts64;
 }
 
+/* Convert a known valid struct timeval into a struct __timeval64.  */
+static inline struct __timeval64
+valid_timeval_to_timeval64 (const struct timeval tv)
+{
+  struct __timeval64 tv64;
+
+  tv64.tv_sec = tv.tv_sec;
+  tv64.tv_usec = tv.tv_usec;
+
+  return tv64;
+}
+
+/* Convert a valid and within range of struct timeval, struct
+   __timeval64 into a struct timeval.  */
+static inline struct timeval
+valid_timeval64_to_timeval (const struct __timeval64 tv64)
+{
+  struct timeval tv;
+
+  tv.tv_sec = (time_t) tv64.tv_sec;
+  tv.tv_usec = (suseconds_t) tv64.tv_usec;
+
+  return tv;
+}
+
+/* Convert a struct __timeval64 into a struct __timespec64.  */
+static inline struct __timespec64
+timeval64_to_timespec64 (const struct __timeval64 tv64)
+{
+  struct __timespec64 ts64;
+
+  ts64.tv_sec = tv64.tv_sec;
+  ts64.tv_nsec = tv64.tv_usec * 1000;
+
+  return ts64;
+}
+
 /* Convert a known valid struct timespec into a struct __timespec64.  */
 static inline struct __timespec64
 valid_timespec_to_timespec64 (const struct timespec ts)
@@ -342,6 +379,18 @@  valid_timespec64_to_timeval (const struct __timespec64 ts64)
   return tv;
 }
 
+/* Convert a struct __timespec64 into a struct __timeval64.  */
+static inline struct __timeval64
+timespec64_to_timeval64 (const struct __timespec64 ts64)
+{
+  struct __timeval64 tv64;
+
+  tv64.tv_sec = ts64.tv_sec;
+  tv64.tv_usec = ts64.tv_nsec / 1000;
+
+  return tv64;
+}
+
 /* Check if a value is in the valid nanoseconds range. Return true if
    it is, false otherwise.  */
 static inline bool