GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-97d1178de3
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 int datetime_is_leap_year(int year, int ad)
17 {
18  if (year == 0)
19  return datetime_error(-1, "datetime_is_leap_year(): illegal year");
20  if (!ad)
21  return 0; /* BC */
22  if (year < 0)
23  return 0; /* ?? */
24 
25  return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
26 }
27 
28 /*!
29  * \brief
30  *
31  * returns the number of days in 'year'
32  *
33  * \param year
34  * \param ad
35  * \return int
36  */
37 int datetime_days_in_year(int year, int ad)
38 {
39  if (year == 0)
40  return datetime_error(-1, "datetime_days_in_year(): illegal year");
41 
42  if (datetime_is_leap_year(year, ad))
43  return 366;
44  else
45  return 365;
46 }
47 
48 /*!
49  * \brief
50  *
51  * returns number of days in 'month' of a particular 'year'
52  *
53  * \param month
54  * \param year
55  * \param ad
56  * \return int
57  */
58 int datetime_days_in_month(int year, int month, int ad)
59 {
60  static int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
61 
62  if (month < 1 || month > 12)
63  return datetime_error(-1, "datetime_days_in_month(): illegal month");
64 
65  if (month == 2 && datetime_is_leap_year(year, ad))
66  return (29);
67 
68  return (days[month - 1]);
69 }
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:58
int datetime_is_leap_year(int year, int ad)
Definition: datetime/misc.c:16
int datetime_days_in_year(int year, int ad)
returns the number of days in 'year'
Definition: datetime/misc.c:37
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 ...