Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / lib / libc / stdtime / time32.c
1 /*-
2  * Copyright (c) 2001 FreeBSD Inc.
3  * All rights reserved.
4  *
5  * These routines are for converting time_t to fixed-bit representations
6  * for use in protocols or storage.  When converting time to a larger
7  * representation of time_t these routines are expected to assume temporal
8  * locality and use the 50-year rule to properly set the msb bits.  XXX
9  *
10  * Redistribution and use under the terms of the COPYRIGHT file at the
11  * base of the source tree.
12  *
13  * $FreeBSD: src/lib/libc/stdtime/time32.c,v 1.5 2002/06/17 01:42:29 wollman Exp $
14  */
15
16 #include <sys/types.h>
17 #include <timeconv.h>
18
19 /*
20  * Convert a 32 bit representation of time_t into time_t.  XXX needs to
21  * implement the 50-year rule to handle post-2038 conversions.
22  */
23 time_t
24 _time32_to_time(__int32_t t32)
25 {
26
27         return((time_t)t32);
28 }
29
30 /*
31  * Convert time_t to a 32 bit representation.  If time_t is 64 bits we can
32  * simply chop it down.   The resulting 32 bit representation can be
33  * converted back to a temporally local 64 bit time_t using time32_to_time.
34  */
35 __int32_t
36 _time_to_time32(time_t t)
37 {
38
39         return((__int32_t)t);
40 }
41
42 /*
43  * Convert a 64 bit representation of time_t into time_t.  If time_t is
44  * represented as 32 bits we can simply chop it and not support times
45  * past 2038.
46  */
47 time_t
48 _time64_to_time(__int64_t t64)
49 {
50
51         return((time_t)t64);
52 }
53
54 /*
55  * Convert time_t to a 64 bit representation.  If time_t is represented
56  * as 32 bits we simply sign-extend and do not support times past 2038.
57  */
58 __int64_t
59 _time_to_time64(time_t t)
60 {
61
62         return((__int64_t)t);
63 }
64
65 /*
66  * Convert to/from 'long'.  Depending on the sizeof(long) this may or
67  * may not require using the 50-year rule.
68  */
69 long
70 _time_to_long(time_t t)
71 {
72
73         if (sizeof(long) == sizeof(__int64_t))
74                 return(_time_to_time64(t));
75         return((long)t);
76 }
77
78 time_t
79 _long_to_time(long tlong)
80 {
81
82         if (sizeof(long) == sizeof(__int32_t))
83                 return(_time32_to_time(tlong));
84         return((time_t)tlong);
85 }
86
87 /*
88  * Convert to/from 'int'.  Depending on the sizeof(int) this may or
89  * may not require using the 50-year rule.
90  */
91 int
92 _time_to_int(time_t t)
93 {
94
95         if (sizeof(int) == sizeof(__int64_t))
96                 return(_time_to_time64(t));
97         return((int)t);
98 }
99
100 time_t
101 _int_to_time(int tint)
102 {
103
104         if (sizeof(int) == sizeof(__int32_t))
105                 return(_time32_to_time(tint));
106         return((time_t)tint);
107 }