/* * DateTimeCore.cpp * * Created on: 3 March 2017 * Author: wentzelc */ // Standard C/C++ Libraries #include #include #include // redA Libraries #include "DateTimeCore.h" //--------------------------------------------------------------------------- // Variable used to temp values with static char ReturnStr[30]; //--------------------------------------------------------------------------- // Set current time on real-time clock bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds ) { struct tm NewTime; struct timeval tv; struct timezone tz; // Get current date and time gettimeofday( &tv, &tz); // Change to new time localtime_r( &tv.tv_sec, &NewTime ); NewTime.tm_hour = Hours; NewTime.tm_min = Minutes; NewTime.tm_sec = Seconds; // Convert back tv.tv_sec = mktime( &NewTime ); tv.tv_usec = 0; // Set date and new time settimeofday( &tv, &tz); return true; } //--------------------------------------------------------------------------- // Get current time from real-time clock bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ) { struct tm CurrentTime; time_t UTC; // Get current date and time time( &UTC ); localtime_r( &UTC, &CurrentTime ); // Extract time Hours = CurrentTime.tm_hour; Minutes = CurrentTime.tm_min; Seconds = CurrentTime.tm_sec; return true; } //--------------------------------------------------------------------------- // Set current date on real-time clock bool SetDate( unsigned char Day, unsigned char Month, unsigned Year ) { struct tm NewDate; struct timeval tv; struct timezone tz; // Get current date and time gettimeofday( &tv, &tz ); // Change to new time localtime_r( &tv.tv_sec, &NewDate ); NewDate.tm_year = Year - 1900; NewDate.tm_mon = Month - 1; NewDate.tm_mday = Day; // Convert back tv.tv_sec = mktime( &NewDate ); tv.tv_usec = 0; // Set date and new time settimeofday( &tv, &tz); return true; } //--------------------------------------------------------------------------- // Get current data from real-time clock bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year ) { struct tm CurrentDate; time_t UTC; // Get current date and time time( &UTC ); localtime_r( &UTC, &CurrentDate ); // Extract date Day = CurrentDate.tm_mday; Month = CurrentDate.tm_mon + 1; Year = CurrentDate.tm_year + 1900; return true; } //--------------------------------------------------------------------------- // Get the current date in a string char const * GetDateStr( const char * DateSeparator ) { unsigned char Day; unsigned char Month; unsigned int Year; // Get Date GetDate( Day, Month, Year ); // Build String sprintf( ReturnStr, "%04d%s%02d%s%02d", Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day ); // Return value return (ReturnStr); } //--------------------------------------------------------------------------- char const * GetTimeStr( const char * TimeSeparator ) { unsigned char Hours; unsigned char Minutes; unsigned char Seconds; // Get Date & Time GetTime( Hours, Minutes, Seconds ); // Build String sprintf( ReturnStr, "%02d%s%02d%s%02d", Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds ); return (ReturnStr); } //--------------------------------------------------------------------------- char const * GetDateTimeStr( const char * DateSeparator, const char * TimeSeparator ) { unsigned char Day; unsigned char Month; unsigned int Year; unsigned char Hours; unsigned char Minutes; unsigned char Seconds; // Get Date & Time GetDate( Day, Month, Year ); GetTime( Hours, Minutes, Seconds ); // Build String sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d", Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day, Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds ); return (ReturnStr); } //--------------------------------------------------------------------------- // Get current time from real-time clock bool ReadTime( const time_t EpochTime, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ) { struct tm CurrentTime; // Get current date and time localtime_r( &EpochTime, &CurrentTime ); // Extract time Hours = CurrentTime.tm_hour; Minutes = CurrentTime.tm_min; Seconds = CurrentTime.tm_sec; return true; } //--------------------------------------------------------------------------- bool ReadDate( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year ) { struct tm CurrentDate; // Get current date and time localtime_r( &EpochTime, &CurrentDate ); // Extract date Day = CurrentDate.tm_mday; Month = CurrentDate.tm_mon + 1; Year = CurrentDate.tm_year + 1900; return true; } //--------------------------------------------------------------------------- // Get the current date in a string char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator ) { unsigned char Day; unsigned char Month; unsigned int Year; // Get Date ReadDate( EpochTime, Day, Month, Year ); // Build String sprintf( ReturnStr, "%04d%s%02d%s%02d", Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day ); // Return value return (ReturnStr); } //--------------------------------------------------------------------------- char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator ) { unsigned char Hours; unsigned char Minutes; unsigned char Seconds; // Get Date & Time ReadTime( EpochTime, Hours, Minutes, Seconds ); // Build String sprintf( ReturnStr, "%02d%s%02d%s%02d", Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds ); return (ReturnStr); } //--------------------------------------------------------------------------- char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparator, const char * TimeSeparator ) { unsigned char Day; unsigned char Month; unsigned int Year; unsigned char Hours; unsigned char Minutes; unsigned char Seconds; // Get Date & Time ReadDate( EpochTime, Day, Month, Year ); ReadTime( EpochTime, Hours, Minutes, Seconds ); // Build String sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d", Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day, Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds ); return (ReturnStr); }