2013-02-22 26 views
1

我们通常使用mktm()或mktime()将长时间转换为日期和时间字符串。无论如何,我们可以做相反的事情吗?我想将日期和时间字符串转换成很长时间。但我从互联网上找不到任何东西。对于您的信息,长时间表示从01/01/1980 00:00:00开始的秒数。用C语言从字符串转换为长时间

例如,如果长时间使用mktm()或mktime()将1045872000转换为字符串时间,我们可以获得21/02/2013 00:00:00。有没有反向功能?转换21/02/2013 00:00:00至1045872000.

+0

http://stackoverflow.com/questions/7524543/convert-a-string-in-to-time-format-in-c是你正在寻找...基本上GETDATE这是什么()函数参考:http://pubs.opengroup.org/onlinepubs/007908799/xsh/getdate.html。 – Anshul 2013-02-22 08:29:58

+0

不是getdate()是否与mktm()或mktime()函数相同? – Coolguy 2013-02-22 08:39:15

回答

1

你有strptime但我不认为这是一个标准。

转换时间的字符串表示的时间tm结构

+0

http://stackoverflow.com/questions/667250/strptime-in-windows – 2013-02-22 08:11:12

0

STRFTIME功能是Normaltime转化为划时代的时间

#include<stdio.h> 
    #include<stdlib.h> 
    #include<time.h> 
    int 
    main(void) 
    { 
     struct tm tm; 
     char buf[255]; 

     strptime("2001-11-13 18:31:01", "%Y-%m-%d %H:%M:%S", &tm); 
     strftime(buf, sizeof(buf), "%s", &tm); 
     puts(buf); 
     exit(EXIT_SUCCESS); 
    } 

这次2001-11-13 18:31: 01时代是1005570061

(或)

系统m函数使用转换历元时间。

 system("date -d 'Mon Feb 25 18:05:57 IST 2013' +'%s'"); 
+0

有转换日期格式到纪元时间的功能吗? – Coolguy 2013-02-22 10:39:37

+0

此功能将时间转换为纪元时间 – loganaayahee 2013-02-22 10:55:31

+0

为什么你首先使用strptime?我的编译器没有这个功能。它只有strftime。 – Coolguy 2013-02-22 11:11:57