185634Sdillon/*- 285634Sdillon * Copyright (c) 2001 FreeBSD Inc. 385634Sdillon * All rights reserved. 485634Sdillon * 585634Sdillon * These routines are for converting time_t to fixed-bit representations 685634Sdillon * for use in protocols or storage. When converting time to a larger 785634Sdillon * representation of time_t these routines are expected to assume temporal 885634Sdillon * locality and use the 50-year rule to properly set the msb bits. XXX 985634Sdillon * 1085634Sdillon * Redistribution and use under the terms of the COPYRIGHT file at the 1185634Sdillon * base of the source tree. 1285634Sdillon */ 1385634Sdillon 1492986Sobrien#include <sys/cdefs.h> 1592986Sobrien__FBSDID("$FreeBSD$"); 1692986Sobrien 1785634Sdillon#include <sys/types.h> 1898313Swollman#include <timeconv.h> 1985634Sdillon 2085634Sdillon/* 2185634Sdillon * Convert a 32 bit representation of time_t into time_t. XXX needs to 2285634Sdillon * implement the 50-year rule to handle post-2038 conversions. 2385634Sdillon */ 2485634Sdillontime_t 2589572Sdillon_time32_to_time(__int32_t t32) 2685634Sdillon{ 2785634Sdillon return((time_t)t32); 2885634Sdillon} 2985634Sdillon 3085634Sdillon/* 3185634Sdillon * Convert time_t to a 32 bit representation. If time_t is 64 bits we can 3285634Sdillon * simply chop it down. The resulting 32 bit representation can be 3385634Sdillon * converted back to a temporally local 64 bit time_t using time32_to_time. 3485634Sdillon */ 3585634Sdillon__int32_t 3689572Sdillon_time_to_time32(time_t t) 3785634Sdillon{ 3885634Sdillon return((__int32_t)t); 3985634Sdillon} 4085634Sdillon 4185634Sdillon/* 4285634Sdillon * Convert a 64 bit representation of time_t into time_t. If time_t is 4385634Sdillon * represented as 32 bits we can simply chop it and not support times 4485634Sdillon * past 2038. 4585634Sdillon */ 4685634Sdillontime_t 4789572Sdillon_time64_to_time(__int64_t t64) 4885634Sdillon{ 4985634Sdillon return((time_t)t64); 5085634Sdillon} 5185634Sdillon 5285634Sdillon/* 5385634Sdillon * Convert time_t to a 64 bit representation. If time_t is represented 5485634Sdillon * as 32 bits we simply sign-extend and do not support times past 2038. 5585634Sdillon */ 5685634Sdillon__int64_t 5789572Sdillon_time_to_time64(time_t t) 5885634Sdillon{ 5985634Sdillon return((__int64_t)t); 6085634Sdillon} 6185634Sdillon 6285636Sdillon/* 6385636Sdillon * Convert to/from 'long'. Depending on the sizeof(long) this may or 6485636Sdillon * may not require using the 50-year rule. 6585636Sdillon */ 6685636Sdillonlong 6789572Sdillon_time_to_long(time_t t) 6885636Sdillon{ 6985636Sdillon if (sizeof(long) == sizeof(__int64_t)) 7089572Sdillon return(_time_to_time64(t)); 7185636Sdillon return((long)t); 7285636Sdillon} 7385636Sdillon 7485636Sdillontime_t 7589572Sdillon_long_to_time(long tlong) 7685636Sdillon{ 7785636Sdillon if (sizeof(long) == sizeof(__int32_t)) 7889572Sdillon return(_time32_to_time(tlong)); 7985636Sdillon return((time_t)tlong); 8085636Sdillon} 8185636Sdillon 8285636Sdillon/* 8385636Sdillon * Convert to/from 'int'. Depending on the sizeof(int) this may or 8485636Sdillon * may not require using the 50-year rule. 8585636Sdillon */ 8685636Sdillonint 8789572Sdillon_time_to_int(time_t t) 8885636Sdillon{ 8985636Sdillon if (sizeof(int) == sizeof(__int64_t)) 9089572Sdillon return(_time_to_time64(t)); 9185636Sdillon return((int)t); 9285636Sdillon} 9385636Sdillon 9485636Sdillontime_t 9589572Sdillon_int_to_time(int tint) 9685636Sdillon{ 9785636Sdillon if (sizeof(int) == sizeof(__int32_t)) 9889572Sdillon return(_time32_to_time(tint)); 9985636Sdillon return((time_t)tint); 10085636Sdillon} 101