work_week.cpp
Go to the documentation of this file.
1 
4 #include "work_week.h"
5 #include <string>
6 #include <stdexcept>
7 #include <sstream>
8 
9 static int work_weeks_between_years( unsigned year, unsigned other_year )
10 {
11  unsigned Jan_1_JDN = utilities::time::jdn( year, 1, 1 );
12  unsigned other_Jan_1_JDN = utilities::time::jdn( other_year, 1, 1 );
13  // We need to compare between weeks, so we get the JDN of the Sunday of the wanted weeks
14  // (JDN + 1) % 7 gives us the day of the week for the JDN. 0 -> Sun, 1 -> Mon ...
15  int start_of_year_first_work_week = Jan_1_JDN - ( ( Jan_1_JDN + 1 ) % 7 );
16  int start_of_other_year_first_work_week = other_Jan_1_JDN - ( ( other_Jan_1_JDN + 1 ) % 7 );
17  // Subtracting the JDNs results in the number of days between 2 dates
18  return ( ( start_of_year_first_work_week - start_of_other_year_first_work_week ) / 7 );
19 }
20 
21 static unsigned days_in_month( unsigned year, unsigned month )
22 {
23  if( month == 2 )
24  {
25  if( ( year % 400 == 0 ) || ( year % 4 == 0 && year % 100 != 0 ) )
26  return 29;
27  else
28  return 28;
29  }
30  // Months with 30 days
31  else if( month == 4 || month == 6 || month == 9 || month == 11 )
32  return 30;
33  else
34  return 31;
35 }
36 
37 namespace utilities {
38 namespace time {
39 
40 work_week::work_week( unsigned year, unsigned ww )
41 {
42  if( ww == 0 || ww > work_weeks_between_years( year + 1, year ) )
43  {
44  std::ostringstream message;
45  message << "Invalid work week given: " << year << " doesn't have a work week " << ww;
46  throw std::runtime_error(message.str());
47  }
48  _year = year;
49  _ww = ww;
50 }
51 
52 work_week::work_week( const std::time_t & t )
53 {
54  auto time = std::localtime( &t );
55 
56  _year = time->tm_year + 1900; // The tm_year field contains the number of years
57  // since 1900, we add 1900 to get current year
58  int Jan_1_wday = ( time->tm_wday - time->tm_yday ) % 7;
59  if( Jan_1_wday < 0 )
60  Jan_1_wday += 7;
61  _ww = ( ( time->tm_yday + Jan_1_wday ) / 7 ) + 1;
62  // As explained in the header file, the last few days of a year may belong to work week 1 of the
63  // following year. This is the case if we are at the end of December, and the next year start
64  // before the week ends
65  if( _ww == 53 && ( 31 - time->tm_mday ) < ( 6 - time->tm_wday ) )
66  {
67  _year++;
68  _ww = 1;
69  }
70 }
71 
72 unsigned work_week::get_year() const
73 {
74  return _year;
75 }
76 
77 unsigned work_week::get_work_week() const
78 {
79  return _ww;
80 }
81 
82 int work_week::operator-( const work_week & other ) const
83 {
84  return work_weeks_between_years( _year, other._year ) + ( this->_ww - other._ww );
85 }
86 
88 {
89  auto t = std::time( nullptr );
90  return work_week( t );
91 }
92 
94 {
95  auto now = work_week::current();
96  unsigned age = now - start;
97  return age;
98 }
99 
100 // Calculation is according to formula found in the link provided in the headet file
101 unsigned jdn( unsigned year, unsigned month, unsigned day )
102 {
103  if (month == 0 || day == 0 || month > 12 || day > days_in_month(year, month))
104  {
105  std::ostringstream message;
106  message << "Invalid date given: " << day << "/" << month << "/" << year;
107  throw std::runtime_error(message.str());
108  }
109  return ( ( 1461 * ( year + 4800 + ( ( (int)month - 14 ) / 12 ) ) ) / 4 )
110  + ( ( 367 * ( month - 2 - ( 12 * ( ( (int)month - 14 ) / 12 ) ) ) ) / 12 )
111  - ( ( 3 * ( ( year + 4900 + ( ( (int)month - 14 ) / 12 ) ) / 100 ) ) / 4 ) + day - 32075;
112 }
113 } // namespace time
114 } // namespace utilities
GLenum GLuint GLenum GLsizei const GLchar * message
static int work_weeks_between_years(unsigned year, unsigned other_year)
Definition: work_week.cpp:9
GLdouble t
unsigned get_year() const
Definition: work_week.cpp:72
GLuint start
unsigned get_work_weeks_since(const work_week &start)
Definition: work_week.cpp:93
work_week(unsigned year, unsigned ww)
Definition: work_week.cpp:40
int operator-(const work_week &ww) const
Definition: work_week.cpp:82
static unsigned days_in_month(unsigned year, unsigned month)
Definition: work_week.cpp:21
static work_week current()
Definition: work_week.cpp:87
unsigned get_work_week() const
Definition: work_week.cpp:77
unsigned jdn(unsigned year, unsigned month, unsigned day)
Definition: work_week.cpp:101


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:23