当前位置:  技术问答>linux和unix

linux 时间编程问题

    来源: 互联网  发布时间:2017-05-14

    本文导语:  在下最近在写一个有关与时间的程序,如下: /*  * 获得本地时间  * 返回字符串格式"年_月_日"  */ char *get_local_time_y_m_d() { time_t *the_time; struct tm tm_ymd; char *buf; the_time=(time_t *)malloc(sizeof(time_t)); //tm_ymd=(struct tm...

在下最近在写一个有关与时间的程序,如下:

/*
 * 获得本地时间
 * 返回字符串格式"年_月_日"
 */
char *get_local_time_y_m_d()
{
time_t *the_time;
struct tm tm_ymd;
char *buf;
the_time=(time_t *)malloc(sizeof(time_t));
//tm_ymd=(struct tm *)malloc(sizeof(struct tm));
buf=(char *)malloc(sizeof(char)*16);
memset(buf,'',sizeof(char)*16);

//printf("%pn%pn",the_time,tm_ptr);

time(the_time);
//tm_ymd=gmtime(the_time);
localtime_r(the_time,&tm_ymd);
//memcpy(tm_ymd,localtime(the_time),sizeof(struct tm));
strftime(buf,16,"%Y_%m_%d",&tm_ymd);
free(the_time);
/*
 * 看样子struct tm 结构似乎不能free
 */
//free(tm_ptr);
return buf;
}
/*
 * 获得本地时间
 * 返回字符串格式"时_分_秒"
 */
char *get_local_time_h_m_s()
{
time_t *the_time;
struct tm tm_hms;
char *buf;
the_time=(time_t *)malloc(sizeof(time_t));
//tm_hms=(struct tm *)malloc(sizeof(struct tm));
buf=(char *)malloc(sizeof(char)*16);
memset(buf,'',sizeof(char)*16);

//printf("%pn%pn",the_time,tm_ptr);

time(the_time);
//tm_hms=gmtime(the_time);
localtime_r(the_time,&tm_hms);
//memcpy(tm_hms,localtime(the_time),sizeof(struct tm));
strftime(buf,16,"%H:%M:%S",&tm_hms);
free(the_time);
/*
 * 看样子struct tm 结构似乎不能free
 */
//free(tm_ptr);
return buf;
}

这两个函数在程序中会经常调用,但是不论先调用了那个函数,在调用另一个的时候,程序立马错误推出。在网上查了查,是由于localtime和gmtime重复调用问题。在下想请问下各位,有没有什么能随时获得系统时间的方法,C语言

|
我看了,改了一下,不知道 改没有改变你的意思:
#include                                                                                                                      
#include                                                                                                                     
#include                                                                                                                    
/*                                                                                                                                    
 *  * 获得本地时间                                                                                                                    
 *   * 返回字符串格式"年_月_日"                                                                                                       
 *    */                                                                                                                              
void get_local_time_y_m_d(char *buf)                                                                                                  
{                                                                                                                                     
    time_t *the_time;                                                                                                                 
    struct tm tm_ymd;                                                                                                                 
    the_time=(time_t *)malloc(sizeof(time_t));                                                                                        
                                                                                                                                      
    time(the_time);                                                                                                                   
    localtime_r(the_time,&tm_ymd);                                                                                                    
    strftime(buf,16,"%Y_%m_%d",&tm_ymd);                                                                                              
                                                                                                                                      
    printf("a time = %sn",buf);                                                                                                      
    free(the_time);                                                                                                                   
}                                                                                                                                     
/*                                                                                                                                    
 * 获得本地时间                                                                                                                       
 * 返回字符串格式"时_分_秒"                                                                                                           
 */                                                                                                                                   
void get_local_time_h_m_s(char *buf)                                                                                                  
{                                                                                                                                     
    time_t *the_time;                                                                                                                 
    struct tm tm_hms;                                                                                                                 
    the_time=(time_t *)malloc(sizeof(time_t));                                                                                        
                                                                                                                                      
    time(the_time);                                                                                                                   
                                                                                                                                      
    localtime_r(the_time,&tm_hms);                                                                                                    
                                                                                                                                      
    strftime(buf,16,"%H:%M:%S",&tm_hms);                                                                                              
    printf("b time = %sn",buf);                                                                                                      
                                                                                                                                      
    free(the_time);                                                                                                                   
                                                                                                                                      
}                                                                                                                                     
                                                                                                                                      
int main()                                                                                                                            
{                                                                                                                                     
                                                                                                                                      
    char *buf = malloc(16);                                                                                                           
    memset(buf,0x00,16);                                                                                                              
                                                                                                                                      
    while (1)                                                                                                                         
    {                                                                                                                                 
        get_local_time_h_m_s(buf);                                                                                                    
        get_local_time_y_m_d(buf);                                                                                                    
    }                                                                                                                                 
    free(buf);                 
}                                                                                                       

这个怎么跑都没有问题,你程序出错,有可能是其他的地方,不要老盯着这个不放。

    
 
 

您可能感兴趣的文章:

  • Linux c socket编程:简单的客户端(client)和服务端(server)实现
  • 都说《UNIX环境高级编程》好,对于学linux编程好不好?《Linux下的c编程》《嵌入式系统C编程》那个更适合初学者?各是那个出版社的?(
  • Linux下c/c++ boost安装及并发编程库mpi介绍
  • 我想学Linux编程,请大家推荐几本学习Linux下C/C++编程的书
  • 学Linux编程前要学Unix吗?哪里有学Linux编程的好书下载?
  • 我想学习linux桌面编程,那么有没有必要学习linux的内核以及内核的相关编程呢?
  • unix编程与linux编程有什么区别?
  • linux下编程跟平时的编程有什么区别吗?
  • Linux下的编程指的就是Shell编程吗?
  • 帮我介绍基本linux下编程的书,《Linux上的C编程》这本书怎么样?还有别的书吗?
  • Linux 下c++开发error while loading shared libraries问题解决 iis7站长之家
  • 请教linux下的编程的选择:我听说有gtk+gnome的编程,也有kde里的 什么编程,到底应该选择哪个呢?好象一个是技术好一点,一个是市场占有
  • 请问LINUX编程(内核编程)是在什么环境下用什么IDE编?
  • Linux C编程一站式学习,高级shell Bash脚本编程指南,
  • 《UNIX网络编程》这本书适合linux下的网络编程吗?
  • 我学&用Linux编程,手里有《UNIX环境高级编程》,看它行吗?
  • Linux下编程有哪本比较好的书可以推荐下呢。本人看过鸟哥的私房菜了,linux基本操作了解了,现在想开始学linux 下的编程 。我以后打算往网络这方面去学习
  • 都说《UNIX环境高级编程》好,对于学linux编程好不好?是那个出版社的,是中文版吗?
  • windows下编程中有没有linux编程中管道和信号的概念?
  • 哪位编程高手知道如何编程实现获取进程的内存使用量信息(linux)
  • 初学者问题,哪里有适合入门的编程电子书看 linux 下 c编程
  • Linux/CentOS下的CST和UTC时间的区别以及不一致的解决方法
  • linux设置时间和把时间从UTC转成本地时间的
  • Linux下gettimeofday()函数和clock()函数:精确到毫秒级的时间
  • 求助,Linux系统时间总比硬件时间慢!
  • Linux下用ntpdate同步时间及date显示设置时间
  • linux c程序运行cpu时间不包括 i/o时间
  • Linux下如何判断系统当前时间和一个表里面已有的时间的大小?
  • Debian linux 开机一段时间后没声音,重启后,一段时间内有声音,然后就没音了.
  • linux有什么命令能把时间戳转换成时间字符串?
  • 如何通过输入时间对linux时间进行设置
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Linux下时钟同步问题:Clock skew detected原因分析及解决方法
  • linux 繁体 汉字输入法 问题 linux 繁体 汉字输入法 问题
  • Linux 下c++开发error while loading shared libraries问题解决
  • 先安装linux,后安装winxp,能否解决正常启动linux的问题不?(初学者问题,很容易回答)
  • 弱弱的一问,linux下的中文问题及网络问题,分不是问题
  • Linux新手问,Linux安装问题。
  • linux下游戏问题。(菜鸟问题)
  • 初学Linux,发现Linux下的版本问题很头痛。【系列问题】
  • 高深问题:有了linux内核源代码如何做成一个linux操作系统
  • 一个很菜的问题,关ARM与linux内核裁剪的问题!
  • vmware装的linux,通过win2000访问linux的web服务器有问题
  • 初次接触linux,关于linux 用户权限问题请教!
  • Linux下是否存在磁盘碎片问题和内存碎片问题?
  • linux开发的应用程序部署到其他linux的问题
  • vmware上装LINUX的问题,WIN7可以拼的通linux的IP,linux拼不通win7的IP
  • Linux互ping在局域网内大量丢包,但是linux和windows互ping就没问题。
  • 最菜的问题—关于linux“动态库”的问题
  • linux下挂载linux分区的问题
  • 一个小问题。Linux下编程语言的问题。
  • linux问题 secureCRT连接的问题 在线等急!~~
  • Linux下环境变量问题以及FLEX安装问题
  • linux c/c++ IP字符串转换成可比较大小的数字
  • 在win分区上安装linux和独立分区安装linux有什么区别?可以同时安装吗?(两个linux系统)
  • linux哪个版本好?linux操作系统版本详细介绍及选择方案推荐
  • 在虚拟机上安装的linux上,能像真的linux系统一样开发linux程序么?
  • secureCRT下Linux终端汉字乱码解决方法
  • 我重装window后,把linux的引导区覆盖了,进不了linux怎么办?急啊,望热心的人帮助 (现在有linux的盘)
  • Linux c字符串中不可打印字符转换成16进制
  • 安装vmware软件,不用再安装linux系统,就可以模拟linux系统了,然后可以在其上学习一下LINUX下的基本操作 了?
  • Linux常用命令介绍:更改所属用户群组或档案属性
  • 红旗Linux主机可以通过127.0.0.1访问,但如何是连网的Win2000机器通过Linux的IP去访问Linux


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,