1237c4e3aSMatthew Dillon /*- 2237c4e3aSMatthew Dillon * Copyright (c) 2001 FreeBSD Inc. 3237c4e3aSMatthew Dillon * All rights reserved. 4237c4e3aSMatthew Dillon * 5237c4e3aSMatthew Dillon * These routines are for converting time_t to fixed-bit representations 6237c4e3aSMatthew Dillon * for use in protocols or storage. When converting time to a larger 7237c4e3aSMatthew Dillon * representation of time_t these routines are expected to assume temporal 8237c4e3aSMatthew Dillon * locality and use the 50-year rule to properly set the msb bits. XXX 9237c4e3aSMatthew Dillon * 10237c4e3aSMatthew Dillon * Redistribution and use under the terms of the COPYRIGHT file at the 11237c4e3aSMatthew Dillon * base of the source tree. 12237c4e3aSMatthew Dillon */ 13237c4e3aSMatthew Dillon 14237c4e3aSMatthew Dillon #include <sys/types.h> 158466ae90SGarrett Wollman #include <timeconv.h> 16237c4e3aSMatthew Dillon 17237c4e3aSMatthew Dillon /* 18237c4e3aSMatthew Dillon * Convert a 32 bit representation of time_t into time_t. XXX needs to 19237c4e3aSMatthew Dillon * implement the 50-year rule to handle post-2038 conversions. 20237c4e3aSMatthew Dillon */ 21237c4e3aSMatthew Dillon time_t _time32_to_time(__int32_t t32)22170ac683SMatthew Dillon_time32_to_time(__int32_t t32) 23237c4e3aSMatthew Dillon { 24237c4e3aSMatthew Dillon return((time_t)t32); 25237c4e3aSMatthew Dillon } 26237c4e3aSMatthew Dillon 27237c4e3aSMatthew Dillon /* 28237c4e3aSMatthew Dillon * Convert time_t to a 32 bit representation. If time_t is 64 bits we can 29237c4e3aSMatthew Dillon * simply chop it down. The resulting 32 bit representation can be 30237c4e3aSMatthew Dillon * converted back to a temporally local 64 bit time_t using time32_to_time. 31237c4e3aSMatthew Dillon */ 32237c4e3aSMatthew Dillon __int32_t _time_to_time32(time_t t)33170ac683SMatthew Dillon_time_to_time32(time_t t) 34237c4e3aSMatthew Dillon { 35237c4e3aSMatthew Dillon return((__int32_t)t); 36237c4e3aSMatthew Dillon } 37237c4e3aSMatthew Dillon 38237c4e3aSMatthew Dillon /* 39237c4e3aSMatthew Dillon * Convert a 64 bit representation of time_t into time_t. If time_t is 40237c4e3aSMatthew Dillon * represented as 32 bits we can simply chop it and not support times 41237c4e3aSMatthew Dillon * past 2038. 42237c4e3aSMatthew Dillon */ 43237c4e3aSMatthew Dillon time_t _time64_to_time(__int64_t t64)44170ac683SMatthew Dillon_time64_to_time(__int64_t t64) 45237c4e3aSMatthew Dillon { 46237c4e3aSMatthew Dillon return((time_t)t64); 47237c4e3aSMatthew Dillon } 48237c4e3aSMatthew Dillon 49237c4e3aSMatthew Dillon /* 50237c4e3aSMatthew Dillon * Convert time_t to a 64 bit representation. If time_t is represented 51237c4e3aSMatthew Dillon * as 32 bits we simply sign-extend and do not support times past 2038. 52237c4e3aSMatthew Dillon */ 53237c4e3aSMatthew Dillon __int64_t _time_to_time64(time_t t)54170ac683SMatthew Dillon_time_to_time64(time_t t) 55237c4e3aSMatthew Dillon { 56237c4e3aSMatthew Dillon return((__int64_t)t); 57237c4e3aSMatthew Dillon } 58237c4e3aSMatthew Dillon 59e8627df6SMatthew Dillon /* 60e8627df6SMatthew Dillon * Convert to/from 'long'. Depending on the sizeof(long) this may or 61e8627df6SMatthew Dillon * may not require using the 50-year rule. 62e8627df6SMatthew Dillon */ 63e8627df6SMatthew Dillon long _time_to_long(time_t t)64170ac683SMatthew Dillon_time_to_long(time_t t) 65e8627df6SMatthew Dillon { 66e8627df6SMatthew Dillon if (sizeof(long) == sizeof(__int64_t)) 67170ac683SMatthew Dillon return(_time_to_time64(t)); 68e8627df6SMatthew Dillon return((long)t); 69e8627df6SMatthew Dillon } 70e8627df6SMatthew Dillon 71e8627df6SMatthew Dillon time_t _long_to_time(long tlong)72170ac683SMatthew Dillon_long_to_time(long tlong) 73e8627df6SMatthew Dillon { 74e8627df6SMatthew Dillon if (sizeof(long) == sizeof(__int32_t)) 75170ac683SMatthew Dillon return(_time32_to_time(tlong)); 76e8627df6SMatthew Dillon return((time_t)tlong); 77e8627df6SMatthew Dillon } 78e8627df6SMatthew Dillon 79e8627df6SMatthew Dillon /* 80e8627df6SMatthew Dillon * Convert to/from 'int'. Depending on the sizeof(int) this may or 81e8627df6SMatthew Dillon * may not require using the 50-year rule. 82e8627df6SMatthew Dillon */ 83e8627df6SMatthew Dillon int _time_to_int(time_t t)84170ac683SMatthew Dillon_time_to_int(time_t t) 85e8627df6SMatthew Dillon { 86e8627df6SMatthew Dillon if (sizeof(int) == sizeof(__int64_t)) 87170ac683SMatthew Dillon return(_time_to_time64(t)); 88e8627df6SMatthew Dillon return((int)t); 89e8627df6SMatthew Dillon } 90e8627df6SMatthew Dillon 91e8627df6SMatthew Dillon time_t _int_to_time(int tint)92170ac683SMatthew Dillon_int_to_time(int tint) 93e8627df6SMatthew Dillon { 94e8627df6SMatthew Dillon if (sizeof(int) == sizeof(__int32_t)) 95170ac683SMatthew Dillon return(_time32_to_time(tint)); 96e8627df6SMatthew Dillon return((time_t)tint); 97e8627df6SMatthew Dillon } 98