C语言里如何得到unix时间戳

qxh0724 2011-07-18 05:40:06
C语言里如何得到unix时间戳

就是1970开始到现在的秒数,例如:1311008334


...全文
2733 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
#include<iostream>
#include<time.h>

using namespace std;

void main()
{
time_t t;
struct tm* tm;//tm结构体存储时间
t=time(NULL);
tm=localtime(&t);
//COUT<<t<<endl;//1970开始到现在的秒数
//cout<<tm->tm_sec<<" "<<tm->hour<<endl;//可以打印tm结构中的成员
cout<<asctime(tm)<<endl;//用asctime格式输出时间函数格式输出时间
}
voipmaker 2011-07-18
  • 打赏
  • 举报
回复


指定格式打印出来。。

void print_time()
{
struct timeval tv;
struct tm* ptm;
char time_str[128];
long milliseconds;
gettimeofday(&tv, NULL);
ptm = localtime(&tv.tv_sec);
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", ptm);
milliseconds = tv.tv_sec / 1000;
printf("%s.%03ld\n", time_str, milliseconds);
}

f_x_p0324 2011-07-18
  • 打赏
  • 举报
回复
查看time()
thecityimissed 2011-07-18
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "time.h"
#include "stdlib.h"

int main()
{
time_t t;
int j;
j = time(&t);
printf("j=%d", j);

return 0;
}

运行结果:j=1310982812
thecityimissed 2011-07-18
  • 打赏
  • 举报
回复
这样?
time_t t;
time(&t);

70,022

社区成员

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

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