Tijl Goens
/
EpaperdisplayII
tempcommit 13/05
Datetime.cpp@2:048e163245b7, 2019-05-15 (annotated)
- Committer:
- tijl
- Date:
- Wed May 15 13:39:22 2019 +0000
- Revision:
- 2:048e163245b7
- Parent:
- 1:63664175e603
blub 15/05;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tijl | 1:63664175e603 | 1 | #include "mbed.h" |
tijl | 1:63664175e603 | 2 | #include "Datetime.h" |
tijl | 1:63664175e603 | 3 | #include <string> |
tijl | 1:63664175e603 | 4 | #include <sstream> |
tijl | 1:63664175e603 | 5 | #include <iostream> |
tijl | 1:63664175e603 | 6 | using namespace std; |
tijl | 1:63664175e603 | 7 | |
tijl | 1:63664175e603 | 8 | Datetime::Datetime(string datetime) { |
tijl | 1:63664175e603 | 9 | /* Voorbeeld datetime input |
tijl | 1:63664175e603 | 10 | "2019-03-22 15:45:00" |
tijl | 1:63664175e603 | 11 | */ |
tijl | 1:63664175e603 | 12 | this->year = atoi(datetime.substr(0,4).c_str()); |
tijl | 1:63664175e603 | 13 | this->month = atoi(datetime.substr(5,2).c_str()); |
tijl | 1:63664175e603 | 14 | this->day = atoi(datetime.substr(8,2).c_str()); |
tijl | 1:63664175e603 | 15 | this->hour = atoi(datetime.substr(11,2).c_str()); |
tijl | 1:63664175e603 | 16 | this->minute = atoi(datetime.substr(14,2).c_str()); |
tijl | 1:63664175e603 | 17 | this->second = atoi(datetime.substr(17,2).c_str()); |
tijl | 1:63664175e603 | 18 | } |
tijl | 1:63664175e603 | 19 | |
tijl | 1:63664175e603 | 20 | Datetime::~Datetime() { |
tijl | 1:63664175e603 | 21 | //int's are freed automatically |
tijl | 1:63664175e603 | 22 | } |
tijl | 1:63664175e603 | 23 | |
tijl | 1:63664175e603 | 24 | int Datetime::getDay() { |
tijl | 1:63664175e603 | 25 | return day; |
tijl | 1:63664175e603 | 26 | } |
tijl | 1:63664175e603 | 27 | |
tijl | 1:63664175e603 | 28 | int Datetime::getMonth() { |
tijl | 1:63664175e603 | 29 | return month; |
tijl | 1:63664175e603 | 30 | } |
tijl | 1:63664175e603 | 31 | |
tijl | 1:63664175e603 | 32 | int Datetime::getYear() { |
tijl | 1:63664175e603 | 33 | return year; |
tijl | 1:63664175e603 | 34 | } |
tijl | 1:63664175e603 | 35 | |
tijl | 1:63664175e603 | 36 | int Datetime::getHour() { |
tijl | 1:63664175e603 | 37 | return hour; |
tijl | 1:63664175e603 | 38 | } |
tijl | 1:63664175e603 | 39 | |
tijl | 1:63664175e603 | 40 | int Datetime::getMinute() { |
tijl | 1:63664175e603 | 41 | return minute; |
tijl | 1:63664175e603 | 42 | } |
tijl | 1:63664175e603 | 43 | |
tijl | 1:63664175e603 | 44 | int Datetime::getSecond() { |
tijl | 1:63664175e603 | 45 | return second; |
tijl | 1:63664175e603 | 46 | } |
tijl | 1:63664175e603 | 47 | |
tijl | 1:63664175e603 | 48 | string Datetime::getDate() { |
tijl | 1:63664175e603 | 49 | string date = ""; |
tijl | 1:63664175e603 | 50 | string buf; // string which will contain the result |
tijl | 1:63664175e603 | 51 | ostringstream convert0; // stream used for the conversion |
tijl | 1:63664175e603 | 52 | ostringstream convert1; |
tijl | 1:63664175e603 | 53 | ostringstream convert2; |
tijl | 1:63664175e603 | 54 | convert0 << day; // insert the textual representation of 'Number' in the characters in the stream |
tijl | 1:63664175e603 | 55 | buf = convert0.str(); // set 'Result' to the contents of the stream |
tijl | 1:63664175e603 | 56 | if(day < 10) {date += "0";} |
tijl | 1:63664175e603 | 57 | date += buf + "/"; |
tijl | 1:63664175e603 | 58 | if(month < 10) {date += "0";} |
tijl | 1:63664175e603 | 59 | convert1 << month; |
tijl | 1:63664175e603 | 60 | buf = convert1.str(); |
tijl | 1:63664175e603 | 61 | date += buf + "/"; |
tijl | 1:63664175e603 | 62 | convert2 << year; |
tijl | 1:63664175e603 | 63 | buf = convert2.str(); |
tijl | 1:63664175e603 | 64 | date += buf; |
tijl | 1:63664175e603 | 65 | return date; |
tijl | 1:63664175e603 | 66 | } |
tijl | 1:63664175e603 | 67 | |
tijl | 1:63664175e603 | 68 | string Datetime::getTime() { |
tijl | 1:63664175e603 | 69 | string time = ""; |
tijl | 1:63664175e603 | 70 | string buf; // string which will contain the result |
tijl | 1:63664175e603 | 71 | ostringstream convert0; // stream used for the conversion |
tijl | 1:63664175e603 | 72 | ostringstream convert1; |
tijl | 1:63664175e603 | 73 | ostringstream convert2; |
tijl | 1:63664175e603 | 74 | convert0 << hour; // insert the textual representation of 'Number' in the characters in the stream |
tijl | 1:63664175e603 | 75 | buf = convert0.str(); // set 'Result' to the contents of the stream |
tijl | 1:63664175e603 | 76 | if(hour < 10) {time += "0";} |
tijl | 1:63664175e603 | 77 | time += buf + ":"; |
tijl | 1:63664175e603 | 78 | if(minute < 10) {time += "0";} |
tijl | 1:63664175e603 | 79 | convert1 << minute; |
tijl | 1:63664175e603 | 80 | buf = convert1.str(); |
tijl | 1:63664175e603 | 81 | time += buf + ":"; |
tijl | 1:63664175e603 | 82 | if(second < 10) {time += "0";} |
tijl | 1:63664175e603 | 83 | convert2 << second; |
tijl | 1:63664175e603 | 84 | buf = convert2.str(); |
tijl | 1:63664175e603 | 85 | time += buf; |
tijl | 1:63664175e603 | 86 | return time; |
tijl | 1:63664175e603 | 87 | } |
tijl | 1:63664175e603 | 88 | |
tijl | 1:63664175e603 | 89 | string Datetime::getDatetime() { |
tijl | 1:63664175e603 | 90 | return getDate() + " " + getTime(); |
tijl | 1:63664175e603 | 91 | } |