Plymouth ELEC351 Group T
/
ELEC351_Group_T
FINAL PROJECT isn't it
Fork of ELEC351 by
TIME.cpp@57:aba1296e51b1, 2018-08-15 (annotated)
- Committer:
- thomasmorris
- Date:
- Wed Aug 15 21:34:59 2018 +0000
- Revision:
- 57:aba1296e51b1
- Parent:
- 52:99915f5240b2
Final Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 31:4a88bf97b53e | 1 | #include "TIME.hpp" |
thomasmorris | 31:4a88bf97b53e | 2 | |
thomasmorris | 31:4a88bf97b53e | 3 | |
thomasmorris | 31:4a88bf97b53e | 4 | using namespace std; |
thomasmorris | 31:4a88bf97b53e | 5 | |
thomasmorris | 52:99915f5240b2 | 6 | Mutex Time_Lock; //Declare MUTEX |
chills | 33:3b5096f0126a | 7 | |
thomasmorris | 52:99915f5240b2 | 8 | int current_time_global = 0; //Declare required variable |
thomasmorris | 52:99915f5240b2 | 9 | int new_time = 0; //Declare required variable |
thomasmorris | 31:4a88bf97b53e | 10 | |
thomasmorris | 31:4a88bf97b53e | 11 | int get_current_time() //Get Current Time |
thomasmorris | 31:4a88bf97b53e | 12 | { |
thomasmorris | 52:99915f5240b2 | 13 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 14 | current_time_global = time(0); //Get current time |
thomasmorris | 52:99915f5240b2 | 15 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 16 | return current_time_global; //Return the current time |
thomasmorris | 31:4a88bf97b53e | 17 | } |
thomasmorris | 31:4a88bf97b53e | 18 | |
thomasmorris | 52:99915f5240b2 | 19 | void set_new_date(int days, int months, int years) //Set new date |
chills | 34:c0b8705f183d | 20 | { |
thomasmorris | 52:99915f5240b2 | 21 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 22 | time_t Time = time(NULL); //Get system time |
thomasmorris | 52:99915f5240b2 | 23 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 24 | tm* Time_Pointer = localtime(&Time); //Create pointer to Time |
thomasmorris | 52:99915f5240b2 | 25 | set_time(get_current_time() - year*(Time_Pointer->tm_year - 70) + year*(years - 1970)); //Set the year |
thomasmorris | 52:99915f5240b2 | 26 | set_time(get_current_time() - month*Time_Pointer->tm_mon + month*(months - 1)); //Set the month |
thomasmorris | 52:99915f5240b2 | 27 | set_time(get_current_time() - day*Time_Pointer->tm_mday + day*days); //Set the day |
chills | 34:c0b8705f183d | 28 | } |
chills | 34:c0b8705f183d | 29 | |
thomasmorris | 52:99915f5240b2 | 30 | void set_new_time(int hours, int minutes, int seconds) //Set new time |
chills | 34:c0b8705f183d | 31 | { |
thomasmorris | 52:99915f5240b2 | 32 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 33 | time_t Time = time(NULL); //Get system time |
thomasmorris | 52:99915f5240b2 | 34 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 35 | tm* Time_Pointer = localtime(&Time); //Create pointer to Time |
thomasmorris | 52:99915f5240b2 | 36 | set_time(get_current_time() - second*Time_Pointer->tm_sec + second*seconds); //Set the second |
thomasmorris | 52:99915f5240b2 | 37 | set_time(get_current_time() - minute*Time_Pointer->tm_min + minute*minutes); //Set the minute |
thomasmorris | 52:99915f5240b2 | 38 | set_time(get_current_time() - hour*Time_Pointer->tm_hour + hour*hours); //Set the hour |
chills | 34:c0b8705f183d | 39 | } |
chills | 34:c0b8705f183d | 40 | |
thomasmorris | 52:99915f5240b2 | 41 | void Add_Second() //Add one second to current time |
thomasmorris | 31:4a88bf97b53e | 42 | { |
thomasmorris | 52:99915f5240b2 | 43 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 44 | new_time = time(0) + 1; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 45 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 46 | set_time(new_time); //Set new time |
thomasmorris | 52:99915f5240b2 | 47 | } |
thomasmorris | 52:99915f5240b2 | 48 | |
thomasmorris | 52:99915f5240b2 | 49 | void Subtract_Second() //Subtract one second from current time |
thomasmorris | 52:99915f5240b2 | 50 | { |
thomasmorris | 52:99915f5240b2 | 51 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 52 | new_time = time(0) - 1; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 53 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 54 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 55 | } |
thomasmorris | 31:4a88bf97b53e | 56 | |
thomasmorris | 52:99915f5240b2 | 57 | void Add_Minute() //Add one minute to the current time |
thomasmorris | 31:4a88bf97b53e | 58 | { |
thomasmorris | 52:99915f5240b2 | 59 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 60 | new_time = time(0) + 60; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 61 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 62 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 63 | } |
thomasmorris | 31:4a88bf97b53e | 64 | |
thomasmorris | 52:99915f5240b2 | 65 | void Subtract_Minute() //Subtract one minute from current time |
thomasmorris | 31:4a88bf97b53e | 66 | { |
thomasmorris | 52:99915f5240b2 | 67 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 68 | new_time = time(0) - 60; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 69 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 70 | set_time(new_time); //Set new time |
thomasmorris | 52:99915f5240b2 | 71 | |
thomasmorris | 31:4a88bf97b53e | 72 | } |
thomasmorris | 31:4a88bf97b53e | 73 | |
thomasmorris | 52:99915f5240b2 | 74 | void Add_Hour() //Add one hour to the current time |
thomasmorris | 31:4a88bf97b53e | 75 | { |
thomasmorris | 52:99915f5240b2 | 76 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 77 | new_time = time(0) + 3600; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 78 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 79 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 80 | } |
thomasmorris | 31:4a88bf97b53e | 81 | |
thomasmorris | 52:99915f5240b2 | 82 | void Subtract_Hour() //Subtract one hour from the current time |
thomasmorris | 31:4a88bf97b53e | 83 | { |
thomasmorris | 52:99915f5240b2 | 84 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 85 | new_time = time(0) - 3600; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 86 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 87 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 88 | } |
thomasmorris | 31:4a88bf97b53e | 89 | |
thomasmorris | 52:99915f5240b2 | 90 | void Add_Day() //Add one day to the current time |
thomasmorris | 31:4a88bf97b53e | 91 | { |
thomasmorris | 52:99915f5240b2 | 92 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 93 | new_time = time(0) + 86400; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 94 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 95 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 96 | } |
thomasmorris | 31:4a88bf97b53e | 97 | |
thomasmorris | 52:99915f5240b2 | 98 | void Subtract_Day() //Subtract one day from the current time |
thomasmorris | 31:4a88bf97b53e | 99 | { |
thomasmorris | 52:99915f5240b2 | 100 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 101 | new_time = time(0) - 86400; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 102 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 103 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 104 | } |
thomasmorris | 31:4a88bf97b53e | 105 | |
thomasmorris | 52:99915f5240b2 | 106 | void Add_Month() //Add one month to current time |
thomasmorris | 31:4a88bf97b53e | 107 | { |
thomasmorris | 52:99915f5240b2 | 108 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 109 | new_time = time(0) + 2629743; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 110 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 111 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 112 | } |
thomasmorris | 31:4a88bf97b53e | 113 | |
thomasmorris | 52:99915f5240b2 | 114 | void Subtract_Month() //Subtract one month from current time |
thomasmorris | 31:4a88bf97b53e | 115 | { |
thomasmorris | 52:99915f5240b2 | 116 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 117 | new_time = time(0) - 2629743; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 118 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 119 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 120 | } |
thomasmorris | 31:4a88bf97b53e | 121 | |
thomasmorris | 52:99915f5240b2 | 122 | void Add_Year() //Add one year to current time |
thomasmorris | 31:4a88bf97b53e | 123 | { |
thomasmorris | 52:99915f5240b2 | 124 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 125 | new_time = time(0) + 31556926; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 126 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 127 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 128 | } |
thomasmorris | 31:4a88bf97b53e | 129 | |
thomasmorris | 52:99915f5240b2 | 130 | void Subtract_Year() //Subtract one year from current time |
thomasmorris | 52:99915f5240b2 | 131 | { |
thomasmorris | 52:99915f5240b2 | 132 | Time_Lock.lock(); //Apply MUTEX lock |
thomasmorris | 52:99915f5240b2 | 133 | new_time = time(0) - 31556926; //Calculate new time |
thomasmorris | 52:99915f5240b2 | 134 | Time_Lock.unlock(); //Release MUTEX lock |
thomasmorris | 52:99915f5240b2 | 135 | set_time(new_time); //Set new time |
thomasmorris | 31:4a88bf97b53e | 136 | } |