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