当前位置:首页 » 编程语言 » c语言时间换算

c语言时间换算

发布时间: 2023-06-07 13:55:45

❶ C语言时间,怎么把time_t类型的时间,转化成年、月、日、时、分、秒呢

可以使用gmtime函数或localtime函数将time_t类型的时间日期转换为struct tm类型(年、月、日、时、分、秒)。
使用time函数返回的是一个long值,该值对用户的意义不大,一般不能根据其值确定具体的年、月、日等数据。gmtime函数可以方便的对time_t类型数据进行转换,将其转换为tm结构的数据方便数据阅读。gmtime函数的原型如下:struct tm *gmtime(time_t *timep);localtime函数的原型如下:struct tm *localtime(time_t *timep);将参数timep所指的time_t类型信息转换成实际所使用的时间日期表示方法,将结果返回到结构tm结构类型的变量。gmtime函数用来存放实际日期时间的结构变量是静态分配的,每次调用gmtime函数都将重写该结构变量。如果希望保存结构变量中的内容,必须将其复制到tm结构的另一个变量中。gmtime函数与localtime函数的区别:gmtime函数返回的时间日期未经时区转换,是UTC时间(又称为世界时间,即格林尼治时间)。localtime函数返回当前时区的时间。
转换日期时间表示形式time_t类型转换为struct tm类型示例:
#include <stdio.h>
#include <time.h>
int main()
{
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指针字符数组*/ time_t t;
struct tm *p;
t=time(NULL);/*获取从1970年1月1日零时到现在的秒数,保存到变量t中*/ p=gmtime(&t); /*变量t的值转换为实际日期时间的表示格式*/
printf("%d年%02d月%02d日",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(" %s ", wday[p->tm_wday]);
printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);
return 0;
}
注意:p=gmtime(&t);此行若改为p=localtime(&t);则返回当前时区的时间。

❷ 关于C语言的时间函数

思路:一般做法都是用指定的时间去和一个固定时间来比较,得出此时间与固定时间所差的天数,一般固定时间都使用“1900-01-01”,比如d1与固定时间的差为Num1天,d2与固定时间的差为Nmu2天,那么d1、d2的天数差就是Num1与Num2间的差值了。最好不要直接用d1和d2来比较,因为不好确定他们之间有多少个闰年、大小月等。
以下代码是我从其它程序中摘出来的,大体上能满足你的需求(使用的中文编程,细节自己修改)
#include <stdio.h>
#include <string.h>

typedef unsigned char UNBYTE;
typedef unsigned short UNWORD;
typedef unsigned long UNLONG;
typedef unsigned long DATETIME;

/** 从 0001-01-01 到 1899-12-31 间的天数,故0日期是从1900-01-01开始的 */
const UNLONG n日期开始 = 693594;

/** 时间的转换系数*/
const UNBYTE n每天时数 = 24;
const UNBYTE n每时分数 = 60;
const UNBYTE n每分秒数 = 60;
const UNWORD n每时秒数 = n每分秒数 * n每时分数;
const UNLONG n每天秒数 = n每天时数 * n每时秒数;

typedef enum
{
TRUE = 0x5A,
FALSE = 0
}UBBOOL;

typedef struct
{
UNBYTE ub_年;
UNBYTE ub_月;
UNBYTE ub_日;
UNBYTE ub_时;
UNBYTE ub_分;
UNBYTE ub_秒;
UNBYTE ub_百分秒;
UNBYTE ub_备用;
}S日历时钟; /**8 byte*/

UNBYTE ub_每月天数[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}
};

UNBYTE 润年判断(const UNBYTE lub_年)
{

if( (lub_年 % 4 == 0) && ((lub_年 % 100 != 0) || (lub_年 % 400 == 0)) )
return 1;
else
return 0;
};

UBBOOL 转换日期(UNBYTE lub_年, UNBYTE lub_月,UNBYTE lub_日, DATETIME &lub_日期)
{
UNBYTE lub_日表序号 = 0;
UNWORD luw_年;
int i;

/**根据是否为润年取日表的序号*/
luw_年 = 2000 + lub_年;
lub_日表序号 = 润年判断(luw_年);

lub_日期 = lub_日;
if ( (luw_年 >= 2000)
&& (luw_年 <= 2099)
&& (lub_月 >= 1)
&& (lub_月 <= 12)
&& (lub_日 >= 1)
&& (lub_日 <= ub_每月天数[lub_日表序号][lub_月-1]) )
{
/**计算月*/
for( i=0; i<lub_月-2; i++ )
{
lub_日期 += ub_每月天数[lub_日表序号][i];
}

luw_年 -= 1;
lub_日期 += (luw_年 * 365) + (luw_年 / 4) - (luw_年 / 100) + (luw_年 / 400) - n日期开始;
return TRUE;
}
else
{
return FALSE;
}
};

UBBOOL 转换时间(UNBYTE lub_时, UNBYTE lub_分, UNBYTE lub_秒, DATETIME &lub_时间)
{
if ( (lub_时 < n每天时数) && (lub_分 < n每时分数) && (lub_秒 < n每分秒数) )
{
lub_时间 = (lub_时 * n每时秒数 + lub_分 * n每分秒数 + lub_秒);
return TRUE;
}
else
{
return FALSE;
}
};

UBBOOL 转换时钟(S日历时钟 &ls_时钟, DATETIME &ldt_时间)
{
DATETIME dt_日 = 0;
DATETIME dt_秒 = 0;

ldt_时间 = 0;
if ((TRUE == 转换日期(ls_时钟.ub_年, ls_时钟.ub_月, ls_时钟.ub_日, dt_日))
&&(TRUE == 转换时间(ls_时钟.ub_时, ls_时钟.ub_分, ls_时钟.ub_秒, dt_秒)))
{
ldt_时间 = dt_日 * n每天秒数 + dt_秒;
return TRUE;
}
else
{
return FALSE;
}
};

void main()
{
DATETIME dt,dt2,cha;
S日历时钟 s_now;
S日历时钟 s_now2;

s_now.ub_年 = 9;
s_now.ub_月 = 12;
s_now.ub_日 = 15;
s_now.ub_时 = 23;
s_now.ub_分 = 59;
s_now.ub_秒 = 59;

s_now2.ub_年 = 9;
s_now2.ub_月 = 12;
s_now2.ub_日 = 16;
s_now2.ub_时 = 0;
s_now2.ub_分 = 0;
s_now2.ub_秒 = 1;

if(( TRUE == 转换时钟(s_now, dt))&&( TRUE == 转换时钟(s_now2, dt2)))
{
cha = dt2-dt;
}
else
puts("error");

//////////////////////////////////////////

}

热点内容
关闭androidstudio 发布:2025-02-14 03:53:42 浏览:18
安卓脚本一键打板 发布:2025-02-14 03:50:16 浏览:866
我的世界租赁服务器会被封ip吗 发布:2025-02-14 03:46:05 浏览:982
数据库附 发布:2025-02-14 03:44:43 浏览:837
安卓系统解不了锁怎么办 发布:2025-02-14 03:44:35 浏览:552
怎么查询信用卡密码 发布:2025-02-14 03:42:04 浏览:318
studio外贸服装直播脚本 发布:2025-02-14 03:34:16 浏览:453
python集合运算符 发布:2025-02-14 03:06:18 浏览:205
pic编译软件 发布:2025-02-14 03:01:04 浏览:984
反编译在编译 发布:2025-02-14 02:55:36 浏览:418