- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
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/
本文实例为大家分享了php实现按天数、星期、月份查询的搜索框,搜索时候展示数据的统计图,主要展示图形的效果,供大家参考,具体内容如下 1.ajax.php ?
星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib,散点图(1) 假设现在有一堆年月日时数据,形如t=2022-07-31 23:45:12,每一条这样的时刻都对应发生了一次事
我想以 EEEE d, MMMM 格式自定义此显示日、日期和月份格式。 提前致谢。 最佳答案 这是完全合乎逻辑的。你可以试试这个 public static class EndDatePickerFr
我是一名优秀的程序员,十分优秀!