Jump to content

Time/Date Calculations

- - - - -

  • Please log in to reply
No replies to this topic

#1
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
I tried to do the explaining in the comments as far as potential error handling. The actual calculation handling is done via standard library functions time, localtime, and mktime, given that values within a struct tm need not be in "valid ranges" at all times -- they can be normalized later.
[COLOR="Green"]#include <stdio.h>

#include <time.h>[/COLOR]


[COLOR="Blue"]const char [/COLOR]*[COLOR="Red"]dtstamp[/COLOR]([COLOR="Blue"]int [/COLOR]days, [COLOR="Blue"]const char [/COLOR]*format)

{

   [COLOR="Blue"]static const char [/COLOR]*errmsg[] = 

   {

      [COLOR="Teal"]"the calendar time is not available"[/COLOR],

      [COLOR="Teal"]"the specified time cannot be converted to local time"[/COLOR],

      [COLOR="Teal"]"the calendar time cannot be represented"[/COLOR],

      [COLOR="Teal"]"strftime conversion failure"[/COLOR]

   };

   [COLOR="Blue"]static char [/COLOR]result [ [COLOR="Teal"]80[/COLOR] ];

   [COLOR="DarkOrchid"]time_t[/COLOR] t;

   [COLOR="Blue"]struct [/COLOR][COLOR="DarkOrchid"]tm [/COLOR]*local;

   [COLOR="Silver"]/*

    * Try to obtain the implementation's best approximation to the current

    * calendar time.

    */[/COLOR]

   [COLOR="Blue"]if [/COLOR]( [COLOR="Red"]time [/COLOR]( &t ) == ([COLOR="DarkOrchid"]time_t[/COLOR])([COLOR="Teal"]-1[/COLOR]) )

   {

      [COLOR="Blue"]return [/COLOR]errmsg[[COLOR="Teal"]0[/COLOR]];

   }

   [COLOR="Silver"]/*

    * Try to convert the calendar time pointed into a broken-down time.

    */[/COLOR]

   local = [COLOR="Red"]localtime [/COLOR]( &t );

   [COLOR="Blue"]if [/COLOR]( !local )

   {

      [COLOR="Blue"]return [/COLOR]errmsg[[COLOR="Teal"]1[/COLOR]];

   }

   [COLOR="Silver"]/*

    * It might be prudent to check for integer over/underflow here, but I'll

    * live dangerously and just adjust by the offset.

    */[/COLOR]

   [B]local->tm_mday += days;[/B]

   [COLOR="Silver"]/*

    * Try to convert the adjusted broken-down time into a calendar time value.

    */[/COLOR]

   t = [COLOR="Red"]mktime [/COLOR]( local );

   [COLOR="Blue"]if [/COLOR]( t == ([COLOR="DarkOrchid"]time_t[/COLOR])([COLOR="Teal"]-1[/COLOR]) )

   {

      [COLOR="Blue"]return [/COLOR]errmsg[2];

   }

   [COLOR="Silver"]/*

    * Try to convert the adjusted calendar time pointed into a broken-down time.

    */[/COLOR]

   local = [COLOR="Red"]localtime [/COLOR]( &t );

   [COLOR="Blue"]if [/COLOR]( !local )

   {

      [COLOR="Blue"]return [/COLOR]errmsg[[COLOR="Teal"]1[/COLOR]];

   }

   [COLOR="Silver"]/*

    * Try to created a formatted output string.

    */[/COLOR]

   [COLOR="Blue"]if [/COLOR]( ![COLOR="Red"]strftime [/COLOR]( result, [COLOR="Blue"]sizeof [/COLOR]result, format, local ) )

   {

      [COLOR="Blue"]return [/COLOR]errmsg[[COLOR="Teal"]3[/COLOR]];

   }

   [COLOR="Blue"]return [/COLOR]result;

}


[COLOR="Blue"]int [/COLOR][COLOR="Red"]main[/COLOR]([COLOR="Blue"]void[/COLOR])

{

   [COLOR="Blue"]static const char [/COLOR]Iso8601[] = [COLOR="Teal"]"%Y-%m-%dT%X"[/COLOR];

   [COLOR="Blue"]int [/COLOR]offset;

   [B]offset = [COLOR="Teal"]-10[/COLOR];[/B]

   [COLOR="Red"]printf[/COLOR]([COLOR="Teal"]"%d days from today is %s\n"[/COLOR], offset, [COLOR="Red"]dtstamp[/COLOR]([B]offset[/B], Iso8601));

   [B]offset = [COLOR="Teal"]10[/COLOR];[/B]

   [COLOR="Red"]printf[/COLOR]([COLOR="Teal"]"%d days from today is %s\n"[/COLOR], offset, [COLOR="Red"]dtstamp[/COLOR]([B]offset[/B], Iso8601));

   [COLOR="Blue"]return [/COLOR][COLOR="Teal"]0[/COLOR];

}


[COLOR="Silver"]/* my output

-10 days from today is 2005-05-06T22:31:11

10 days from today is 2005-05-26T22:31:11

*/[/COLOR]





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users