获得给定周的第一天

时间:2022-05-10 20:20:30

I have the current week as an integer (43 as of now). I need the date for Monday in a format like 'Mon Oct 25'.

我将当前周作为整数(截至目前为43)。我需要星期一的日期,格式为'Mon Oct 25'。

Thought I could accomplish that by a function from but I don't know how to do that. Any suggestions?

以为我可以通过一个函数完成它,但我不知道该怎么做。有什么建议么?

EDIT: I tried the suggestion from R., but it doesn't give the expected result. Did I implement it wrong?

编辑:我尝试了R.的建议,但它没有给出预期的结果。我做错了吗?

time_t monday;
char date_format[32];
time_t now = time(NULL);
struct tm *tm = localtime(&now);

tm->tm_yday = 0; // reset to Jan 1st
tm->tm_hour = 24 * 7 * WEEK + 24; // goto Sun and add 24h for Mon

monday = mktime(tm);

strftime(date_format, 31, "%a : %D", tm);

printf("%s\n", date_format);

2 个解决方案

#1


2  

Note: Not tested, but given the current year, this should do it:

注意:未经测试,但考虑到当前年度,应该这样做:

const char *months[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
                        "Oct","Nov","dec","Jan"};
/* Start with January 1st of the current year */
struct tm curYear={
  0, // secs
  0, // mins
  0, // hours
  1, // Day of month
  0, // Month (Jan)
  year,
  0, // wday
  0, // yday
  0}; // isdst

/* Offset the number of weeks specified */
time_t secsSinceEpoch=mktime(&curYear)+
                      weekNum*86400*7; /* Shift by number of weeks */
struct tm *candidateDate=gmtime(&secsSinceEpoch);

/* If the candidate date is not a Monday, shift it so that it is */
if (candidateDate->tm_wday!=1)
{
  secsSinceEpoch+=(86400*(candidateDate->tm_wday-1)); 
  candidateDate=gmtime(&secsSinceEpoch);
}

printf("Mon %s %d",months[candidateDate->tm_mon],candidateDate->tm_mday\n");

You may have to adjust the formulas in this code depending on what exactly you mean by week 43 of a given year or to conform with ISO-8601, for example. However, this should present you with good boiler plate code to get started. You may also want to parameterize the day of the week, so that it is not hard coded.

您可能需要调整此代码中的公式,具体取决于您在给定年份的第43周时的确切含义或符合ISO-8601。但是,这应该为您提供良好的锅炉板代码以便入门。您可能还想参数化星期几,以便它不是硬编码的。

Also, if you want, you can avoid the months array and having to format the time, by truncating the result of the ctime function, which just so happens to display more than you asked for. You would pass to it a pointer to the secsSinceEpoch value and truncate its output to just display the day of the week, the day of the month and the abbreviation of the months name.

此外,如果需要,您可以通过截断ctime函数的结果来避免使用月数组并且必须格式化时间,这恰好比您要求的更多地显示。您将向其传递一个指向secsSinceEpoch值的指针,并截断其输出以仅显示星期几,月份日期和月份名称的缩写。

#2


2  

The mktime function can do this. Simply initialize struct tm foo to represent the first day of the year (or first day of the first week of the year, as needed), then set tm_hour to 24*7*weeknum and call mktime. It will normalize the date for you.

mktime函数可以做到这一点。只需初始化struct tm foo以表示一年中的第一天(或一年中第一周的第一天,根据需要),然后将tm_hour设置为24 * 7 * weeknum并调用mktime。它会为您规范日期。

#1


2  

Note: Not tested, but given the current year, this should do it:

注意:未经测试,但考虑到当前年度,应该这样做:

const char *months[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
                        "Oct","Nov","dec","Jan"};
/* Start with January 1st of the current year */
struct tm curYear={
  0, // secs
  0, // mins
  0, // hours
  1, // Day of month
  0, // Month (Jan)
  year,
  0, // wday
  0, // yday
  0}; // isdst

/* Offset the number of weeks specified */
time_t secsSinceEpoch=mktime(&curYear)+
                      weekNum*86400*7; /* Shift by number of weeks */
struct tm *candidateDate=gmtime(&secsSinceEpoch);

/* If the candidate date is not a Monday, shift it so that it is */
if (candidateDate->tm_wday!=1)
{
  secsSinceEpoch+=(86400*(candidateDate->tm_wday-1)); 
  candidateDate=gmtime(&secsSinceEpoch);
}

printf("Mon %s %d",months[candidateDate->tm_mon],candidateDate->tm_mday\n");

You may have to adjust the formulas in this code depending on what exactly you mean by week 43 of a given year or to conform with ISO-8601, for example. However, this should present you with good boiler plate code to get started. You may also want to parameterize the day of the week, so that it is not hard coded.

您可能需要调整此代码中的公式,具体取决于您在给定年份的第43周时的确切含义或符合ISO-8601。但是,这应该为您提供良好的锅炉板代码以便入门。您可能还想参数化星期几,以便它不是硬编码的。

Also, if you want, you can avoid the months array and having to format the time, by truncating the result of the ctime function, which just so happens to display more than you asked for. You would pass to it a pointer to the secsSinceEpoch value and truncate its output to just display the day of the week, the day of the month and the abbreviation of the months name.

此外,如果需要,您可以通过截断ctime函数的结果来避免使用月数组并且必须格式化时间,这恰好比您要求的更多地显示。您将向其传递一个指向secsSinceEpoch值的指针,并截断其输出以仅显示星期几,月份日期和月份名称的缩写。

#2


2  

The mktime function can do this. Simply initialize struct tm foo to represent the first day of the year (or first day of the first week of the year, as needed), then set tm_hour to 24*7*weeknum and call mktime. It will normalize the date for you.

mktime函数可以做到这一点。只需初始化struct tm foo以表示一年中的第一天(或一年中第一周的第一天,根据需要),然后将tm_hour设置为24 * 7 * weeknum并调用mktime。它会为您规范日期。