Given a date, how to find the beginning date and ending date of that week
please advise! 3 5909
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
"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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| | |