Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I determine week number in a month?

Posted on 2007-11-19
3
Medium Priority
?
563 Views
Last Modified: 2013-12-26
Hello,

I have a batch job that I am writing that is scheduled to be run every Tuesday throughout the year.  
I have a huge file that I am ftp'ing to another machine but I am dividing it up into 4 files.
The first Tuesday of the month will write all of the data to these four files and ftp the first file to
the other machine.The second, third, and fourth(last) Tuesday of the month will ftp the other files.

Now I need to determine what Tuesday in the month I am in.  So if it is the first Tuesday of the month, I need it to do some calculation and if it is the 2nd, 3rd, 4th Tuesday then my job will do something else.  I know there are circumstances where there are 5 Tuesdays in a month but I need to ignore that in my code.  Can someone provide me with the code or ideas to do this?

Thanks in advance!
0
Comment
Question by:farekat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20312335
Use struct tm :

You can easily detect whether a day is tuesday or not using the tm_wday field.
The day of the month can be found in tm_mday (note the first tuesday will have a tm_mday value <= 7, the second tuesday a value between 8 and 14, etc.).


So, something like :

#include <time.h>
 
time_t now;
struct tm *nowtm = 0;
 
time(&now);
nowtm = localtime(&now);
 
if (nowtm->tm_wday == 2) {  /* tueasday */
  if (nowtm->tm_mday <= 7) {
    /* first tuesday of the month */
  }
  else if (nowtm->tm_mday <= 14) {
    /* second tuesday of the month */
  }
  else if (nowtm->tm_mday <= 21) {
    /* third tuesday of the month */
  }
  else if (nowtm->tm_mday <= 28) {
    /* fourth tuesday of the month */
  }
}

Open in new window

0
 

Author Comment

by:farekat
ID: 20313225
Would taking the actual date in the month (i.e. today = 19) and dividing it by 7+1 (using integers) give me the week number too?  Is there situations where that won't work?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 20314004
if dom is the day of the month (1 ... 31) :

        ((dom - 1) / 7) + 1

would give you the week number (1, 2, etc.), assuming that / is integer division.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question