在C语言中没有直接将日期转换为double型的函数,但是在将日期类型保存到文件时往往需要日期的转换。
/* ************************************************************************************************************
* conversion between double type and datetime type
* wxz 2011-08-29
* Compiled byVC6.0and VC2010
**************************************************************************************************************/
/**********************************************
* EncodeDateTime.h
**********************************************/
#ifndef EncodeDateTime_H
#define EncodeDateTime_H
typedef unsigned short word;//unsigned two bytes
//convert datetime to double type
extern double EncodeDateTime(word year,word month,word day,word hour,word min,word sec,word msec); //convert double to datetime type
extern void DecodeDateTime(double dt,word *year,word *month,word *day,word *hour,word *min,word *sec,word *msec);
//extern char *DecodeDateTime(char* dtStr, double dt,word *year,word *month,word *day,word *hour,word *min,word *sec,word *msec);
#endif
/* ********************************************************
* conversion between double type and datetime type
* EncodeDateTime.c
***********************************************************/
#include
#include "EncodeDateTime.h"
const int DateDelta = 693594;// Days between 1/1/0001 and 12/31/1899
const int HoursPerDay = 24;//hours per day
const int MinsPerHour = 60;//minutes per hour
const int SecsPerMin = 60;//seconds per minute
const int MSecsPerSec = 1000;//milliseconds per second
const double MinDateTime= -657434.0; // 01/01/0100 12:00:00.000 AM
const double MaxDateTime= 2958465.99999; // 12/31/9999 11:59:59.999 PM
void DivMod(int dividend,int divisor,word *result,word *remainder)
{
*result=dividend/divisor;
*remainder=dividend%divisor;
}
//convert datetime to double type
double EncodeDateTime(word year,word month,word day,word hour,word min,word sec,word msec)
{
const int MinsPerDay = HoursPerDay * MinsPerHour;//minutes per day
const int SecsPerDay = MinsPerDay * SecsPerMin; //seconds per day
const int SecsPerHour = SecsPerMin * MinsPerHour; //seconds per hour
const int MSecsPerDay = SecsPerDay * MSecsPerSec; //milliseconds per day
//
int i;
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
double date;
double time;
date=0;
time=0;
if ((year >= 1) && (year <= 9999) && (month >= 1) && (month <= 12))
{
//leap year
if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) days[2]=29;
if ((day >= 1) && (day <= days[month]))
{
for( i=1;i<=month-1;i++) day+=days[i];
year-=1;
date= year * 365 + year / 4 - year / 100 + year / 400 + day - DateDelta;
if ((hour < HoursPerDay) && (min < MinsPerHour) && (sec < SecsPerMin) && (msec < MSecsPerSec))
{
time = (hour * (MinsPerHour * SecsPerMin * MSecsPerSec) +
min * (SecsPerMin * MSecsPerSec) +
sec * MSecsPerSec +
msec);
time/=MSecsPerDay;
}
return date+time;
}
}
return 1; // 12/31/1899
}
//convert double to datetime type
void DecodeDateTime(double dt,word *year,word *month,word *day,word *hour,word *min,word *sec,word *msec)
{
const int MinsPerDay = HoursPerDay * MinsPerHour;//minutes per day
const int SecsPerDay = MinsPerDay * SecsPerMin; //seconds per day
const int SecsPerHour = SecsPerMin * MinsPerHour; //seconds per hour
const int MSecsPerDay = SecsPerDay * MSecsPerSec; //milliseconds per day
const int D1 = 365;
const int D4 = D1 * 4 + 1;
const int D100 = D4 * 25 - 1;
const int D400 = D100 * 4 + 1;
//
word y,m,d,r,temp;
word hh,mm,ss,mss;
char str[20]; //
int ipart;//integer part
double fpart;//float part
int date,time; //date and time
//int MinCount, MSecCount;
word MinCount, MSecCount;
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
ipart=0;
fpart=0;
y=0;m=0;d=0;r=0,temp=0;
hh=0;mm=0;ss=0;mss=0;
MinCount=0;
MSecCount=0;
if(!(dt< MinDateTime)||(dt >(int)MaxDateTime+1.0))
{
ipart=(int)(dt);
fpart=dt-ipart;
date=ipart+DateDelta;
time=(int)(MSecsPerDay*fpart);
//-------------------------decode date-------------------------
if (date<=0)
{
*year=1899;
*month=12;
*day=31;
*hour=0;
*min=0;
*sec=0;
*msec=0;
}
else
{
temp=date % 7 +1;
date-=1;
y=1;
while(date >= D400)
{
date-=D400;
y+=400;
}
DivMod(date,D100,&r,&d);
if (r==4)
{
r-=1;
d+=D100;
}
y+=r*100;
DivMod(d, D4, &r,&d);
y+=r*4;
DivMod(d,D1,&r,&d);
if (r==4)
{
r-=1;
d+=D1;
}
y+=r;
//leap year
if ((y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0))) days[2]=29;
m = 1;
while(1)
{
r=days[m];
if (d< r) break;
d-=r;
m+=1;
}
*year = y;
*month = m;
*day = d + 1;
}
//-------------decode time----------------------------------------
DivMod(time, SecsPerMin * MSecsPerSec, &MinCount, &MSecCount);
DivMod(MinCount, MinsPerHour, &hh, &mm);
DivMod(MSecCount, MSecsPerSec, &ss, &mss);
*hour=hh;
*min=mm;
*sec=ss;
*msec=mss;
}
}
/* ********************************************************
* Test.c
**********************************************************/
#include "EncodeDateTime.h"
#include
int main()
{
word year,month,day,hour,min,sec,msec;
double dt;
char str[20];
dt=EncodeDateTime(2011,8,29,16,21,40,0);
DecodeDateTime(dt,&year,&month,&day,&hour,&min,&sec,&msec);
sprintf(str,"%04d%02d%02d%02d%02d%02d",year,month,day,hour,min,sec);
printf("%s ",str);
scanf(str);
return 0;
}