From 02b43fe6b8881ab0731f36d37ff36fa2fa88a540 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Fri, 3 Mar 2017 18:00:44 +0200 Subject: [PATCH 1/2] Important update: - Add new DateTimeCore file - Provides functions to get/set system date - Create Date, Time and Date/Time string with custom separators --- CMakeLists.txt | 2 +- DateTimeCore.cpp | 177 +++++++++++++++++++++++++++++++++++++++++++++++ DateTimeCore.h | 32 +++++++++ 3 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 DateTimeCore.cpp create mode 100644 DateTimeCore.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c413d7c..261847f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,3 @@ PROJECT(lib_redAcore) -ADD_LIBRARY(redAcore TimingCore.cpp LogCore.cpp SignalCore.cpp BufferCore.cpp FunctionCore.cpp DeviceCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp) +ADD_LIBRARY(redAcore TimingCore.cpp DateTimeCore.cpp LogCore.cpp SignalCore.cpp BufferCore.cpp FunctionCore.cpp DeviceCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp) diff --git a/DateTimeCore.cpp b/DateTimeCore.cpp new file mode 100644 index 0000000..487fd80 --- /dev/null +++ b/DateTimeCore.cpp @@ -0,0 +1,177 @@ +/* + * DateTimeCore.cpp + * + * Created on: 3 Mar 2017 + * Author: wentzelc + */ + +// redA Libraries +#include "DateTimeCore.h" + +// Standard C/C++ Libraries +#include +#include +#include +#include + +//#include +//#include + +//--------------------------------------------------------------------------- + +// Variable used to temp values with +char ReturnStr[20]; + +//--------------------------------------------------------------------------- + +// 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); +} +//--------------------------------------------------------------------------- + diff --git a/DateTimeCore.h b/DateTimeCore.h new file mode 100644 index 0000000..1c013a0 --- /dev/null +++ b/DateTimeCore.h @@ -0,0 +1,32 @@ +/* + * DateTimeCore.h + * + * Created on: 3 Mar 2017 + * Author: wentzelc + */ + +#ifndef REDACORE_DATETIMECORE_H_ +#define REDACORE_DATETIMECORE_H_ + +// redA Libraries +/* none */ + +// Standard C/C++ Libraries +#include + +//--------------------------------------------------------------------------- + +// Get and set System Date and Time +bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds ); +bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ); + +bool SetDate( unsigned char Day, unsigned char Month, unsigned Year ); +bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year ); + +char const * GetDateStr( const char * DateSeparator = NULL ); +char const * GetTimeStr( const char * TimeSeparator = NULL ); +char const * GetDateTimeStr( const char * DateSeparator = NULL, const char * TimeSeparator = NULL ); + +//--------------------------------------------------------------------------- + +#endif /* REDACORE_DATETIMECORE_H_ */ From ae95602f646c8dd48c5b0e4aab03cb5777664ff4 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Sat, 4 Mar 2017 21:44:39 +0200 Subject: [PATCH 2/2] Important update: - Bug fix: make DateTimeCore functions linkable by using it in CLogCore - CLogCore: - Rename FileOutput -> OutputFile - Add DateTimeStr to Output if FileNo > 3 --- DateTimeCore.cpp | 8 +++----- DateTimeCore.h | 2 +- LogCore.cpp | 49 +++++++++++++++++++++++++++++------------------- LogCore.h | 4 ++-- TimingCore.cpp | 3 ++- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/DateTimeCore.cpp b/DateTimeCore.cpp index 487fd80..55df337 100644 --- a/DateTimeCore.cpp +++ b/DateTimeCore.cpp @@ -1,7 +1,7 @@ /* * DateTimeCore.cpp * - * Created on: 3 Mar 2017 + * Created on: 3 March 2017 * Author: wentzelc */ @@ -13,14 +13,12 @@ #include #include #include - -//#include -//#include +#include //--------------------------------------------------------------------------- // Variable used to temp values with -char ReturnStr[20]; +static char ReturnStr[20]; //--------------------------------------------------------------------------- diff --git a/DateTimeCore.h b/DateTimeCore.h index 1c013a0..bfaaf60 100644 --- a/DateTimeCore.h +++ b/DateTimeCore.h @@ -12,7 +12,7 @@ /* none */ // Standard C/C++ Libraries -#include +#include //--------------------------------------------------------------------------- diff --git a/LogCore.cpp b/LogCore.cpp index 104642c..c1b3922 100644 --- a/LogCore.cpp +++ b/LogCore.cpp @@ -7,6 +7,7 @@ // redA Libraries #include "LogCore.h" +#include "DateTimeCore.h" // Standard C/C++ Libraries #include @@ -22,9 +23,9 @@ char LogStr[1000]; // Temporary var to create log messages, //--------------------------------------------------------------------------- -CLogCore::CLogCore( FILE * pFileOutput ) +CLogCore::CLogCore( FILE * pOutputFile ) { - FileOutput = pFileOutput; + OutputFile = pOutputFile; } //--------------------------------------------------------------------------- @@ -37,14 +38,19 @@ bool CLogCore::Message( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const char return false; // Check debug level - if (!FileOutput || (MsgLevel > DebugLevel)) { + if (!OutputFile || (MsgLevel > DebugLevel)) { return true; } - // Show normal output + // Show Date & Time + if (OutputFile->_fileno > 3) { + fprintf( OutputFile, "%s", GetDateTimeStr( "/", ":" )); + } + + // Print formated message va_start( ArgPtr, Format ); - vfprintf( FileOutput, Format, ArgPtr ); - fprintf( FileOutput, "\r\n" ); + vfprintf( OutputFile, Format, ArgPtr ); + fprintf( OutputFile, "\r\n" ); va_end( ArgPtr ); return true; } @@ -59,7 +65,7 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short return false; // Check debug level - if (!FileOutput || (MsgLevel > DebugLevel)) { + if (!OutputFile || (MsgLevel > DebugLevel)) { return true; } @@ -67,10 +73,15 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short if (Len == -1) Len = strlen( Buffer ); + // Show date & time + if (OutputFile->_fileno > 3) { + fprintf( OutputFile, "%s", GetDateTimeStr( "/", ":" )); + } + // Show Lead if (Format && *Format) { va_start( ArgPtr, Format ); - vfprintf( FileOutput, Format, ArgPtr ); + vfprintf( OutputFile, Format, ArgPtr ); va_end( ArgPtr ); } @@ -78,11 +89,11 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short if (Show & OUT_COUNT) { // Print byte count - fprintf( FileOutput, " [%d] ", Len ); + fprintf( OutputFile, " [%d] ", Len ); // EOL if only count wanted if (Show & OUT_COUNT) { - fprintf( FileOutput, "\n" ); + fprintf( OutputFile, "\n" ); } } @@ -91,25 +102,25 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short { if (Show & OUT_ASIS) { // Print entire buffer as is (line feeds included) - fprintf( FileOutput, "%s", Buffer ); + fprintf( OutputFile, "%s", Buffer ); } else { // Ignore \r\n for (int i=0; i 126)) { if ((Show & OUT_CRLF) && ((Buffer[i] == '\r') || (Buffer[i] == '\n'))) - fprintf( FileOutput, "%c", Buffer[i] ); + fprintf( OutputFile, "%c", Buffer[i] ); else - fprintf( FileOutput, "." ); + fprintf( OutputFile, "." ); } else { - fprintf( FileOutput, "%c", Buffer[i] ); + fprintf( OutputFile, "%c", Buffer[i] ); } } } // Add EOL if not present or ignored if (!(Show & (OUT_ASIS | OUT_CRLF)) || (Buffer[Len-1] != '\n')) { - fprintf( FileOutput, "\n" ); + fprintf( OutputFile, "\n" ); } } @@ -118,9 +129,9 @@ bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short { // Print Hex values of individual bytes for (int i=0; i MilliSeconds)? true : false); } //--------------------------------------------------------------------------- +