免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8871 | 回复: 8
打印 上一主题 下一主题

在unix下面如何获取系统时间啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-18 11:26 |只看该作者 |倒序浏览
请问一下如何在unix 下面获取系统时间啊?

论坛徽章:
0
2 [报告]
发表于 2006-08-18 11:34 |只看该作者
time?

论坛徽章:
0
3 [报告]
发表于 2006-08-20 15:27 |只看该作者
c程序中取
man localtime

论坛徽章:
0
4 [报告]
发表于 2006-08-20 17:19 |只看该作者
有很多种,先time,然后再将其变换为相应的格式
  1. #include<stdio.h>
  2. #include<time.h>

  3. int main(void)
  4. {
  5.     time_t t;
  6.     struct tm *tm;
  7.     char buf[64];
  8.     t=time(NULL);
  9.     tm=localtime(&t);

  10.     strftime(buf,sizeof(buf),"%c",tm);
  11.     printf("%s",buf);
  12.     return 0;
  13. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2006-08-21 18:20 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2006-08-21 23:22 |只看该作者
原帖由 xiaomiao 于 2006-8-21 18:20 发表
Unix下的time是19700101开始的一个long变量,32 bit的到2038年好像就overflow了,64 bit的Unix没有这个问题


用time_t,不用担心这个。

论坛徽章:
0
7 [报告]
发表于 2006-08-21 23:42 |只看该作者
date

论坛徽章:
0
8 [报告]
发表于 2006-08-22 15:45 |只看该作者
#include<stdio.h>
#include<time.h>

int main(void)
{
    time_t t;
    struct tm *tm;
    char buf[64];
    t=time(NULL);
    tm=localtime(&t);

    strftime(buf,sizeof(buf),"%c",tm);
    printf("%s",buf);
    return 0;
}



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

  3. int main(int argc, char **argv)
  4. {
  5.         time_t t;
  6.         t = time(NULL);
  7.         printf("The time is: %s", ctime(&t));
  8.         return 0;
  9. }
复制代码


好像这样更方便一点

论坛徽章:
0
9 [报告]
发表于 2006-08-22 15:47 |只看该作者

回复 8楼 rushrush 的帖子

哈哈,其实我本也想发这个的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP