如何不使用任何时间函数得到日期时间?
想通过自己计算,
把一个整数转化为日期时间,
好像是通过毫秒的计算,
从1970.1.1 00:00:00时间点开始,
麻烦大家了,
多谢。。。
顺祝新年快乐!
[解决办法]
每天有24*3600秒。所以首先将整数除以24*3600以及求关于24*3600的模。
这样就可以知道离1970.1.1 00:00:00有多少天加多少秒。
根据一天里面离0点多少秒很容易计算出时间,就不用介绍了吧.
而根据天数计算年月日,也不是很难,我们需要了解一下韵年的规律就可以了。
所以每400年,总共有99+400*365天,根据这个可以先计算出过了多少个400年(呵呵,对于我们现在常用的日期,这个部分应该都是0)
然后对于不超过400年的,开始30年(不包含2000年)有7个韵年,所以是30*365+7天。
可以计算出在2000年后还是2000年前
后面部分,第一个100年24个韵年为100*365+24天,后面每100年为25个韵年为100*365+25天,可以计算出到底在2000年后又过了多少个100年余多少天。
对于余下的天数,每4年一个韵年,可以计算出年份。
找出年份后,然后确定这个年份是否韵年,以及在当前年份还余多少天,根据每月天数确定月份和日期。
[解决办法]
这是我的日期函数库的一部分,希望对你有所帮助
#include <time.h>
#include <math.h>
#include <stdio.h>
#include "datetime.h "
int main()
{
unsigned y,m,d,h,mi,s,ms;
DateTime dt;
dt = Now();
DecodeDate(dt,&y,&m,&d);
DecodeTime(dt,&h,&mi,&s,&ms);
printf( "CurrentYear is %d\n ",CurrentYear());
printf( "Now is %lf\n ",(double)dt);
printf( "y=%d,m=%d,d=%d\n ",y,m,d);
printf( "h=%d,mi=%d,s=%d\n ",h,mi,s);
getchar();
return 0;
}
/*{ Date/time support routines }*/
void DateTimeToTimeStamp(DateTime ADateTime,TimeStamp *ts)
{
double ip; //整数部分
double p; //小数部分
p = modf(ADateTime,&ip); //分别取浮点数的整数部分和小数部分
ts-> ts_date = (int)ip + DateDelta;
ts-> ts_time = (int)(p * MSecsPerDay);
}
void ValidateTimeStamp(TimeStamp ts)
{
if(ts.ts_time < 0 || ts.ts_date < 0)
{
fprintf(stderr, "%d.%d is not a valid timestamp\n ",ts.ts_date,ts.ts_time);
exit(1);
}
}
DateTime TimeStampToDateTime(TimeStamp ATimeStamp)
{
double dt;
ValidateTimeStamp(ATimeStamp);
dt = ATimeStamp.ts_date - DateDelta;
dt += ATimeStamp.ts_time / (double)MSecsPerDay;
return ((DateTime) dt);
}
/*Time encoding and decoding*/
int TryEncodeTime(unsigned Hour,unsigned Min,unsigned Sec,unsigned MSec,DateTime *Time)
{
if ((Hour < HoursPerDay) && (Min < MinsPerHour) && (Sec < SecsPerMin) && (MSec < MSecsPerSec))
{
*Time = (Hour * (MinsPerHour * SecsPerMin * MSecsPerSec) +
Min * (SecsPerMin * MSecsPerSec) +
Sec * MSecsPerSec +
MSec) / (double)MSecsPerDay;
return (1);
}
return (0);
}
DateTime EncodeTime(unsigned Hour,unsigned Min,unsigned Sec,unsigned MSec)
{
DateTime dt;
if(TryEncodeTime(Hour,Min,Sec,MSec,&dt))
return dt;
else
{
fprintf(stderr, "EncodeTime():Invalid argument to time encode.\n ");
exit(1);
}
}
void DecodeTime(DateTime ADateTime,unsigned *Hour,unsigned *Min, unsigned *Sec,unsigned *MSec)
{
TimeStamp ts;
int msecCount;
DateTimeToTimeStamp(ADateTime,&ts);
msecCount = ts.ts_time;
*MSec = msecCount % MSecsPerSec;
msecCount /= MSecsPerSec;
*Sec = msecCount % SecsPerMin;
msecCount /= SecsPerMin;
*Min = msecCount % MinsPerHour;
*Hour = msecCount / MinsPerHour;
}
/*Date encoding and decoding*/
int LeapYear(unsigned year)
{
if( year & 3 ) /*如果不能被4整除*/
return( 0 );
if( year % 100 != 0 ) /*能被4整除但不能被100整除*/
return ( 1 );
if( year % 400 == 0 ) /*能被400整除*/
return( 1 );
return( 0 );
}
int TryEncodeDate(unsigned Year,unsigned Month,unsigned Day,DateTime *Date)
{
int daytbl[2][12] = {
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
int daysof[] = {
0,//1月
31,//2
31+28,//3
31+28+31,//4
31+28+31+30,//5
31+28+31+30+31,//6
31+28+31+30+31+30,//7
31+28+31+30+31+30+31,//8
31+28+31+30+31+30+31+31,//9
31+28+31+30+31+30+31+31+30,//10
31+28+31+30+31+30+31+31+30+31,//11
31+28+31+30+31+30+31+31+30+31+30//12
};
if((Year > 0 && Year < 10000) && (Month > 0 && Month < 13) &&
(Day > 0 && Day <= daytbl[LeapYear(Year)][Month-1]))
{
Day += daysof[Month - 1];
if(LeapYear(Year) && Month > 2)
++Day;
--Year;
*Date = (DateTime)(Year * 365 + Year / 4 - Year /100 + Year / 400 + Day -DateDelta);
return 1;
}
return 0;
}
DateTime EncodeDate(unsigned Year,unsigned Month,unsigned Day)
{
DateTime dt;
if(TryEncodeDate(Year,Month,Day,&dt))
return dt;
else
{
fprintf(stderr, "EncodeDate():Invalid argument to date encode ");
exit(1);
}
}
int DecodeDateFully(DateTime ADateTime,unsigned *Year,unsigned *Month,unsigned *Day,unsigned *DOW)
{
const int D1 = 365;
const int D4 = D1 * 4 + 1;
const int D100 = D4 * 25 - 1;
const int D400 = D100 * 4 + 1;
int daytbl[2][12] = {
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
int *daytb;
unsigned y,m,d,i;
int t,ret;
TimeStamp ts;
DateTimeToTimeStamp(ADateTime,&ts);
t = ts.ts_date;
if(t < 0){
*Year = 0;
*Month = 0;
*Day = 0;
*DOW = 0;
return 0;
}
else{
*DOW = t % 7 + 1;
--t;
for(y = 1;t > = D400;t -= D400)
y += 400;
i = t / D100;
d = t % D100;
if(i == 4){
--i;
d += D100;
}
y += i * 100;
i = d / D4;
d %= D4;
y += i * 4;
i = d / D1;
d %= D1;
if(i == 4){
--i;
d += D1;
}
y += i;
ret = LeapYear(y);
daytb = daytbl[ret];
m = 0;
for(;;){
i = daytb[m];
if(d < i)
break;
d -= i;
++m;
}
*Year = y;
*Month = m + 1;
*Day = ++d;
return (ret);
}
}
void DecodeDate(DateTime ADateTime,unsigned *Year, unsigned *Month, unsigned *Day)
{
unsigned dw;
DecodeDateFully(ADateTime,Year,Month,Day,&dw);
}
unsigned DayOfWeek( DateTime ADateTime)
{
TimeStamp ts;
DateTimeToTimeStamp(ADateTime,&ts);
return (ts.ts_date % 7 + 1);
}
DateTime Date(void)
{
time_t local_time;
struct tm *date;
local_time = time(&local_time);
date = localtime(&local_time);
return EncodeDate(date-> tm_year + 1900,date-> tm_mon + 1,date-> tm_mday);
}
DateTime Time(void)
{
time_t local_time;
struct tm *date;
DateTime dt;
local_time = time(&local_time);
date = localtime(&local_time);
dt = EncodeTime(date-> tm_hour,date-> tm_min,date-> tm_sec,0);
return dt;
}
DateTime Now(void)
{
time_t local_time;
struct tm *date;
local_time = time(&local_time);
date = localtime(&local_time);
return (EncodeDate(date-> tm_year + 1900,date-> tm_mon + 1,date-> tm_mday) +
EncodeTime(date-> tm_hour,date-> tm_min,date-> tm_sec,0));
}
unsigned CurrentYear(void)
{
time_t local_time;
struct tm *date;
local_time = time(&local_time);
date = localtime(&local_time);
return (date-> tm_year + 1900);
}
[解决办法]
头文件
#include <time.h>
typedef double DateTime;
//{ Date and time record }
//typedef struct timestamp_s *TimeStampPrt;
typedef struct {
int ts_time; //Number of milliseconds since midnight
int ts_date; //One plus number of days since 1/1/0001
} TimeStamp;
int LeapYear(unsigned year);
void DateTimeToTimeStamp(DateTime ADateTime,TimeStamp *ts);
DateTime TimeStampToDateTime(TimeStamp ATimeStamp);
/*{ EncodeDate encodes the given year, month, and day into a TDateTime value.
The year must be between 1 and 9999, the month must be between 1 and 12,
and the day must be between 1 and N, where N is the number of days in the
specified month. If the specified values are not within range, an
EConvertError exception is raised. The resulting value is the number of
days between 12/30/1899 and the given date. }
*/
//function EncodeDate(Year, Month, Day: Word): TDateTime;
DateTime EncodeDate(unsigned Year,unsigned Month,unsigned Day);
/*{ EncodeTime encodes the given hour, minute, second, and millisecond into a
TDateTime value. The hour must be between 0 and 23, the minute must be
between 0 and 59, the second must be between 0 and 59, and the millisecond
must be between 0 and 999. If the specified values are not within range, an
EConvertError exception is raised. The resulting value is a number between
0 (inclusive) and 1 (not inclusive) that indicates the fractional part of
a day given by the specified time. The value 0 corresponds to midnight,
0.5 corresponds to noon, 0.75 corresponds to 6:00 pm, etc. }
*/
//function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;
DateTime EncodeTime(unsigned Hour,unsigned Min,unsigned Sec,unsigned MSec);
/*{ Instead of generating errors the following variations of EncodeDate and
EncodeTime simply return False if the parameters given are not valid.
Other than that, these functions are functionally the same as the above
functions. }
*/
int TryEncodeDate(unsigned Year,unsigned Month,unsigned Day,DateTime *Date);
int TryEncodeTime(unsigned Hour,unsigned Min,unsigned Sec,unsigned MSec,DateTime *Time);
/*{ DecodeDate decodes the integral (date) part of the given TDateTime value
into its corresponding year, month, and day. If the given TDateTime value
is less than or equal to zero, the year, month, and day return parameters
are all set to zero. }
*/
void DecodeDate(DateTime ADateTime,unsigned *Year, unsigned *Month, unsigned *Day);
/*{ This variation of DecodeDate works similarly to the above function but
returns more information. The result value of this function indicates
whether the year decoded is a leap year or not. }
*/
int DecodeDateFully(DateTime ADateTime,unsigned *Year,unsigned *Month,unsigned *Day,unsigned *DOW);
/*{ DecodeTime decodes the fractional (time) part of the given TDateTime value
into its corresponding hour, minute, second, and millisecond. }
*/
void DecodeTime(DateTime ADateTime,unsigned *Hour,unsigned *Min, unsigned *Sec,unsigned *MSec);
/*{ DayOfWeek returns the day of the week of the given date. The result is an
integer between 1 and 7, corresponding to Sunday through Saturday.
This function is not ISO 8601 compliant, for that see the DateUtils unit. }
*/
unsigned DayOfWeek( DateTime ADateTime);
/*{ Date returns the current date. }*/
DateTime Date(void);
//{ Time returns the current time. }
DateTime Time(void);
//DateTime GetTime(void);
//{ Now returns the current date and time, corresponding to Date + Time. }
DateTime Now(void);
//{ Current year returns the year portion of the date returned by Now }
unsigned CurrentYear(void);
[解决办法]
用整数表达日期,如果要精确到毫秒是不现实的。32位整数精确到秒只能表示130多年;如果精确到毫秒,表示不了一年。
[解决办法]
你可以参照这段代码。就是把一个整数分解成年月日
int DecodeDateFully(DateTime ADateTime,unsigned *Year,unsigned *Month,unsigned *Day,unsigned *DOW)
{
const int D1 = 365 ;
const int D4 = (D1 * 4 + 1) ;
const int D100 = (D4 * 25 - 1) ;
const int D400 = (D100 * 4 + 1) ;
int daytbl[2][12] = {
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
int *daytb;
unsigned y,m,d,i;
int t,ret;
TimeStamp ts;
DateTimeToTimeStamp(ADateTime,&ts);
t = ts.ts_date;
if(t < 0){
*Year = 0;
*Month = 0;
*Day = 0;
*DOW = 0;
return 0;
}
else{
*DOW = t % 7 + 1;
--t;
for(y = 1;t > = D400;t -= D400)
y += 400;
i = t / D100;
d = t % D100;
if(i == 4){
--i;
d += D100;
}
y += i * 100;
i = d / D4;
d %= D4;
y += i * 4;
i = d / D1;
d %= D1;
if(i == 4){
--i;
d += D1;
}
y += i;
ret = LeapYear(y);
daytb = daytbl[ret];
m = 0;
for(;;){
i = daytb[m];
if(d < i)
break;
d -= i;
++m;
}
*Year = y;
*Month = m + 1;
*Day = ++d;
return (ret);
}
}
[解决办法]
但是现在有个问题,即:不知道怎样得到总共有多少年?
恩?
这个就是所使用的数据类型的极限值,
比如 long 类型的极值,
除以 每年的最低精度累计值(比如秒: 365*24*3600)不就是这个最大可以表示的年么?