Real Time Clock. get time-str, set time.
Revision 0:006ab0d7b90f, committed 2015-06-11
- Comitter:
- AkinoriHashimoto
- Date:
- Thu Jun 11 01:55:55 2015 +0000
- Child:
- 1:b4e796fd32a9
- Commit message:
- Real Time Clock
Changed in this revision
RealTimeClock.cpp | Show annotated file Show diff for this revision Revisions of this file |
RealTimeClock.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RealTimeClock.cpp Thu Jun 11 01:55:55 2015 +0000 @@ -0,0 +1,62 @@ +#include "RealTimeClock.h" + +void RealTimeClock::setSecondsRealTime() +{ + secRT = time(NULL); + return; +} + +string RealTimeClock::getHMS8() +{ + setSecondsRealTime(); + strftime(buf, 10, "%H:%M:%S", localtime(&secRT)); + bufTmp= buf; + return bufTmp; +} +string RealTimeClock::getHMS6() +{ + setSecondsRealTime(); + strftime(buf, 8, "%H%M%S", localtime(&secRT)); + bufTmp= buf; + return bufTmp; +} + +string RealTimeClock::getYMD8() +{ + setSecondsRealTime(); + strftime(buf, 10, "%y/%m/%d", localtime(&secRT)); + bufTmp= buf; + return bufTmp; +} +string RealTimeClock::getYMD6() +{ + setSecondsRealTime(); + strftime(buf, 8, "%y%m%d", localtime(&secRT)); + bufTmp= buf; + return bufTmp; +} + + +bool RealTimeClock::setRealTime(string str) +{ + long now= A2I(str, 10); + now += 3600* 9; // + JST 9hour. + // 1,427,597,183(ISO 8601形式: 2015/03/29 02:46:23Z) + // 2,147,483,647秒を経過した (2038/01/19 03:14:07) + if(!(1427597183<now && now<2147483600)) + return false; + if(now == -1) + return false; + + // now is in range of appropriate time. + setRealTime(now); + return true; +} + +void RealTimeClock::setRealTime(long now) +{ + set_time(now); + return; +} + +// End of File \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RealTimeClock.h Thu Jun 11 01:55:55 2015 +0000 @@ -0,0 +1,44 @@ +#pragma once + +#include "mbed.h" +#include <string> + +#include "StrLib.h" + +/** Real Time Clock library + * + */ +class RealTimeClock +{ +private: + time_t secRT; // secondsRealTime. JST; 9hour + char buf[14]; // using for strftime() + string bufTmp; // return value + void setSecondsRealTime(); // set secRt; + +public: + /** @rtnval YY/MM/DD */ + string getYMD8(); + + /** @rtnval YYMMDD */ + string getYMD6(); + + /** @rtnval HH:MM:SS */ + string getHMS8(); + + /** @rtnval HHMMSS */ + string getHMS6(); + + /** + * @param seconds from 1970/01/01. + * @rtnval success(true), failure(false) + */ + bool setRealTime(string str); + + /** + * @param seconds from 1970/01/01. + * @rtnval void + */ + void setRealTime(long now); + +}; \ No newline at end of file