/* * 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]; //--------------------------------------------------------------------------- bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds ) { struct tm NewTime; struct timeval tv; struct timezone tz; // Get current Epoch (UTC) gettimeofday( &tv, &tz); // Convert to local time localtime_r( &tv.tv_sec, &NewTime ); // Set new time (keep date as is) NewTime.tm_hour = Hours; NewTime.tm_min = Minutes; NewTime.tm_sec = Seconds; // Convert back from local time to epoch time tv.tv_sec = mktime( &NewTime ); tv.tv_usec = 0; // Set date and new time settimeofday( &tv, &tz); return true; } //--------------------------------------------------------------------------- bool SetDate( unsigned char Day, unsigned char Month, unsigned Year ) { struct tm NewDate; struct timeval tv; struct timezone tz; // Get current Epoch (UTC) gettimeofday( &tv, &tz ); // Convert to local time localtime_r( &tv.tv_sec, &NewDate ); // Set new date (keep time as is) NewDate.tm_year = Year - 1900; NewDate.tm_mon = Month - 1; NewDate.tm_mday = Day; // Convert back from local time to epoch time tv.tv_sec = mktime( &NewDate ); tv.tv_usec = 0; // Set date and new time settimeofday( &tv, &tz); return true; } //--------------------------------------------------------------------------- bool SetDateTime( unsigned char Day, unsigned char Month, unsigned Year, unsigned char Hours, unsigned char Minutes, unsigned char Seconds ) { struct tm NewTime; struct timeval tv; struct timezone tz; // Get current Epoch (UTC) gettimeofday( &tv, &tz); // Convert to local time localtime_r( &tv.tv_sec, &NewTime ); // Set new date & time NewTime.tm_year = Year - 1900; NewTime.tm_mon = Month - 1; NewTime.tm_mday = Day; NewTime.tm_hour = Hours; NewTime.tm_min = Minutes; NewTime.tm_sec = Seconds; // Convert back from local time to epoch time tv.tv_sec = mktime( &NewTime ); tv.tv_usec = 0; // Set date and new time settimeofday( &tv, &tz); return true; } //--------------------------------------------------------------------------- bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ) { struct tm CurrentTime; time_t UTC; // Get current UTC date and time time( &UTC ); // Convert to local time localtime_r( &UTC, &CurrentTime ); // Extract time only Hours = CurrentTime.tm_hour; Minutes = CurrentTime.tm_min; Seconds = CurrentTime.tm_sec; return true; } //--------------------------------------------------------------------------- bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year ) { struct tm CurrentDate; time_t UTC; // Get current UTC date and time time( &UTC ); // Convert to local time localtime_r( &UTC, &CurrentDate ); // Extract date only Day = CurrentDate.tm_mday; Month = CurrentDate.tm_mon + 1; Year = CurrentDate.tm_year + 1900; return true; } //--------------------------------------------------------------------------- bool GetDateTime( unsigned char &Day, unsigned char &Month, unsigned &Year, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ) { struct tm CurrentTime; time_t UTC; // Get current UTC date and time time( &UTC ); // Convert to local time localtime_r( &UTC, &CurrentTime ); // Extract Date & time Day = CurrentTime.tm_mday; Month = CurrentTime.tm_mon + 1; Year = CurrentTime.tm_year + 1900; Hours = CurrentTime.tm_hour; Minutes = CurrentTime.tm_min; Seconds = CurrentTime.tm_sec; return true; } //--------------------------------------------------------------------------- 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, bool LocalTime, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ) { struct tm CurrentTime; // Get current date and time if (LocalTime) localtime_r( &EpochTime, &CurrentTime ); else gmtime_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, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year ) { struct tm CurrentDate; // Get current date and time if (LocalTime) localtime_r( &EpochTime, &CurrentDate ); else gmtime_r( &EpochTime, &CurrentDate ); // Extract date Day = CurrentDate.tm_mday; Month = CurrentDate.tm_mon + 1; Year = CurrentDate.tm_year + 1900; return true; } //--------------------------------------------------------------------------- bool ReadDateTime( const time_t EpochTime, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ) { struct tm CurrentTime; // Get current date and time if (LocalTime) localtime_r( &EpochTime, &CurrentTime ); else gmtime_r( &EpochTime, &CurrentTime ); // Extract time Hours = CurrentTime.tm_hour; Minutes = CurrentTime.tm_min; Seconds = CurrentTime.tm_sec; // Extract date Day = CurrentTime.tm_mday; Month = CurrentTime.tm_mon + 1; Year = CurrentTime.tm_year + 1900; return true; } //--------------------------------------------------------------------------- bool ReadDateTime( const char * DateTimeStr, bool LocalTime, time_t &EpochTime ) { int ItemsAssigned; struct tm NewTime; // Read time ItemsAssigned = sscanf( DateTimeStr, "%d%*[/-]%d%*[/-]%d%*[ ]%d%*[:]%d%*[:]%d", &(NewTime.tm_year), &(NewTime.tm_mon), &(NewTime.tm_mday), &(NewTime.tm_hour), &(NewTime.tm_min), &(NewTime.tm_sec) ); if (ItemsAssigned != 6) return false; // Update time NewTime.tm_year -= 1900; NewTime.tm_mon -= 1; NewTime.tm_zone = "UTC"; // Convert to Epoch Time EpochTime = mktime( &NewTime ) + ((LocalTime)? 0 : NewTime.tm_gmtoff); return true; } //--------------------------------------------------------------------------- // Read components from date/time string bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime ) { int ItemsAssigned; int TempDay; int TempMonth; int TempYear; int TempHours = -1; int TempMinutes = -1; int TempSeconds = -1; // Read string ItemsAssigned = sscanf( DateTimeStr, "%d%*[/-]%d%*[/-]%d%*[ ]%d%*[:]%d%*[:]%d", &TempYear, &TempMonth, &TempDay, &TempHours, &TempMinutes, &TempSeconds ); // Return Values Hours = (TempHours != -1)? TempHours : (Maxtime)? 23 : 0; Minutes = (TempMinutes != -1)? TempMinutes : (Maxtime)? 59 : 0; Seconds = (TempSeconds != -1)? TempSeconds : (Maxtime)? 59 : 0; // Check if success return ((ItemsAssigned >= 3)? true : false); } //--------------------------------------------------------------------------- bool ReadDateStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year ) { int ItemsAssigned; int TempDay; int TempMonth; int TempYear; int TempHours = -1; int TempMinutes = -1; int TempSeconds = -1; // Read string ItemsAssigned = sscanf( DateTimeStr, "%d%*[/-]%d%*[/-]%d%*[ ]%d%*[:]%d%*[:]%d", &TempYear, &TempMonth, &TempDay, &TempHours, &TempMinutes, &TempSeconds ); // Return Values Day = TempDay; Month = TempMonth; Year = TempYear; // Check if success return ((ItemsAssigned >= 3)? true : false); } //--------------------------------------------------------------------------- bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime ) { int ItemsAssigned; int TempDay; int TempMonth; int TempYear; int TempHours = -1; int TempMinutes = -1; int TempSeconds = -1; // Read string ItemsAssigned = sscanf( DateTimeStr, "%d%*[/-]%d%*[/-]%d%*[ ]%d%*[:]%d%*[:]%d", &TempYear, &TempMonth, &TempDay, &TempHours, &TempMinutes, &TempSeconds ); // Return Values Day = TempDay; Month = TempMonth; Year = TempYear; Hours = (TempHours != -1)? TempHours : (Maxtime)? 23 : 0; Minutes = (TempMinutes != -1)? TempMinutes : (Maxtime)? 59 : 0; Seconds = (TempSeconds != -1)? TempSeconds : (Maxtime)? 59 : 0; // Check if success return ((ItemsAssigned >= 3)? true : false); } //--------------------------------------------------------------------------- char const * BuildDateStr( unsigned char Day, unsigned char Month, unsigned Year, const char * DateSeparator ) { // Build String sprintf( ReturnStr, "%04d%s%02d%s%02d", Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day ); // Return value return (ReturnStr); } //--------------------------------------------------------------------------- char const * BuildTimeStr( unsigned char Hours, unsigned char Minutes, unsigned char Seconds, const char * TimeSeparator ) { // Build String sprintf( ReturnStr, "%02d%s%02d%s%02d", Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds ); return (ReturnStr); } //--------------------------------------------------------------------------- char const * BuildDateTimeStr( unsigned char Day, unsigned char Month, unsigned Year, unsigned char Hours, unsigned char Minutes, unsigned char Seconds, const char * DateSeparator, const char * TimeSeparator ) { // 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 the current date in a string char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator ) { unsigned char Day; unsigned char Month; unsigned int Year; // Get Date ReadDate( EpochTime, LocalTime, Day, Month, Year ); // Build String return BuildDateStr( Day, Month, Year, DateSeparator ); } //--------------------------------------------------------------------------- char const * BuildTimeStr( const time_t EpochTime, bool LocalTime, const char * TimeSeparator ) { unsigned char Hours; unsigned char Minutes; unsigned char Seconds; // Get Date & Time ReadTime( EpochTime, LocalTime, Hours, Minutes, Seconds ); // Build String return BuildTimeStr( Hours, Minutes, Seconds, TimeSeparator ); } //--------------------------------------------------------------------------- char const * BuildDateTimeStr( const time_t EpochTime, bool LocalTime, 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 ReadDateTime( EpochTime, LocalTime, Day, Month, Year, Hours, Minutes, Seconds ); // Build String return BuildDateTimeStr( Day, Month, Year, Hours, Minutes, Seconds, DateSeparator, TimeSeparator ); }