time32.c revision 92986
1176227Sbde/*-
2176227Sbde * Copyright (c) 2001 FreeBSD Inc.
3176227Sbde * All rights reserved.
4176227Sbde *
5176227Sbde * These routines are for converting time_t to fixed-bit representations
6176227Sbde * for use in protocols or storage.  When converting time to a larger
7176227Sbde * representation of time_t these routines are expected to assume temporal
8176227Sbde * locality and use the 50-year rule to properly set the msb bits.  XXX
9176227Sbde *
10176227Sbde * Redistribution and use under the terms of the COPYRIGHT file at the
11176227Sbde * base of the source tree.
12176227Sbde */
13176227Sbde
14176227Sbde#include <sys/cdefs.h>
15176227Sbde__FBSDID("$FreeBSD: head/lib/libc/stdtime/time32.c 92986 2002-03-22 21:53:29Z obrien $");
16176227Sbde
17176227Sbde#include <sys/types.h>
18176227Sbde#include <sys/time.h>
19176227Sbde
20176227Sbde/*
21176227Sbde * Convert a 32 bit representation of time_t into time_t.  XXX needs to
22176227Sbde * implement the 50-year rule to handle post-2038 conversions.
23176227Sbde */
24217108Skibtime_t
25217108Skib_time32_to_time(__int32_t t32)
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    return((__int32_t)t);
39}
40
41/*
42 * Convert a 64 bit representation of time_t into time_t.  If time_t is
43 * represented as 32 bits we can simply chop it and not support times
44 * past 2038.
45 */
46time_t
47_time64_to_time(__int64_t t64)
48{
49    return((time_t)t64);
50}
51
52/*
53 * Convert time_t to a 64 bit representation.  If time_t is represented
54 * as 32 bits we simply sign-extend and do not support times past 2038.
55 */
56__int64_t
57_time_to_time64(time_t t)
58{
59    return((__int64_t)t);
60}
61
62/*
63 * Convert to/from 'long'.  Depending on the sizeof(long) this may or
64 * may not require using the 50-year rule.
65 */
66long
67_time_to_long(time_t t)
68{
69    if (sizeof(long) == sizeof(__int64_t))
70	return(_time_to_time64(t));
71    return((long)t);
72}
73
74time_t
75_long_to_time(long tlong)
76{
77    if (sizeof(long) == sizeof(__int32_t))
78	return(_time32_to_time(tlong));
79    return((time_t)tlong);
80}
81
82/*
83 * Convert to/from 'int'.  Depending on the sizeof(int) this may or
84 * may not require using the 50-year rule.
85 */
86int
87_time_to_int(time_t t)
88{
89    if (sizeof(int) == sizeof(__int64_t))
90	return(_time_to_time64(t));
91    return((int)t);
92}
93
94time_t
95_int_to_time(int tint)
96{
97    if (sizeof(int) == sizeof(__int32_t))
98	return(_time32_to_time(tint));
99    return((time_t)tint);
100}
101