LDOM - Last Day of Month

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hunt4grouse@yahoo.com

    #1

    LDOM - Last Day of Month

    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

    #2
    Re: LDOM - Last Day of Month

    In article <1109361303.938 968.311590@g14g 2000cwa.googleg roups.com>,
    <hunt4grouse@ya hoo.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,3 1,30,31,31,30,3 1,30,31};

    int get_last_day(in t 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

    Comment

    • Alan Balmer

      #3
      Re: LDOM - Last Day of Month

      On 25 Feb 2005 11:55:03 -0800, hunt4grouse@yah oo.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
      removebalmercon sultingthis@att .net

      Comment

      • Eric Sosman

        #4
        Re: LDOM - Last Day of Month



        hunt4grouse@yah oo.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

        Comment

        • Keith Thompson

          #5
          Re: LDOM - Last Day of Month

          hunt4grouse@yah oo.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_Keit h) 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.

          Comment

          • Stan Milam

            #6
            Re: LDOM - Last Day of Month

            hunt4grouse@yah oo.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_mon th()
            ---------------------------------------------------------------
            NAME:
            last_day_of_mon th() - Computes a date value for the last day
            of the month.

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

            DESCRIPTION:
            The last_day_of_mon th() 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_mon th()
            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_mo nth(), date().

            Comment

            Working...