gpt4 book ai didi

c - strftime 问题星期几

转载 作者:太空宇宙 更新时间:2023-11-04 01:36:58 28 4
gpt4 key购买 nike

        char buffer[800];
struct tm str_time;

str_time.tm_mday = Cur_Day;
str_time.tm_mon = Cur_Month - 1;
str_time.tm_year = entries[i].Year_Start - 1900;
int len = strftime(buffer, 100, "%A, %d %B %Y", &str_time);
printf("\n%s\n", buffer);

无论 Cur_Day 和 Cur_Month 的值如何,如果星期几始终是星期日,上述结果如何?

示例输出:

Sunday, 23 November 2012
------------------------
stuff

Sunday, 25 November 2012
------------------------
stuff

Sunday, 26 November 2012
------------------------
stuff

最佳答案

您的 str_time 结构(如果它看起来是一个局部变量)在其字段中具有不确定的值,除非您显式设置它们。 strftime 所做的只是使用它拥有的值,它不会首先调整值以符合其他字段。

由于您没有设置 tm_wday,它会保持原来的状态(看起来是 0,因为它总是星期日)。

如果您确实想要根据其他字段调整字段,您应该查看mktime()

来自标准 (ISO C99):

The mktime function converts the broken-down time, expressed as local time, in the structure pointed to by timeptr into a calendar time value with the same encoding as that of the values returned by the time function.

The original values of the tm_wday and tm_yday components of the structure are ignored, and the original values of the other components are not restricted to the ranges indicated above.

On successful completion, the values of the tm_wday and tm_yday components of the structure are set appropriately, and the other components are set to represent the specified calendar time, but with their values forced to the ranges indicated above; the final value of tm_mday is not set until tm_mon and tm_year are determined.

最好的办法是使用 time()localtime() 来填充 tm 结构,然后更改您想要的字段在调用 mktime() 之前更改。这样,您就可以保证所有 字段都具有合理的值。

下面的程序展示了一种方法:

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

int main (void) {
char buffer[100];
time_t now;
struct tm *ts;

// Get today in local time and output it.

now = time (NULL);
struct tm *ts = localtime (&now);
strftime (buffer, 100, "%A, %d %B %Y", ts);
printf ("Now = %s\n", buffer);

// Advance day-of-month and make new date.
// Probably need to intelligently handle month rollover.

ts->tm_mday++;
mktime (ts);
strftime (buffer, 100, "%A, %d %B %Y", ts);
printf ("Tomorrow = %s\n", buffer);

return 0;
}

该程序的输出是:

Now      = Tuesday, 09 October 2012
Tomorrow = Wednesday, 10 October 2012

值得一提的是,这里有一个完整的程序,它使用该方法为您提供给定日期(默认为今天)的星期几。

您可以使用可选的 -y-m-d 参数以任意顺序更改年月日您想要的次数不限,但每种类型的最后一次才算数。

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

static int makeError (char *argVal, char *errStr) {
printf ("Error with argument '%s': %s\n", argVal, errStr);
printf ("Usage: dow [-y<year>] [-m<month>] [-d<day>]\n");
return 1;
}

int main (int argc, char *argv[]) {
int idx, intVal;
char chVal;
char buff[100];
time_t now = time (NULL);
struct tm *nowStr = localtime (&now);

for (idx = 1; idx < argc; idx++) {
chVal = (*argv[idx] != '-') ? '\0' : *(argv[idx] + 1);
if ((chVal != 'y') && (chVal != 'm') && (chVal != 'd'))
return makeError (argv[idx], "does not start with '-y/m/d'");

intVal = atoi (argv[idx] + 2);
if (intVal < 0)
return makeError (argv[idx], "suffix is negative");
sprintf (buff, "%d", intVal);
if (strcmp (buff, argv[idx] + 2) != 0)
return makeError (argv[idx], "suffix is not numeric");

switch (chVal) {
case 'y': nowStr->tm_year = intVal - 1900; break;
case 'm': nowStr->tm_mon = intVal - 1; break;
case 'd': nowStr->tm_mday = intVal; break;
}
}

mktime (nowStr);
strftime (buff, sizeof (buff), "%A, %d %B %Y", nowStr);
printf ("%s\n", buff);

return 0;
}

示例成绩单:

pax> ./dow
Tuesday, 09 October 2012

pax> ./dow -y2011
Sunday, 09 October 2011

pax> ./dow -y2000 -m1 -d1
Saturday, 01 January 2000

关于c - strftime 问题星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12792044/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com