鉴于获得一周的第一天 [英] get first day of given week

查看:129
本文介绍了鉴于获得一周的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有本周作为一个整数(43截至目前)。
我需要为周一的日期,如周一10月25日的格式。

想我可以做到,通过从功能,但我不知道该怎么做。
有什么建议?

编辑:
我试图从河的建议,但它并没有产生预期的结果。难道我实现它错了吗?

  time_t的星期一;
焦炭DATE_FORMAT [32];
现在time_t的时间=(NULL);
结构TM * TM =本地时间(安培,现在);TM-> tm_yday = 0; //重置为1月1日
TM-GT&; tm_hour = 24 * 7 *每周+ 24; //转到太阳和周一增加24小时星期一= mktime(TM);的strftime(DATE_FORMAT,31日,%A:%D,TM);的printf(%S \\ n,DATE_FORMAT);


解决方案

注:没有测试,但考虑到目前的一年,这应该这样做:

 为const char *个月[12] = {一月,二月,月,2007-4-1,五一,君,七月,八月,月,
                        十月,月,月,月};
/ *启动与当年的1月1日* /
结构TM curYear = {
  0,//秒
  0,//分钟
  0,//小时
  1,//一个月中的天
  0,//月(月)
  年,
  0,// wday
  0,// yday
  0}; // isdst/ *偏移规定的周数* /
time_t的secsSinceEpoch = mktime(安培; curYear)+
                      WEEKNUM * 86400 * 7; / *由周数移位* /
结构TM * candidateDate = gmtime的(安培; secsSinceEpoch);/ *如果候选日是不是一个星期一,移位,以便它是* /
如果(candidateDate->!tm_wday = 1)
{
  secsSinceEpoch + =(86400 *(candidateDate-> tm_wday-1));
  candidateDate = gmtime的(安培; secsSinceEpoch);
}的printf(星期一%S%D,几个月[candidateDate-> tm_mon] candidateDate-> tm_mday \\ n);

您可能取决于究竟你的意思是某一年43星期与ISO-8601符合,例如,调整公式在这个code。不过,这应该present您提供良好的锅炉板code上手。还可能希望参数一周中的一天,所以,这是不硬$ C $光盘。

另外,如果你愿意,你可以避开几个月阵列和具有由截断的ctime 功能,这恰好显示的结果格式化的时候,比你提出的要求。你会传递给它一个指向 secsSinceEpoch 值和截断其输出只显示星期,月份的天和月份的名称的缩写。

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'.

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?

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);

解决方案

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.

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.

这篇关于鉴于获得一周的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆