getdate 和 ctime 无法正常工作

发布于 2024-11-25 19:13:24 字数 502 浏览 3 评论 0原文

这是我的程序 time_play.c :

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

int
main (void)
{
    char *time_str = "2011-07-20 17:30:18";
    time_t time_tm = getdate(time_str);

    printf("str: %s and time: %s \n", time_str, ctime(&time_tm));

    return 0;
} 

它的输出:

$ gcc -o time_play time_play.c 
$ ./time_play
str: 2011-07-20 17:30:18 and time: Wed Dec 31 16:00:00 1969

可以看到 time 的值为 NULL。为什么会这样呢?

Here is my program time_play.c :

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

int
main (void)
{
    char *time_str = "2011-07-20 17:30:18";
    time_t time_tm = getdate(time_str);

    printf("str: %s and time: %s \n", time_str, ctime(&time_tm));

    return 0;
} 

And it's output:

$ gcc -o time_play time_play.c 
$ ./time_play
str: 2011-07-20 17:30:18 and time: Wed Dec 31 16:00:00 1969

It can be seen that time is getting value NULL. Why is it so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

要走就滚别墨迹 2024-12-02 19:13:24

您是否创建了指定您正在使用的日期格式的模板文件?

从手册页:

用户提供的模板用于解析和解释输入字符串。模板是由用户创建并通过环境变量 DATEMSK 标识的文本文件。

创建一个类似 timetemplate.txt 的文件:

%Y-%m-%d %H:%M:%S

然后将 DATEMSK 的值设置为指向它:

export DATEMSK=/home/somebody/timetemplate.txt

这段代码在 OS X 上适用于我,结合设置 DATEMSK:

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

int main (void)
{
  char *time_str = "2011-07-20 17:30:18";
  struct tm *time_tm = getdate(time_str);
  time_t t = mktime(time_tm);

  printf("str: %s and time: %s", time_str, ctime(&t));
  return 0;
}

Did you create a template file specifying the date format you are using?

From the man page:

User-supplied templates are used to parse and interpret the input string. The templates are text files created by the user and identified via the environment variable DATEMSK.

Create a file like timetemplate.txt:

%Y-%m-%d %H:%M:%S

Then set the value of DATEMSK to point to it:

export DATEMSK=/home/somebody/timetemplate.txt

This code worked for me on OS X, in combination with setting the DATEMSK:

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

int main (void)
{
  char *time_str = "2011-07-20 17:30:18";
  struct tm *time_tm = getdate(time_str);
  time_t t = mktime(time_tm);

  printf("str: %s and time: %s", time_str, ctime(&t));
  return 0;
}
椒妓 2024-12-02 19:13:24

来自精细手册

struct tm *getdate(const char *string);

getdate 函数返回 struct tm *,而不是 time_t >。你很幸运,你的程序没有崩溃并着火。

请为您的编译器打开更多警告标志,您应该看到如下内容:

警告:初始化从指针生成整数而不进行强制转换

对第 9 行进行强制转换。

From the fine manual:

struct tm *getdate(const char *string);

The getdate function returns a struct tm *, not a time_t. You're lucky that your program isn't falling over and catching on fire.

Please turn on more warning flags for your compiler, you should have seen something like this:

warning: initialization makes integer from pointer without a cast

about line 9.

晨敛清荷 2024-12-02 19:13:24

在 openSUSE 13.2 中,

必须在第一行添加以下代码:

#define _XOPEN_SOURCE 500

glibc 的功能测试宏要求(请参阅 feature_test_macros(7)):

   getdate():
       _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
   getdate_r():
       _GNU_SOURCE

man 3 getdate 它会告诉您:

glibc 的功能测试宏要求(请参阅 feature_test_macros( 7)):

   getdate():
       _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
   getdate_r():
       _GNU_SOURCE

In openSUSE 13.2,

MUST ADD the following code at the first line :

#define _XOPEN_SOURCE 500

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   getdate():
       _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
   getdate_r():
       _GNU_SOURCE

man 3 getdate it will tell you:

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   getdate():
       _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
   getdate_r():
       _GNU_SOURCE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文