Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//
//INCLUDE files for :ADDTIME.C - Add a t
// ime period to a base time, normalizing t
// he result
//**************************************
//
/* +++Date last modified: 05-Jul-1997 */
/*
** Header for SNIPPETS date & time function, excluding scalar dates
*/
#ifndef DATETIME__H
#define DATETIME__H
#include "sniptype.h"
/*
** Julian day number functions from JDN_L.C
*/
long ymd_to_jdnl(int y, int m, int d, int julian);
void jdnl_to_ymd(long jdn, int *yy, int *mm, int *dd, int julian);
/*
** Add times from ADDTIME.C
*/
int add_time(unsigned basehrs, unsigned basemins, unsigned basesecs,
int spanhrs, int spanmins, int spansecs,
unsigned *hrs, unsigned *mins, unsigned *secs, int *days);
/*
** Determine the date of Easter for any given year
*/
void easter(int year, int *easter_month, int *easter_day);
/*
** Moon phase from MOON_AGE.C
*/
int moon_age(int month, int day, int year);
/*
** Date parser from PARSDATE.C
*/
typedef enum {USA, ISO, EUROPE} Syntax_T;
Boolean_T parse_date(const char *str, unsigned *year, unsigned *month,
unsigned *day, Syntax_T syntax);
/*
** Time parser from PARSTIME.C
*/
Boolean_T parse_time(const char *str, unsigned *hours, unsigned *mins,
unsigned *secs);
#endif /* DATETIME__H */
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
//
/* +++Date last modified: 05-Jul-1997 */
/*
** ADDTIME.C - Add a time period to a base time, normalizing the result
**
** arguments: 1 - Base hours (0 -> 24)
** 2 - Base minutes (0 -> 59)
** 3 - Base seconds (0 -> 59)
** 4 - Span hours
** 5 - Span minutes
** 6 - Span seconds
** 7 - Address of returned hours (0 -> 23)
** 9 - Address of returned minutes (0 -> 59)
** 10 - Address of returned seconds (0 -> 59)
** 11 - Address of number of days to add to result
**
** returns: 0 if no error, non-zero if base time range error
**
** Notes: 1) Span values may be negative.
** 2) Overflowing midnight will cause a positive number of days to be
**returned.
** 3) Underflowing midnight will cause a negative number of days to be
**returned.
**
** Original Copyright 1994 by Bob Stout as part of
** the MicroFirm Function Library (MFL)
**
** The user is granted a free limited license to use this source file
** to create royalty-free programs, subject to the terms of the
** license restrictions specified in the LICENSE.MFL file.
*/
#include
#include "datetime.h"
int add_time(unsigned basehrs, unsigned basemins, unsigned basesecs,
int spanhrs, int spanmins, int spansecs,
unsigned *hrs, unsigned *mins, unsigned *secs, int *days)
{
int h, m, s;
div_t r;
if (basehrs > 24 || basemins > 59 || basesecs > 59)
return -1;
if (24 == basehrs)
basehrs = 0;
h = (int)basehrs + spanhrs;
m = (int)basemins + spanmins;
s = (int)basesecs + spansecs;
r = div(s, 60);
if (s < 0)
{
r.rem += 60;
--r.quot;
}
*secs = r.rem;
m += r.quot;
r = div(m, 60);
if (m < 0)
{
r.rem += 60;
--r.quot;
}
*mins = r.rem;
h += r.quot;
r = div(h, 24);
if (h < 0)
{
r.rem += 24;
--r.quot;
}
*hrs = r.rem;
*days = r.quot;
return 0;
}
#ifdef TEST
#include
main(int argc, char *argv[])
{
unsigned bh, bm, bs, h, m, s;
int sh, sm, ss, days;
if (7 > argc)
{
puts("Usage: ADDTIME base_hrs base_mins base_secs "
"span_hrs span_mins span_secs");
return EXIT_FAILURE;
}
bh = (unsigned)atoi(argv[1]);
bm = (unsigned)atoi(argv[2]);
bs = (unsigned)atoi(argv[3]);
sh = atoi(argv[4]);
sm = atoi(argv[5]);
ss = atoi(argv[6]);
printf("add_time() returned %d\n",
add_time(bh, bm, bs, sh, sm, ss, &h, &m, &s, &days));
printf("%2d:%02d:%02d + %2d:%02d:%02d = %2d:%02d:%02d + %d days\n",
bh, bm, bs, sh, sm, ss, h, m, s, days);
return EXIT_SUCCESS;
}
#endif /* TEST */