?? time.c
字號:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (c) 2007 by Qu chun lei watt@vip.163.com
*
* Filename: TIME.C
* Module: 0404C
* Language: ANSI C
* $Revision: 18 $
*
* Math and conversion functions which operate on time values. For details
* see TIME.H.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef TIME_C
#define TIME_C
/*------------------------*
| Include header files
*------------------------*/
#include "time.h"
/*-------------*
| Constants
*-------------*/
#define TIME_MAX 0xFFFF
/*-------------------------------*
| Private function prototypes
*-------------------------------*/
#if TIME_OF_DAY_ADD || TIME_ADD
static TTime TimeAddMod(TTime t1, TTime t2, UINT8 modulus);
#endif
#if TIME_OF_DAY_SUBTRACT || TIME_SUBTRACT
static TTime TimeSubtractMod(TTime t1, TTime t2, UINT8 modulus);
#endif
#if TIME_OF_DAY_ADD || TIME_ADD || TIME_OF_DAY_SUBTRACT || TIME_SUBTRACT
static TTimeOfDay TimeOfDayCalculate(TTimeOfDay t1, TTime t2,
TTimeOfDayFormat tt, BOOLEAN addition);
static TTime TimeCalculate(TTime t1, TTime t2, BOOLEAN addition);
#endif
/*--------------------*
| Public functions
*--------------------*/
/*---------------------------------------------------------------------------*
| TimeOfDayAdd
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_OF_DAY_ADD
TTimeOfDay TimeOfDayAdd(TTimeOfDay t1, TTime t2, TTimeOfDayFormat tt)
{
return TimeOfDayCalculate(t1, t2, tt, 1);
}
#endif
/*---------------------------------------------------------------------------*
| TimeOfDaySubtract
|
| day value.
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_OF_DAY_SUBTRACT
TTimeOfDay TimeOfDaySubtract(TTimeOfDay t1, TTime t2, TTimeOfDayFormat tt)
{
return TimeOfDayCalculate(t1, t2, tt, 0);
}
#endif
/*---------------------------------------------------------------------------*
| TimeAdd
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_ADD
TTime TimeAdd(TTime t1, TTime t2)
{
return TimeCalculate(t1, t2, 1);
}
#endif
/*---------------------------------------------------------------------------*
| TimeSubtract
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_SUBTRACT
TTime TimeSubtract(TTime t1, TTime t2)
{
return TimeCalculate(t1, t2, 0);
}
#endif
/*---------------------------------------------------------------------------*
| TimeCorrectFormat
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_CORRECT_FORMAT
TTime TimeCorrectFormat(TTime timeValue)
{
TTime result;
/* bcd digit check */
if(((timeValue & 0xF000) > 0x9000) || ((timeValue & 0x0F00) > 0x0900) ||
((timeValue & 0x00F0) > 0x0090) || ((timeValue & 0x000F) > 0x0009))
{
result = TIME_ERROR;
}
else
{ /* if 2 ls digits > 59, subtract 60, add 1 to 2 ms digits */
if((timeValue & 0x00f0) > 0x0050)
{
timeValue -= 0x0060;
timeValue += 0x0100;
if((timeValue & 0x0f00) > 0x0900)
{
timeValue -= 0x0A00;
timeValue += 0x1000;
}
}
if((timeValue & 0xFF00) > 0x9900) /* Check overflow in 2 MSD's */
result = TIME_ERROR;
else
result = timeValue;
}
return(result);
}
#endif
/*---------------------------------------------------------------------------*
| TimeValid
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_VALID
BOOLEAN TimeValid(TTime timeValue)
{
BOOLEAN result;
/* bcd digit check, ls 2 digits must be <= 59 */
if(((timeValue & 0xF000) > 0x9000) || ((timeValue & 0x0F00) > 0x0900) ||
((timeValue & 0x00F0) > 0x0050) || ((timeValue & 0x000F) > 0x0009))
{
result = FALSE;
}
else
{
result = TRUE;
}
return(result);
}
#endif
/*---------------------------------------------------------------------------*
| TimeOfDayValid
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_OF_DAY_VALID
BOOLEAN TimeOfDayValid(TTimeOfDay timeValue, TTimeOfDayFormat tt)
{
BOOLEAN result;
/* BCD digit check, ls 2 digits must be <= 59 */
if(((timeValue & 0xF000) > 0x9000) || ((timeValue & 0x0F00) > 0x0900) ||
((timeValue & 0x00F0) > 0x0050) || ((timeValue & 0x000F) > 0x0009))
{
result = FALSE;
}
else
{
timeValue &= 0xFF00; /* Range check */
if(tt == TIME_12_HOUR)
{
if((timeValue < 0x0100) || (timeValue > 0x1200))
result = FALSE;
else
result = TRUE;
}
else
{
if(timeValue > 0x2300)
result = FALSE;
else
result = TRUE;
}
}
return(result);
}
#endif
/*---------------------------------------------------------------------------*
| TimeToBinary
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_TO_BINARY
UINT16 TimeToBinary(TTime bcdTime)
{
UINT16 result;
result = (bcdTime & 0x000f);
result += (((bcdTime & 0x00F0)/0x0010)*10);
result += (((bcdTime & 0x0F00)/0x0100)*60);
result += (((bcdTime & 0xF000)/0x1000)*600);
return(result);
}
#endif
/*---------------------------------------------------------------------------*
| TimeFromBinary
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_FROM_BINARY
TTime TimeFromBinary(UINT16 binaryTime)
{
TTime result;
UINT16 scratch;
scratch = binaryTime/600;
result = (scratch * 0x1000);
binaryTime -= (scratch * 600);
scratch = binaryTime/60;
result += (scratch * 0x0100);
binaryTime -= (scratch * 60);
scratch = binaryTime/10;
result += (scratch * 0x0010);
binaryTime -= (scratch * 10);
result += binaryTime;
return(result);
}
#endif
/*---------------------------------------------------------------------------*
| Time12ToBinary
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_12_TO_BINARY
UINT16 Time12ToBinary(TTime bcdTime)
{
UINT16 result;
result = TimeToBinary(bcdTime);
if(result >= 720)
result -= 720;
return(result);
}
#endif
/*---------------------------------------------------------------------------*
| Time12FromBinary
|
| For details see TIME.H
*---------------------------------------------------------------------------*/
#if TIME_12_FROM_BINARY
TTime Time12FromBinary(UINT16 binaryTime)
{
return TimeFromBinary(binaryTime < 60 ? binaryTime + 720: binaryTime);
}
#endif
/*---------------------*
| Private functions
*---------------------*/
/*---------------------------------------------------------------------------*
| TimeAddMod result = t1 + t2
|
| Add two bcd time values and return a time value.
|
| On Entry: t1 is the first time value to add
| t2 is the second time value to add
|
| On Exit: Returns the 4-digit sum
*---------------------------------------------------------------------------*/
#if TIME_OF_DAY_ADD || TIME_ADD
static TTime TimeAddMod(TTime t1, TTime t2, UINT8 modulus)
{
TTime result;
UINT16 decimal, carry;
UINT8 hour1, hour2;
decimal = (t1 & 0xF) + (t2 & 0xF); /* Low nibble */
carry = decimal / 10;
decimal %= 10;
result = (t1 & 0xF0) + (t2 & 0xF0) + (carry*0x10);
carry = result / 0x60;
result = (result % 0x60) + decimal; /* Add in low nibble */
hour1 = (UINT8)(t1/0x100); /* Modulus check on high byte */
hour2 = (UINT8)(t2/0x100);
decimal = (hour1 & 0xF) + (hour2 & 0xF) + ((hour1/0x10) + (hour2/0x10))*10
+ carry;
decimal %= modulus;
result += (decimal % 10 + (decimal / 10) * 0x10)*0x100;
return result;
}
#endif
/*---------------------------------------------------------------------------*
| TimeSubtractMod result = t1 - t2
|
| Subtract time from a time value and return a time value
| result = t1 - t2
|
| On Entry: t1 is the first time value to subtract
| t2 is the second time value to subtract
|
| On Exit: Returns the 4-digit difference
*---------------------------------------------------------------------------*/
#if TIME_OF_DAY_SUBTRACT || TIME_SUBTRACT
static TTime TimeSubtractMod(TTime t1, TTime t2, UINT8 modulus)
{
TTime result;
UINT16 borrow, scratch;
/*-----------*
| Minutes
*-----------*/
if((result = (t1 & 0xF) - (t2 & 0xF)) > 9)
{
borrow = 0x10;
result += 10;
}
else
borrow = 0;
if((scratch = (t1 & 0xF0) - (t2 & 0xF0) - borrow) > 0x50)
{
scratch += 0x60;
borrow = 0x100;
}
else
borrow = 0;
result += scratch; /* Result now contains minutes */
/*---------*
| Hours
*---------*/
scratch = ((t1&0xF000) / 0x10)*10 + (t1 & 0xF00) - borrow;
scratch -= ((t2&0xF000) / 0x10)*10 + (t2 & 0xF00);
while(scratch > (0x100 * modulus))
scratch += (0x100 * modulus);
scratch /= 0x100;
scratch = scratch/10*0x10 + (scratch%10);
scratch *= 0x100;
result += scratch;
return result;
}
#endif
#if TIME_OF_DAY_ADD || TIME_ADD || TIME_OF_DAY_SUBTRACT || TIME_SUBTRACT
/*---------------------------------------------------------------------------*
| TimeOfDayCalculate
|
| Perform math function requested on t1 and t2 and return the result.
| If addition then result = t1 + t2, otherwise result = t1 - t2
|
| On Entry: t1 is the first time of day value to calculate
| t2 is the second time of day value to calculate
|
| On Exit: Returns the result in BCD format
*---------------------------------------------------------------------------*/
static TTimeOfDay TimeOfDayCalculate(TTimeOfDay t1, TTime t2,
TTimeOfDayFormat tt, BOOLEAN addition)
{
TTimeOfDay result;
UINT8 modulus;
if(TimeOfDayValid(t1, tt) && TimeValid(t2))
{
if(tt == TIME_12_HOUR) /* 1:00-12:00 -> 0:00-11:00 */
{
modulus = 12;
t1 -= 0x0100;
if((t1 & 0x0F00) > 0x0900)
t1 -= 0x0600;
}
else
modulus = 24;
if(addition)
result = TimeAddMod(t1, t2, modulus);
else
result = TimeSubtractMod(t1, t2, modulus);
if(tt == TIME_12_HOUR) /* 0:00-11:00 -> 1:00-12:00 */
{
result += 0x0100;
if((result & 0x0F00) > 0x0900)
result += 0x0600;
}
}
else
result = TIME_ERROR;
return(result);
}
#endif
#if TIME_OF_DAY_ADD || TIME_ADD || TIME_OF_DAY_SUBTRACT || TIME_SUBTRACT
/*---------------------------------------------------------------------------*
| TimeCalculate
|
| Perform math function requested on t1 and t2 and return the result.
| If addition then result = t1 + t2, otherwise result = t1 - t2
|
| On Entry: t1 is the first time value to calculate
| t2 is the second time value to calculate
|
| On Exit: Returns the result in BCD format
*---------------------------------------------------------------------------*/
static TTime TimeCalculate(TTime t1, TTime t2, BOOLEAN addition)
{
TTime result;
if(TimeValid(t1) && TimeValid(t2))
{
if(addition)
result = TimeAddMod(t1, t2, (UINT8) TIME_MAX);
else
result = TimeSubtractMod(t1, t2, (UINT8) TIME_MAX);
if(!TimeValid(result))
result = TIME_ERROR;
}
else
result = TIME_ERROR;
return(result);
}
#endif
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -