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 14 #include <sys/cdefs.h> 15 #include <sys/types.h> 16 #include <timeconv.h> 17 18 /* 19 * Convert a 32 bit representation of time_t into time_t. XXX needs to 20 * implement the 50-year rule to handle post-2038 conversions. 21 */ 22 time_t 23 _time32_to_time(__int32_t t32) 24 { 25 return((time_t)t32); 26 } 27 28 /* 29 * Convert time_t to a 32 bit representation. If time_t is 64 bits we can 30 * simply chop it down. The resulting 32 bit representation can be 31 * converted back to a temporally local 64 bit time_t using time32_to_time. 32 */ 33 __int32_t 34 _time_to_time32(time_t t) 35 { 36 return((__int32_t)t); 37 } 38 39 /* 40 * Convert a 64 bit representation of time_t into time_t. If time_t is 41 * represented as 32 bits we can simply chop it and not support times 42 * past 2038. 43 */ 44 time_t 45 _time64_to_time(__int64_t t64) 46 { 47 return((time_t)t64); 48 } 49 50 /* 51 * Convert time_t to a 64 bit representation. If time_t is represented 52 * as 32 bits we simply sign-extend and do not support times past 2038. 53 */ 54 __int64_t 55 _time_to_time64(time_t t) 56 { 57 return((__int64_t)t); 58 } 59 60 /* 61 * Convert to/from 'long'. Depending on the sizeof(long) this may or 62 * may not require using the 50-year rule. 63 */ 64 long 65 _time_to_long(time_t t) 66 { 67 if (sizeof(long) == sizeof(__int64_t)) 68 return(_time_to_time64(t)); 69 return((long)t); 70 } 71 72 time_t 73 _long_to_time(long tlong) 74 { 75 if (sizeof(long) == sizeof(__int32_t)) 76 return(_time32_to_time(tlong)); 77 return((time_t)tlong); 78 } 79 80 /* 81 * Convert to/from 'int'. Depending on the sizeof(int) this may or 82 * may not require using the 50-year rule. 83 */ 84 int 85 _time_to_int(time_t t) 86 { 87 if (sizeof(int) == sizeof(__int64_t)) 88 return(_time_to_time64(t)); 89 return((int)t); 90 } 91 92 time_t 93 _int_to_time(int tint) 94 { 95 if (sizeof(int) == sizeof(__int32_t)) 96 return(_time32_to_time(tint)); 97 return((time_t)tint); 98 } 99