linux怎么获取当前时间

binbinxiaogui 2010-03-29 03:58:22
我想做的就是把当前时间取到手,让后显示出来,用正常的人能看懂的那种形式显示出来,怎么弄啊?用C语言实现,大家帮个忙
...全文
546 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
liujundykx 2010-03-29
  • 打赏
  • 举报
回复
void get_time_fun(char* ptime)
{
struct tm* tm_strcut;
time_t timer;
time(&timer);
tm_strcut = localtime(&timer);

/*格式化文件名*/
sprintf(ptime, "%d-%d-%d %d:%d:%d", 1900 + tm_strcut->tm_year, 1 + tm_strcut->tm_mon, tm_strcut->tm_mday,
tm_strcut->tm_hour, tm_strcut->tm_min, tm_strcut->tm_sec);
return ;
}
mymtom 2010-03-29
  • 打赏
  • 举报
回复

#include <sys/time.h>

#include <stdio.h>
#include <time.h>

int
main(int argc, char *argv[])
{
struct timeval tv;
struct tm *tp;
char buf[28];
size_t len;

gettimeofday(&tv, NULL);
tp = localtime(&tv.tv_sec);
len = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tp);
snprintf(buf + len, sizeof(buf) - len, ".%06d", (int)tv.tv_usec);
printf("%s\n", buf);

return 0;
}
yyysss520 2010-03-29
  • 打赏
  • 举报
回复
timeval t;
struct timezone tz; gettimeofday(&t, &tz); //自己用
cout<<"cur time: "<<ctime(&t.tv_sec)<<endl; // 给别人看
wyfwx 2010-03-29
  • 打赏
  • 举报
回复
使用获取struct tm的成员
KimenWu 2010-03-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 steptodream 的回复:]

你们干嘛回复这么快
C/C++ code

#include <stdio.h>
#include <time.h>

int main(void)
{
time_t now;
struct tm *timenow;
time(&now);
timenow=localtime(&now);
pri……
[/Quote]

我是永远也抢不过,吼吼
guoyang1007 2010-03-29
  • 打赏
  • 举报
回复
re
[Quote=引用楼主 binbinxiaogui 的回复:]
我想做的就是把当前时间取到手,让后显示出来,用正常的人能看懂的那种形式显示出来,怎么弄啊?用C语言实现,大家帮个忙
[/Quote]
ecsape 2010-03-29
  • 打赏
  • 举报
回复
难得我抢先一回。哈哈。
[Quote=引用 3 楼 steptodream 的回复:]

你们干嘛回复这么快

[/Quote]
steptodream 2010-03-29
  • 打赏
  • 举报
回复
你们干嘛回复这么快

#include <stdio.h>
#include <time.h>

int main(void)
{
time_t now;
struct tm *timenow;
time(&now);
timenow=localtime(&now);
printf("Curent Time is:%s\n",asctime(timenow));
return 0;
}
~
kingteng 2010-03-29
  • 打赏
  • 举报
回复
调用time函数
time_t t = time(NULL);
printf("%s\n",ctime(&t));
ecsape 2010-03-29
  • 打赏
  • 举报
回复

time_t now;
struct tm *timenow;
time(&now);
timenow = localtime(&now);
ostringstream tstr;
tstr << "[" <<timenow->tm_hour<<":"<<timenow->tm_min<<":"<<timenow->tm_sec << "]" ;

23,222

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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