xref: /freebsd/lib/libc/stdtime/time32.c (revision e8627df6)
1237c4e3aSMatthew Dillon /*-
2237c4e3aSMatthew Dillon  * Copyright (c) 2001 FreeBSD Inc.
3237c4e3aSMatthew Dillon  * All rights reserved.
4237c4e3aSMatthew Dillon  *
5237c4e3aSMatthew Dillon  * These routines are for converting time_t to fixed-bit representations
6237c4e3aSMatthew Dillon  * for use in protocols or storage.  When converting time to a larger
7237c4e3aSMatthew Dillon  * representation of time_t these routines are expected to assume temporal
8237c4e3aSMatthew Dillon  * locality and use the 50-year rule to properly set the msb bits.  XXX
9237c4e3aSMatthew Dillon  *
10237c4e3aSMatthew Dillon  * Redistribution and use under the terms of the COPYRIGHT file at the
11237c4e3aSMatthew Dillon  * base of the source tree.
12237c4e3aSMatthew Dillon  *
13237c4e3aSMatthew Dillon  * $FreeBSD$
14237c4e3aSMatthew Dillon  */
15237c4e3aSMatthew Dillon 
16237c4e3aSMatthew Dillon #include <sys/types.h>
17237c4e3aSMatthew Dillon #include <sys/time.h>
18237c4e3aSMatthew Dillon 
19237c4e3aSMatthew Dillon /*
20237c4e3aSMatthew Dillon  * Convert a 32 bit representation of time_t into time_t.  XXX needs to
21237c4e3aSMatthew Dillon  * implement the 50-year rule to handle post-2038 conversions.
22237c4e3aSMatthew Dillon  */
23237c4e3aSMatthew Dillon time_t
24237c4e3aSMatthew Dillon time32_to_time(__int32_t t32)
25237c4e3aSMatthew Dillon {
26237c4e3aSMatthew Dillon     return((time_t)t32);
27237c4e3aSMatthew Dillon }
28237c4e3aSMatthew Dillon 
29237c4e3aSMatthew Dillon /*
30237c4e3aSMatthew Dillon  * Convert time_t to a 32 bit representation.  If time_t is 64 bits we can
31237c4e3aSMatthew Dillon  * simply chop it down.   The resulting 32 bit representation can be
32237c4e3aSMatthew Dillon  * converted back to a temporally local 64 bit time_t using time32_to_time.
33237c4e3aSMatthew Dillon  */
34237c4e3aSMatthew Dillon __int32_t
35237c4e3aSMatthew Dillon time_to_time32(time_t t)
36237c4e3aSMatthew Dillon {
37237c4e3aSMatthew Dillon     return((__int32_t)t);
38237c4e3aSMatthew Dillon }
39237c4e3aSMatthew Dillon 
40237c4e3aSMatthew Dillon /*
41237c4e3aSMatthew Dillon  * Convert a 64 bit representation of time_t into time_t.  If time_t is
42237c4e3aSMatthew Dillon  * represented as 32 bits we can simply chop it and not support times
43237c4e3aSMatthew Dillon  * past 2038.
44237c4e3aSMatthew Dillon  */
45237c4e3aSMatthew Dillon time_t
46237c4e3aSMatthew Dillon time64_to_time(__int64_t t64)
47237c4e3aSMatthew Dillon {
48237c4e3aSMatthew Dillon     return((time_t)t64);
49237c4e3aSMatthew Dillon }
50237c4e3aSMatthew Dillon 
51237c4e3aSMatthew Dillon /*
52237c4e3aSMatthew Dillon  * Convert time_t to a 64 bit representation.  If time_t is represented
53237c4e3aSMatthew Dillon  * as 32 bits we simply sign-extend and do not support times past 2038.
54237c4e3aSMatthew Dillon  */
55237c4e3aSMatthew Dillon __int64_t
56237c4e3aSMatthew Dillon time_to_time64(time_t t)
57237c4e3aSMatthew Dillon {
58237c4e3aSMatthew Dillon     return((__int64_t)t);
59237c4e3aSMatthew Dillon }
60237c4e3aSMatthew Dillon 
61e8627df6SMatthew Dillon /*
62e8627df6SMatthew Dillon  * Convert to/from 'long'.  Depending on the sizeof(long) this may or
63e8627df6SMatthew Dillon  * may not require using the 50-year rule.
64e8627df6SMatthew Dillon  */
65e8627df6SMatthew Dillon long
66e8627df6SMatthew Dillon time_to_long(time_t t)
67e8627df6SMatthew Dillon {
68e8627df6SMatthew Dillon     if (sizeof(long) == sizeof(__int64_t))
69e8627df6SMatthew Dillon 	return(time_to_time64(t));
70e8627df6SMatthew Dillon     return((long)t);
71e8627df6SMatthew Dillon }
72e8627df6SMatthew Dillon 
73e8627df6SMatthew Dillon time_t
74e8627df6SMatthew Dillon long_to_time(long tlong)
75e8627df6SMatthew Dillon {
76e8627df6SMatthew Dillon     if (sizeof(long) == sizeof(__int32_t))
77e8627df6SMatthew Dillon 	return(time32_to_time(tlong));
78e8627df6SMatthew Dillon     return((time_t)tlong);
79e8627df6SMatthew Dillon }
80e8627df6SMatthew Dillon 
81e8627df6SMatthew Dillon /*
82e8627df6SMatthew Dillon  * Convert to/from 'int'.  Depending on the sizeof(int) this may or
83e8627df6SMatthew Dillon  * may not require using the 50-year rule.
84e8627df6SMatthew Dillon  */
85e8627df6SMatthew Dillon int
86e8627df6SMatthew Dillon time_to_int(time_t t)
87e8627df6SMatthew Dillon {
88e8627df6SMatthew Dillon     if (sizeof(int) == sizeof(__int64_t))
89e8627df6SMatthew Dillon 	return(time_to_time64(t));
90e8627df6SMatthew Dillon     return((int)t);
91e8627df6SMatthew Dillon }
92e8627df6SMatthew Dillon 
93e8627df6SMatthew Dillon time_t
94e8627df6SMatthew Dillon int_to_time(int tint)
95e8627df6SMatthew Dillon {
96e8627df6SMatthew Dillon     if (sizeof(int) == sizeof(__int32_t))
97e8627df6SMatthew Dillon 	return(time32_to_time(tint));
98e8627df6SMatthew Dillon     return((time_t)tint);
99e8627df6SMatthew Dillon }
100e8627df6SMatthew Dillon 
101