时间间隔问题

54286 2004-07-23 09:30:34
小弟初学C语言,不知道算两个日期之间
隔了多少天用什么现成的函数啊!
...全文
218 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
renheihei 2004-07-24
  • 打赏
  • 举报
回复
下面这个头文件加到程序中,直接就可以用了
//: C09:Cpptime.h
// A simple time class
#ifndef CPPTIME_H
#define CPPTIME_H
#include <ctime>
#include <cstring>

class Time {
std::time_t t;
std::tm local;
char asciiRep[26];
unsigned char lflag, aflag;
void updateLocal() {
if(!lflag) {
local = *std::localtime(&t);
lflag++;
}
}
void updateAscii() {
if(!aflag) {
updateLocal();
std::strcpy(asciiRep,std::asctime(&local));
aflag++;
}
}
public:
Time() { mark(); }
void mark() {
lflag = aflag = 0;
std::time(&t);
}
const char* ascii() {
updateAscii();
return asciiRep;
}
// Difference in seconds:
int delta(Time* dt) const {
return int(std::difftime(t, dt->t));
}
int daylightSavings() {
updateLocal();
return local.tm_isdst;
}
int dayOfYear() { // Since January 1
updateLocal();
return local.tm_yday;
}
int dayOfWeek() { // Since Sunday
updateLocal();
return local.tm_wday;
}
int since1900() { // Years since 1900
updateLocal();
return local.tm_year;
}
int month() { // Since January
updateLocal();
return local.tm_mon;
}
int dayOfMonth() {
updateLocal();
return local.tm_mday;
}
int hour() { // Since midnight, 24-hour clock
updateLocal();
return local.tm_hour;
}
int minute() {
updateLocal();
return local.tm_min;
}
int second() {
updateLocal();
return local.tm_sec;
}
};
#endif // CPPTIME_H ///:~
qingyuan18 2004-07-24
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

int year,month,day;
int years[2]={365,366};
int months[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 isp(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
else return 0;
}

void get_char(char *str)
{
char temp[10]={0};
char *p;
int i=0,j=0;
int a[3]={0};
if((p=strchr(str,'-'))!=NULL)
{
p=str;
while(*p)
{
temp[i++]=*p++;
if(*p=='-')
{
temp[i]=0;
i=0;
a[j++]=atoi(temp);
p++;
}
if(*p==0)
{
temp[i]=0;
i=0;
a[j++]=atoi(temp);
}
}
}
if((p=strchr(str,'/'))!=NULL)
{
p=str;
while(*p)
{
temp[i++]=*p++;
if(*p=='/')
{
temp[i]=0;
i=0;
a[j++]=atoi(temp);
p++;
}
if(*p==0)
{
temp[i]=0;
i=0;
a[j++]=atoi(temp);
}
}
}
year=a[0];
month=a[1];
day=a[2];
}



int get_data(int year,int month,int day)
{
int count=0;
int i;
for(i=1900;i<year;i++)
count=count+years[isp(year)];
for(i=1;i<month;i++)
count=count+months[isp(year)][i];
for(i=1;i<day;i++)
count=count++;
return count;
}



void main()
{
char flag;
int count1=0,count2=0;
int result;
char str[10]={0};
printf("该程序能识别'-'和'/'两种格式的daytime\n");
while(1)
{
printf("输入第一个时间值:\t\n");
gets(str);
fflush(stdin);
get_char(str);
count1=get_data(year,month,day);
memset(str,0,10);
year=0;month=0;day=0;
printf("输入第二个时间值\n");
gets(str);
fflush(stdin);
get_char(str);
count2=get_data(year,month,day);
result=count1-count2;
printf("时间间隔是%d\n",result);
printf("还要继续么[y/n]\n");
scanf("%c",&flag);
fflush(stdin);
if(flag=='n'||flag=='N')
break;
}
}
ma100 2004-07-24
  • 打赏
  • 举报
回复
double difftime( time_t timer1, time_t timer0 );
返回两个日期相差的秒

然后,楼主自己算吧

69,380

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧