ALMaSS Rabbit ODdox  1.00
The rabbit model description following ODdox protocol
calendar.cpp
Go to the documentation of this file.
1 /*
2 *******************************************************************************************************
3 Copyright (c) 2011, Christopher John Topping, University of Aarhus
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided
7 that the following conditions are met:
8 
9 Redistributions of source code must retain the above copyright notice, this list of conditions and the
10 following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12 the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
17 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
19 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 ********************************************************************************************************
23 */
24 
25 #include <stdlib.h>
26 #include <cstdlib>
27 
28 
29 #include <string>
30 #include "maperrormsg.h"
31 #include "calendar.h"
32 
33 // Auto generated by 'daylength.pl'.
34 #include "daylength.h"
35 
36 using namespace std;
37 
39 
40 void Calendar::Reset( void )
41 {
42  m_date = 0;
43  m_olddays = 0;
44  m_day_in_month = 1;
45  m_minutes = 0;
46  m_hours = 0;
47  m_day_in_year = 0; // [0..364]
48  m_month = 0; // [0..11]
50  m_simulationyear = 0;
51  m_janfirst = true;
52  m_marchfirst = false;
53 }
54 
56 {
57  Reset();
58 }
59 
60 
61 long Calendar::GlobalDate( int a_day, int a_month, int a_year )
62 {
63  return m_olddays + DayInYear( a_day, a_month) + a_year*365;
64 }
65 
66 
67 int Calendar::DayLength( int a_day_in_year )
68 {
69  if ( a_day_in_year<0 || a_day_in_year>364 ) {
70  g_msg->Warn( WARN_BUG, "Calendar::DayLength(): Day outside a year!",
71  "" );
72  exit(1);
73  }
74  return m_daylength[ a_day_in_year ];
75 }
76 
77 
78 
79 bool Calendar::ValidDate( int a_day, int a_month )
80 {
81  const int m_monthlength[12] = {
82  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
83 
84  if ( a_month < 1 ||
85  a_month > 12 ||
86  a_day < 1 ||
87  a_day > m_monthlength[ a_month - 1 ] ) {
88  return false;
89  } else {
90  return true;
91  }
92 }
93 
94 
95 
96 int Calendar::DayInYear( int a_day, int a_month )
97 {
98  const int m_monthsum[12] = {
99  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
100 
101  return m_monthsum[ a_month - 1 ] + a_day - 1;
102 }
103 
104 
105 
107 {
108  bool dotick = false;
109 
110  if (m_minutes++ > 59) {
111  m_minutes = 0;
112  dotick = true;
113  }
114  return dotick;
115 }
116 
118 {
119  bool dotick = false;
120  m_minutes += 10;
121  if (m_minutes > 59) {
122  m_minutes = 0;
123  dotick = true;
124  }
125  return dotick;
126 }
127 
129 {
130  bool dotick = false;
131 
132  if ( ++m_hours > 23 ) {
133  m_minutes = 0;
134  m_hours = 0;
135  dotick = true;
136  }
137  return dotick;
138 }
139 
140 
141 void Calendar::Tick( void )
142 {
143  const int m_monthlength[12] = {
144  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
145  const int m_maxmonth = 11;
146 
147  m_minutes = 0;
148  m_hours = 0;
149  m_date++;
150  m_day_in_year++;
151  m_day_in_month++;
152  m_janfirst = false;
153  m_marchfirst = false;
154 
155  if ( m_day_in_month > m_monthlength[ m_month ])
156  {
157  m_day_in_month = 1;
158  m_month++;
159  if ( m_month == 2 )
160  // March 1st.
161  m_marchfirst = true;
162  }
163 
164  if ( m_month > m_maxmonth )
165  {
166  m_month = 0;
167  m_year++;
169  m_olddays += 365;
170  m_janfirst = true;
171  m_day_in_year = 0;
172  }
175 }
176 
int m_hours
Definition: calendar.h:98
static int m_daylength[]
The length of the day between sun up and sun down im minutes (DK - Jutland)
Definition: calendar.h:102
long m_olddays
Definition: calendar.h:87
int m_firstyear
Definition: calendar.h:88
int m_day_in_month
Definition: calendar.h:90
int m_todayslength
Definition: calendar.h:95
bool TickMinute(void)
Definition: calendar.cpp:106
void Reset(void)
Definition: calendar.cpp:40
long m_date
Definition: calendar.h:86
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
double m_daylightproportion
Definition: calendar.h:96
int m_year
Definition: calendar.h:93
int DayInYear(void)
Definition: calendar.h:58
bool ValidDate(int a_day, int a_month)
Definition: calendar.cpp:79
bool TickMinute10(void)
Definition: calendar.cpp:117
bool m_janfirst
Definition: calendar.h:99
int m_day_in_year
Definition: calendar.h:91
class Calendar * g_date
Definition: calendar.cpp:38
bool m_marchfirst
Definition: calendar.h:100
int m_minutes
Definition: calendar.h:97
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:56
int DayLength(void)
Definition: calendar.h:63
Calendar(void)
Definition: calendar.cpp:55
int m_month
Definition: calendar.h:92
long GlobalDate(int a_day, int a_month, int a_year)
Definition: calendar.cpp:61
int m_simulationyear
Definition: calendar.h:94
void Tick(void)
Definition: calendar.cpp:141
bool TickHour(void)
Definition: calendar.cpp:128