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