time32.c revision 85636
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 * $FreeBSD: head/lib/libc/stdtime/time32.c 85636 2001-10-28 20:13:16Z dillon $
1485634Sdillon */
1585634Sdillon
1685634Sdillon#include <sys/types.h>
1785634Sdillon#include <sys/time.h>
1885634Sdillon
1985634Sdillon/*
2085634Sdillon * Convert a 32 bit representation of time_t into time_t.  XXX needs to
2185634Sdillon * implement the 50-year rule to handle post-2038 conversions.
2285634Sdillon */
2385634Sdillontime_t
2485634Sdillontime32_to_time(__int32_t t32)
2585634Sdillon{
2685634Sdillon    return((time_t)t32);
2785634Sdillon}
2885634Sdillon
2985634Sdillon/*
3085634Sdillon * Convert time_t to a 32 bit representation.  If time_t is 64 bits we can
3185634Sdillon * simply chop it down.   The resulting 32 bit representation can be
3285634Sdillon * converted back to a temporally local 64 bit time_t using time32_to_time.
3385634Sdillon */
3485634Sdillon__int32_t
3585634Sdillontime_to_time32(time_t t)
3685634Sdillon{
3785634Sdillon    return((__int32_t)t);
3885634Sdillon}
3985634Sdillon
4085634Sdillon/*
4185634Sdillon * Convert a 64 bit representation of time_t into time_t.  If time_t is
4285634Sdillon * represented as 32 bits we can simply chop it and not support times
4385634Sdillon * past 2038.
4485634Sdillon */
4585634Sdillontime_t
4685634Sdillontime64_to_time(__int64_t t64)
4785634Sdillon{
4885634Sdillon    return((time_t)t64);
4985634Sdillon}
5085634Sdillon
5185634Sdillon/*
5285634Sdillon * Convert time_t to a 64 bit representation.  If time_t is represented
5385634Sdillon * as 32 bits we simply sign-extend and do not support times past 2038.
5485634Sdillon */
5585634Sdillon__int64_t
5685634Sdillontime_to_time64(time_t t)
5785634Sdillon{
5885634Sdillon    return((__int64_t)t);
5985634Sdillon}
6085634Sdillon
6185636Sdillon/*
6285636Sdillon * Convert to/from 'long'.  Depending on the sizeof(long) this may or
6385636Sdillon * may not require using the 50-year rule.
6485636Sdillon */
6585636Sdillonlong
6685636Sdillontime_to_long(time_t t)
6785636Sdillon{
6885636Sdillon    if (sizeof(long) == sizeof(__int64_t))
6985636Sdillon	return(time_to_time64(t));
7085636Sdillon    return((long)t);
7185636Sdillon}
7285636Sdillon
7385636Sdillontime_t
7485636Sdillonlong_to_time(long tlong)
7585636Sdillon{
7685636Sdillon    if (sizeof(long) == sizeof(__int32_t))
7785636Sdillon	return(time32_to_time(tlong));
7885636Sdillon    return((time_t)tlong);
7985636Sdillon}
8085636Sdillon
8185636Sdillon/*
8285636Sdillon * Convert to/from 'int'.  Depending on the sizeof(int) this may or
8385636Sdillon * may not require using the 50-year rule.
8485636Sdillon */
8585636Sdillonint
8685636Sdillontime_to_int(time_t t)
8785636Sdillon{
8885636Sdillon    if (sizeof(int) == sizeof(__int64_t))
8985636Sdillon	return(time_to_time64(t));
9085636Sdillon    return((int)t);
9185636Sdillon}
9285636Sdillon
9385636Sdillontime_t
9485636Sdillonint_to_time(int tint)
9585636Sdillon{
9685636Sdillon    if (sizeof(int) == sizeof(__int32_t))
9785636Sdillon	return(time32_to_time(tint));
9885636Sdillon    return((time_t)tint);
9985636Sdillon}
10085636Sdillon
101