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