关于C#:使用struct tm打印特定日期和strftime

Use struct tm to print a specific date and strftime

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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\
"
);
    else
        {
        fprintf(stdout,"My birthday in second is: %ld \
"
,time_bday);
        fprintf(stdout,"My birthday is: %s\
"
, 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;
}

错误为:

1
2
3
Error:  passing argument 4 of a€?strftimea€? makes pointer from integer without a cast

    expected a€?const struct tm * restricta€? but argument is of type a€?time_ta€?

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

编辑:将time_bday更改为


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

1
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的各个member1。

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

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

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

然而,初始化所有相关成员也很重要。 OP的代码未分配tm_isdst,而对于struct tm str_bday = { 0 },则类似于

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

麻烦的是,在OP的学校中,该日期的夏令时设置肯定是夏令时。

1
2
3
4
5
6
// 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)

时显而易见

1
2
// My birthday is: Sat Jul 30 13:53:00 1994
My birthday is: Sat Jul 30 12:53:00 1994

更正的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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\
"
);
  } else {
    // Do not assume `long`.  Better to cast to a wide type.
    fprintf(stdout,"My birthday in second is: %lld \
"
, (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\
"
, buffer);
  }
  return 0;
}

1个成员tm_wdaytm_yday对计算无贡献,但会被更新。