summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/calsupp.c
blob: 113f08aa873ee8c382c815b1d496a922daf67a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* calsupp.c -- public domain by Ray McVay */

/*  This module provides three handy date related functions:
**       dow() - Returns the day of the week for a given date
**       IsLeap() - Returns 1 if a year is a leap year
**       GetToday() - Returns today's date from the operating system
*/

#include <time.h>

/*
** Returns an integer that represents the day of the week for
**  the date passed as parameters.
**
**   day:    day of month
**   mon:    month (1-12)
**   yr:     year
**
**  returns 0-6 where 0 == sunday
*/

int dow(int day, int mon, int yr)
{
      int dow;

      if (mon <= 2)
      {
            mon += 12;
            yr -= 1;
      }
      dow = (day + mon * 2 + ((mon + 1) * 6) / 10 +
            yr + yr / 4 - yr / 100 + yr / 400 + 2);
      dow = dow % 7;
      return ((dow ? dow : 7) - 1);
}


/*
**  Returns 1 if yr is a leap year, 0 if it is not
*/

int IsLeap(int yr)
{
      if (yr % 400 == 0)  return 1;
      if (yr % 100 == 0)  return 0;
      if (yr % 4 == 0)    return 1;
      else                return 0;
}


/*
**  Returns the current day, month and year in the referenced variables
*/

void GetToday(int *day, int *mon, int *yr)
{
      struct tm   today;
      time_t  ctime;

      time(&ctime);
      today = *localtime(&ctime);
      *day = today.tm_mday;
      *mon = today.tm_mon + 1;
      *yr = today.tm_year + 1900;
}