GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
datetime/misc.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1995. Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro
3  *
4  * This program is free software under the GPL (>=v2)
5  * Read the file GPL.TXT coming with GRASS for details.
6  */
7 #include <grass/datetime.h>
8 
9 /*!
10  * \brief
11  *
12  * \param year
13  * \param ad
14  * \return int
15  */
16 
17 int datetime_is_leap_year(int year, int ad)
18 {
19  if (year == 0)
20  return datetime_error(-1, "datetime_is_leap_year(): illegal year");
21  if (!ad)
22  return 0; /* BC */
23  if (year < 0)
24  return 0; /* ?? */
25 
26  return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
27 }
28 
29 /*!
30  * \brief
31  *
32  * returns the number of days in 'year'
33  *
34  * \param year
35  * \param ad
36  * \return int
37  */
38 
39 int datetime_days_in_year(int year, int ad)
40 {
41  if (year == 0)
42  return datetime_error(-1, "datetime_days_in_year(): illegal year");
43 
44  if (datetime_is_leap_year(year, ad))
45  return 366;
46  else
47  return 365;
48 }
49 
50 /*!
51  * \brief
52  *
53  * returns number of days in 'month' of a particular 'year'
54  *
55  * \param month
56  * \param year
57  * \param ad
58  * \return int
59  */
60 
61 int datetime_days_in_month(int year, int month, int ad)
62 {
63  static int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
64 
65  if (month < 1 || month > 12)
66  return datetime_error(-1, "datetime_days_in_month(): illegal month");
67 
68  if (month == 2 && datetime_is_leap_year(year, ad))
69  return (29);
70 
71  return (days[month - 1]);
72 }
int datetime_days_in_month(int year, int month, int ad)
returns number of days in 'month' of a particular 'year'
Definition: datetime/misc.c:61
int datetime_is_leap_year(int year, int ad)
Definition: datetime/misc.c:17
int datetime_days_in_year(int year, int ad)
returns the number of days in 'year'
Definition: datetime/misc.c:39
int datetime_error(int code, char *msg)
record 'code' and 'msg' as error code/msg (in static variables) code==0 will clear the error (ie set ...