Connecting Tech Pros Worldwide Forums | Help | Site Map

LDOM - Last Day of Month

hunt4grouse@yahoo.com
Guest
 
Posts: n/a
#1: Nov 14 '05
I'm looking for C code that will give me the last day of the month.

Input - any day that I pass in as a variable....
input = 20050201

Output - calculate and format with slashes....
output = 2005/02/28


Dave Vandervies
Guest
 
Posts: n/a
#2: Nov 14 '05

re: LDOM - Last Day of Month


In article <1109361303.938968.311590@g14g2000cwa.googlegroups .com>,
<hunt4grouse@yahoo.com> wrote:[color=blue]
>I'm looking for C code that will give me the last day of the month.
>
>Input - any day that I pass in as a variable....
> input = 20050201
>
>Output - calculate and format with slashes....
> output = 2005/02/28[/color]

Here's the easy part:
--------
int ndays[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};

int get_last_day(int month,int year)
{
int ret=ndays[month];
if(is_leap(year) && month==2)
ret++;
return ret;
}
--------
Extracting the month and year from your input, implementing is_leap,
and packing the last day (along with the month and year) into the output
format are left as exercises.


dave

--
Dave Vandervies dj3vande@csclub.uwaterloo.ca
What you have in mind about trees is extremely obscure. They come in green,
red-black, avl, binary, B, deciduous, non-deciduous, tropical, fruit,
ornamental, just to name a few varieties. --CBFalconer in comp.lang.c
Alan Balmer
Guest
 
Posts: n/a
#3: Nov 14 '05

re: LDOM - Last Day of Month


On 25 Feb 2005 11:55:03 -0800, hunt4grouse@yahoo.com wrote:
[color=blue]
>I'm looking for C code that will give me the last day of the month.
>
>Input - any day that I pass in as a variable....
> input = 20050201
>
>Output - calculate and format with slashes....
> output = 2005/02/28[/color]

This is so trivial that it must be homework. We don't do homework.
Have a go at it, then if you have problems, post your code here.

--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net
Eric Sosman
Guest
 
Posts: n/a
#4: Nov 14 '05

re: LDOM - Last Day of Month




hunt4grouse@yahoo.com wrote:[color=blue]
> I'm looking for C code that will give me the last day of the month.
>
> Input - any day that I pass in as a variable....
> input = 20050201
>
> Output - calculate and format with slashes....
> output = 2005/02/28[/color]

There's no built-in function to do this. However,
you could use the fact that the mktime() function accepts
non-canonical inputs and "normalizes" them, making multiple
calls to try out different possible end dates until you
find one that lands in the specified month:

int year = (input / 10000);
int month = (input / 100) % 100;
int day;
for (day = 31; ; --day) {
struct tm bdt = { 0 };
bdt.tm_year = year - 1900;
bdt.tm_mon = month - 1;
bdt.tm_mday = day;
mktime (&bdt);
if (bdt.tm_mon == month - 1)
break;
}
printf ("%d/%02d/%02d\n", year, month, day);

Alternatively, you could use a simple pre-initialized
array and make your own Leap Year determination. However,
a surprising number of programmers have botched the simple
Leap Year test, so it might be safer to stick with mktime().

--
Eric.Sosman@sun.com

Keith Thompson
Guest
 
Posts: n/a
#5: Nov 14 '05

re: LDOM - Last Day of Month


hunt4grouse@yahoo.com writes:[color=blue]
> I'm looking for C code that will give me the last day of the month.
>
> Input - any day that I pass in as a variable....
> input = 20050201
>
> Output - calculate and format with slashes....
> output = 2005/02/28[/color]

Is the input a string or an integer? If it's an integer, keep in mind
that there's no guarantee that type int can represent a value larger
than 32767.

If you have any influence over the output requirements, I encourage
you to use hyphens rather than slashes for separators:

2005-02-28

This is the ISO 8601 date format.

If this is homework, I encourage you to do it yourself (or give us
your instructor's e-mail address so we can submit the solution
directly).

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Stan Milam
Guest
 
Posts: n/a
#6: Nov 14 '05

re: LDOM - Last Day of Month


hunt4grouse@yahoo.com wrote:[color=blue]
> I'm looking for C code that will give me the last day of the month.
>
> Input - any day that I pass in as a variable....
> input = 20050201
>
> Output - calculate and format with slashes....
> output = 2005/02/28
>[/color]
From my date library documentations:

---------------------------------------------------------------
last_day_of_month()
---------------------------------------------------------------
NAME:
last_day_of_month() - Computes a date value for the last day
of the month.

SYNOPSIS:
#include "datetool.h" or "toolbox.h"
date_t last_day_of_month( date_t date_value );

DESCRIPTION:
The last_day_of_month() function uses the date_value argument to
compute a date value which is the last day of the month in which
the date_value argument falls. For example, if the date_value
argument were for Monday, 14 February 1994 the last_day_of_month()
function would calculate a date value for Monday, 28 February 1994.
The date_value argument is checked to be a valid date value between
01/01/0001 A.D. and 12/31/9999 A.D.

ARGUMENTS:
A valid date value between 01/01/0001 A.D. and 12/31/9999 A.D.

RETURN VALUE:
A date value representing the last day of month in which the
argument falls. A value of (date_t) -1L is returned when the
date_value argument is invalid.

SEE ALSO:
first_day_of_month(), date().

Closed Thread