使用 struct tm 打印特定日期和 strftime

Use struct tm to print a specific date and strftime

所以我需要专门使用struct tm 打印出我的生日,我成功了。但是,我还需要使用 strftime() 以不同格式打印它。 这就是我遇到问题的地方,因为 strftime() 只识别指针参数。

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

int main(){

    struct tm str_bday;
    time_t time_bday;
    char buffer[15];

    str_bday.tm_year = 1994 - 1900 ;
    str_bday.tm_mon = 7 - 1;
    str_bday.tm_mday = 30;
    str_bday.tm_hour = 12;
    str_bday.tm_min = 53;
    time_bday = mktime(&str_bday);
    if(time_bday == (time_t)-1)
        fprintf(stdout,"error\n");
    else
        {
        fprintf(stdout,"My birthday in second is: %ld \n",time_bday);
        fprintf(stdout,"My birthday is: %s\n", ctime(&time_bday));//Wed July 22 12:53:00 1998
        strftime(buffer,15,"%d/%m/%Y",time_bday);
        fprintf(stdout,"My birthday in D/M/Y format is %s",buffer);
        }
    return 0;
}

错误是:

Error:  passing argument 4 of ‘strftime’ makes pointer from integer without a cast

    expected ‘const struct tm * restrict’ but argument is of type ‘time_t’

有人可以告诉我如何解决吗?

编辑:将 time_bday 更改为 &str_bday 有效!但是现在每次我 运行 程序都会输出随机时间和日期。

编辑:在 strftime() 之后我没有使用 fprintf(),而是使用了 puts(buffer),它运行良好。另外,将 buffer[15] 更改为 buffer[30],因为我有小时、分钟和秒。

通过查看 strftime 的原型,您可以看到您应该传递一个 const struct tm* 作为最后一个参数:

size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr);

在你的情况下是 &str_bday 而不是 time_bday

struct tm 有几个字段您没有初始化,因此采用不确定的值,导致您看到的时间跳跃。在插入值之前,您可以使用 struct tm str_bday = {0} 将所有字段初始化为零。

mktime(&str_bday); 在计算 time_t 时取决于 struct tm 的各个成员 1

首先将成员初始化为 0 是一个好习惯。

// struct tm str_bday;
struct tm str_bday = { 0 };

OP 的代码无法初始化某些成员,例如 tm_sec,这肯定会导致错误的结果。

然而,初始化所有相关成员也很重要。 OP的代码没有分配tm_isdststruct tm str_bday = { 0 },这就像

str_bday.tm_isdst = 0; // Set time stamp to **standard** time.

问题是在 OP's school 中,该日期的夏令时设置肯定是 夏令时

// add
str_bday.tm_isdst = -1; // Let mktime() determine DST setting
// or if one knowns it is daylight time.
str_bday.tm_isdst = 1; // Set time stamp to **daylight** time.
// or if one knowns it is standard time.
str_bday.tm_isdst = 0; // Set time stamp to **standard** time.

错误应该在 ctime(&time_bday)

的打印中很明显
// My birthday is: Sat Jul 30 13:53:00 1994
My birthday is: Sat Jul 30 12:53:00 1994

更正代码

#include <locale.h>
#include <time.h>
int main() {

  struct tm str_bday = { 0 };
  time_t time_bday;
  char buffer[15];

  str_bday.tm_year = 1994 - 1900;
  str_bday.tm_mon = 7 - 1;
  str_bday.tm_mday = 30;
  str_bday.tm_hour = 12;
  str_bday.tm_min = 53;
  str_bday.tm_isdst = -1;
  time_bday = mktime(&str_bday);

  strftime(buffer, sizeof buffer, "%d/%m/%Y", &str_bday);
  if (time_bday == (time_t) -1) {
    fprintf(stdout, "error\n");
  } else {
    // Do not assume `long`.  Better to cast to a wide type. 
    fprintf(stdout, "My birthday in second is: %lld \n", (long long) time_bday);
    fprintf(stdout, "My birthday is: %s", ctime(&time_bday));
    // strftime(buffer,15,"%d/%m/%Y",time_bday);
    strftime(buffer, sizeof buffer, "%d/%m/%Y", &str_bday);
    fprintf(stdout, "My birthday in D/M/Y format is %s\n", buffer);
  }
  return 0;
}

1 成员 tm_wdaytm_yday 不参与计算,但会更新。