GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 /*!
11  * \brief
12  *
13  * \param year
14  * \param ad
15  * \return int
16  */
17 
18 int datetime_is_leap_year(int year, int ad)
19 {
20  if (year == 0)
21  return datetime_error(-1, "datetime_is_leap_year(): illegal year");
22  if (!ad)
23  return 0; /* BC */
24  if (year < 0)
25  return 0; /* ?? */
26 
27  return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
28 }
29 
30 
31 /*!
32  * \brief
33  *
34  * returns the number of days in 'year'
35  *
36  * \param year
37  * \param ad
38  * \return int
39  */
40 
41 int datetime_days_in_year(int year, int ad)
42 {
43  if (year == 0)
44  return datetime_error(-1, "datetime_days_in_year(): illegal year");
45 
46  if (datetime_is_leap_year(year, ad))
47  return 366;
48  else
49  return 365;
50 }
51 
52 
53 /*!
54  * \brief
55  *
56  * returns number of days in 'month' of a particular 'year'
57  *
58  * \param month
59  * \param year
60  * \param ad
61  * \return int
62  */
63 
64 int datetime_days_in_month(int year, int month, int ad)
65 {
66  static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
67 
68  if (month < 1 || month > 12)
69  return datetime_error(-1, "datetime_days_in_month(): illegal month");
70 
71  if (month == 2 && datetime_is_leap_year(year, ad))
72  return (29);
73 
74  return (days[month - 1]);
75 }
int datetime_error(int code, char *msg)
record &#39;code&#39; and &#39;msg&#39; as error code/msg (in static variables) code==0 will clear the error (ie set ...
int datetime_days_in_year(int year, int ad)
returns the number of days in &#39;year&#39;
Definition: datetime/misc.c:41
int datetime_days_in_month(int year, int month, int ad)
returns number of days in &#39;month&#39; of a particular &#39;year&#39;
Definition: datetime/misc.c:64
int datetime_is_leap_year(int year, int ad)
Definition: datetime/misc.c:18