Thread: Calculating the first tuesday of any month in a given year?

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    2

    Calculating the first tuesday of any month in a given year?

    Hi There -

    I'm trying to determine the first Tuesday of any month in a given year and was hoping someone might be able to point me in a direction that would be useful. I'd prefer to use my own functions to arrive at a solution, but logically I could use help sorting these things out.

    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Narrow your problem down into something simpler, such as finding the first weekday of the year. Then it is a simple hop to finding the first weekday of any month, and from there the first Tuesday. You might consider taking advantage of the time.h library to get starting values.

  3. #3
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Remember to calculate for leap years. Also, you may consider bit-fields as a good way to save memory space ( you could represent each day of the week as a number up to 7, and declare it like this unsigned int weekday:3; ) If you wanted a way to increase readability, I would recommend you use a enum or something similar to give lables to the values, thus making your code easier to read.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It's a trivial problem if you're allowed to use gmtime() and mktime()
    gmtime(3) - Linux man page
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Lots of info, including several formulae you could implement: Determination of the day of the week - Wikipedia, the free encyclopedia.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    2
    Thanks for the replies.

    I thought that if I started with the base range year and I knew what the first Tuesday in November was during that year, I could calculate what the first Tuesday of any subsequent year.

    For instance, November 4, 1788 was the first Tuesday in November of that year. If I then wanted to find the first Tuesday in November 1792 (a leap year), I would subtract 4 (the number of years in between), have that somehow return a 7, and then subtract one to give me 6. The first Tuesday in 1792 was 11/6/1792.

    Do you think there's a better was of arriving at this? I guess the problem with that is how would I tell a loop that 0 meant 7?

    I'm a beginner at all this, so I kind of need to come up with my own function rather than source out one already existing in math.h or something else.

    Any thoughts?

    Thanks!

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Salem's suggestion of gmtime is great if you need this for work or something. But I'm guessing this is for school/your own learning, in which case I think coding your own would be beneficial. However, I would start with an existing algorithm and simply implement it.

    Take another look at that link I sent, and pay attention to the "useful concepts" section, which talks about corresponding months and years, and gives month/year tables. That should give you some clue about whether you increment your weekday counter by one (common year) or two (leap year), depending on which month you're concerned with.

    You're on the right track, starting with a known date and calculating from there. However, I wouldn't use a single subtraction of the number of years in between. For example, a gap of 7 years may have 0, 1 or 2 leap years in there. Have a variable called first_tuesday, set to the known first tuesday (4 in your example). Write a loop that iterates through the years, from the known start year to the target year (1788 to 1792 in your example), and decrements first_tuesday by 1 or 2 depending on whether you have to account for a leap year. A simple if statement will handle the "0 means 7 case"
    Code:
    if first_tuesday < 1  // went before first of month
        first_tuesday += 7
    So, e.g., Nov 0th was a Tuesday, then so is Nov 7th, the real first Tuesday. Similartly, Nov -1st becomes Nov 6th.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to find month/day from day of year
    By jmack549 in forum C Programming
    Replies: 3
    Last Post: 04-09-2009, 05:01 PM
  2. Help with Day Month Year Program..due by 12
    By jgassen15 in forum C Programming
    Replies: 1
    Last Post: 12-06-2007, 11:21 PM
  3. Year, month and day
    By Newbie999 in forum C Programming
    Replies: 15
    Last Post: 12-01-2006, 10:17 AM
  4. getting the next day's day, month, year etc
    By underthesun in forum C Programming
    Replies: 3
    Last Post: 02-17-2005, 07:43 AM
  5. need current day, month, year
    By ihatejava in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2002, 11:41 AM