2001-10-28 19:54:49 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2001 FreeBSD Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* These routines are for converting time_t to fixed-bit representations
|
|
|
|
* for use in protocols or storage. When converting time to a larger
|
|
|
|
* representation of time_t these routines are expected to assume temporal
|
|
|
|
* locality and use the 50-year rule to properly set the msb bits. XXX
|
|
|
|
*
|
|
|
|
* Redistribution and use under the terms of the COPYRIGHT file at the
|
|
|
|
* base of the source tree.
|
|
|
|
*/
|
|
|
|
|
2002-03-22 21:53:29 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-10-28 19:54:49 +00:00
|
|
|
#include <sys/types.h>
|
2002-06-17 01:42:33 +00:00
|
|
|
#include <timeconv.h>
|
2001-10-28 19:54:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a 32 bit representation of time_t into time_t. XXX needs to
|
|
|
|
* implement the 50-year rule to handle post-2038 conversions.
|
|
|
|
*/
|
|
|
|
time_t
|
2002-01-19 23:20:02 +00:00
|
|
|
_time32_to_time(__int32_t t32)
|
2001-10-28 19:54:49 +00:00
|
|
|
{
|
|
|
|
return((time_t)t32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert time_t to a 32 bit representation. If time_t is 64 bits we can
|
|
|
|
* simply chop it down. The resulting 32 bit representation can be
|
|
|
|
* converted back to a temporally local 64 bit time_t using time32_to_time.
|
|
|
|
*/
|
|
|
|
__int32_t
|
2002-01-19 23:20:02 +00:00
|
|
|
_time_to_time32(time_t t)
|
2001-10-28 19:54:49 +00:00
|
|
|
{
|
|
|
|
return((__int32_t)t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a 64 bit representation of time_t into time_t. If time_t is
|
|
|
|
* represented as 32 bits we can simply chop it and not support times
|
|
|
|
* past 2038.
|
|
|
|
*/
|
|
|
|
time_t
|
2002-01-19 23:20:02 +00:00
|
|
|
_time64_to_time(__int64_t t64)
|
2001-10-28 19:54:49 +00:00
|
|
|
{
|
|
|
|
return((time_t)t64);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert time_t to a 64 bit representation. If time_t is represented
|
|
|
|
* as 32 bits we simply sign-extend and do not support times past 2038.
|
|
|
|
*/
|
|
|
|
__int64_t
|
2002-01-19 23:20:02 +00:00
|
|
|
_time_to_time64(time_t t)
|
2001-10-28 19:54:49 +00:00
|
|
|
{
|
|
|
|
return((__int64_t)t);
|
|
|
|
}
|
|
|
|
|
2001-10-28 20:13:16 +00:00
|
|
|
/*
|
|
|
|
* Convert to/from 'long'. Depending on the sizeof(long) this may or
|
|
|
|
* may not require using the 50-year rule.
|
|
|
|
*/
|
|
|
|
long
|
2002-01-19 23:20:02 +00:00
|
|
|
_time_to_long(time_t t)
|
2001-10-28 20:13:16 +00:00
|
|
|
{
|
|
|
|
if (sizeof(long) == sizeof(__int64_t))
|
2002-01-19 23:20:02 +00:00
|
|
|
return(_time_to_time64(t));
|
2001-10-28 20:13:16 +00:00
|
|
|
return((long)t);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
2002-01-19 23:20:02 +00:00
|
|
|
_long_to_time(long tlong)
|
2001-10-28 20:13:16 +00:00
|
|
|
{
|
|
|
|
if (sizeof(long) == sizeof(__int32_t))
|
2002-01-19 23:20:02 +00:00
|
|
|
return(_time32_to_time(tlong));
|
2001-10-28 20:13:16 +00:00
|
|
|
return((time_t)tlong);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert to/from 'int'. Depending on the sizeof(int) this may or
|
|
|
|
* may not require using the 50-year rule.
|
|
|
|
*/
|
|
|
|
int
|
2002-01-19 23:20:02 +00:00
|
|
|
_time_to_int(time_t t)
|
2001-10-28 20:13:16 +00:00
|
|
|
{
|
|
|
|
if (sizeof(int) == sizeof(__int64_t))
|
2002-01-19 23:20:02 +00:00
|
|
|
return(_time_to_time64(t));
|
2001-10-28 20:13:16 +00:00
|
|
|
return((int)t);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
2002-01-19 23:20:02 +00:00
|
|
|
_int_to_time(int tint)
|
2001-10-28 20:13:16 +00:00
|
|
|
{
|
|
|
|
if (sizeof(int) == sizeof(__int32_t))
|
2002-01-19 23:20:02 +00:00
|
|
|
return(_time32_to_time(tint));
|
2001-10-28 20:13:16 +00:00
|
|
|
return((time_t)tint);
|
|
|
|
}
|