473,411 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Given a date, how to find the beginning date and ending date of that week

Given a date, how to find the beginning date and ending date of that week

please advise!
Nov 13 '05 #1
3 5912
Matt wrote:
Given a date, how to find the beginning date and ending date of that week

please advise!


Initialise a struct tm like this:

struct tm tmt = {0};

Populate the fields you know. You will need, at a minimum, the day, month,
and year. Months are 0-11, and you will need to subtract 1900 from the
year.

Pass to mktime(). That will give you a time_t.

Pass its address to localtime(), giving you a pointer to a struct tm.

Read that struct tm's tm_wday field, where 0 means Sunday and 6 means
Saturday. If it's, say, 3, you know it's Wednesday. Decide what you mean by
"beginning of week". If you think Sunday is the first day of the week, for
example, then you know you're three away from the beginning of the week. So
subtract three from the monthday in the struct tm, and pass it back to
mktime() to normalise it. Then you can pass it to localtime() - again! - to
get a more useful form. Use a similar technique for the end of the week.

Perhaps I'm making this more complicated than it need be, but I don't see a
way to make it shorter.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2
"Richard Heathfield" <do******@address.co.uk.invalid> wrote:
Initialise a struct tm like this:

struct tm tmt = {0};

Populate the fields you know. You will need, at a minimum, the day,
month, and year. Months are 0-11, and you will need to subtract 1900
from the year.
Yes.
Pass to mktime(). That will give you a time_t.
But you don't need the time_t. Throw it away.
Pass its address to localtime(), giving you a pointer to a struct tm.
This is entirely unnecessary. mktime fills in the required field
(tm_wday) through the pointer you gave it.
Read that struct tm's tm_wday field, where 0 means Sunday and 6 means
Saturday. If it's, say, 3, you know it's Wednesday. Decide what you
mean by "beginning of week". If you think Sunday is the first day of
the week, for example, then you know you're three away from the
beginning of the week. So subtract three from the monthday in the
struct tm, and pass it back to mktime() to normalise it.
Correct so far.
Then you can pass it to localtime() - again! - to get a more useful
form. Use a similar technique for the end of the week.
Again unnecessary. You can use the struct tm immediately after calling
mktime.
Perhaps I'm making this more complicated than it need be, but I don't
see a way to make it shorter.


A little shorter, yes:

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

int main(int argc, char **argv)
{
if(argc != 4)
{
puts("Require 3 args: YYYY MM DD");
}
else
{
char buf[81];
struct tm date = {
.tm_year = strtol(argv[1], 0, 0) - 1900,
.tm_mon = strtol(argv[2], 0, 0) - 1,
.tm_mday = strtol(argv[3], 0, 0)
};
mktime(&date);
strftime(buf, 80, "%Y-%m-%d is a %A\n", &date);
puts(buf);

/* You can adjust the next line if you don't consider
* Sunday as the first day of the week.
*/
date.tm_mday -= date.tm_wday;
mktime(&date);
strftime(buf, 80, "Start of week is %A %Y-%m-%d", &date);
puts(buf);

date.tm_mday += 6;
mktime(&date);
strftime(buf, 80, " End of week is %A %Y-%m-%d", &date);
puts(buf);
}
return 0;
}

No time_t used at all!

--
Simon.
Nov 13 '05 #3
Simon Biber wrote:
"Richard Heathfield" <do******@address.co.uk.invalid> wrote:
Initialise a struct tm like this:

struct tm tmt = {0};

Populate the fields you know. You will need, at a minimum, the day,
month, and year. Months are 0-11, and you will need to subtract 1900
from the year.


Yes.
Pass to mktime(). That will give you a time_t.


But you don't need the time_t. Throw it away.


Ah, of course - it hacks the struct as it goes, which is a huge saving.
Silly of me. Thanks.

(Ref: 7.23.2.3)
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Matt | last post by:
My ASP page allows the user select the date, and it will display the report during that week of that date. My question is when the program query database, I need to know the beginning date and...
10
by: Kenneth | last post by:
I have a Query that consist of a lot of different sales data, and one of the colums are different date. The date goes from 1jan2003 til 31jan2003. in this Query I only want the salesdata for...
1
by: Fabrizio | last post by:
HI I would like to know if there is any method to find the beginning date of a week starting from a week number For the week number 13 i would like to know which date (short) is on his first day.
13
by: Jim Armstrong | last post by:
Hi all - This problem has been driving me crazy, and I'm hoping the answer is something stupid I am neglecting to see.... The procedure posted below is part of an Access/SQL database I have...
3
by: Tim Chase | last post by:
I've been trying to come up with a good algorithm for determining the starting and ending dates given the week number (as defined by the strftime("%W") function). My preference would be for a...
16
by: Mik | last post by:
I apologise if this post seems a little basic, but I am a newbie and have NO access knowledge. I have downloaded the Accounts Ledger from the Microsoft Website. It allows the user to review a...
5
slifland
by: slifland | last post by:
I am trying to make a appointment book for a access 2003 database. I would like the report to display the title "Appointments for "month day" - "month day" "year" What I have so far doesn’t do...
0
by: jmarcrum | last post by:
Hi all, I have an excel spreadsheet that in column A, has a formula under the first row in A2 that automatically calculates the Week-ending date when someone starts typing something in cells B2,...
1
by: Mtek | last post by:
Hi, We have a form where the user selects a date from a calendar, the date is in the format May 23, 2008. The date in the datebase is in the format 05212008. What we need to do is get the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.