Important Update:

- DateTimeCore:
  - Added new functions Get/SetDateTime() for combined date & time get/set
  - Added LocalTime param to ReadDate/Time() and BuildDate/TimeStr()
    - Allow input time to interpretted as ether UTC or local time
- DeviceCore:
  - Added SourceRef to HandleCommand()
This commit is contained in:
Charl Wentzel
2019-06-19 11:18:34 +02:00
parent 794b7e5486
commit 207caa696d
5 changed files with 129 additions and 58 deletions

View File

@@ -20,24 +20,24 @@ 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
// Get current Epoch (UTC)
gettimeofday( &tv, &tz);
// Change to new time
// 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
// Convert back from local time to epoch time
tv.tv_sec = mktime( &NewTime );
tv.tv_usec = 0;
@@ -47,43 +47,24 @@ bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds
}
//---------------------------------------------------------------------------
// 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
// Get current Epoch (UTC)
gettimeofday( &tv, &tz );
// Change to new time
// 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
// Convert back from local time to epoch time
tv.tv_sec = mktime( &NewDate );
tv.tv_usec = 0;
@@ -94,17 +75,70 @@ bool SetDate( unsigned char Day, unsigned char Month, unsigned Year )
}
//---------------------------------------------------------------------------
// Get current data from real-time clock
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 date and time
// Get current UTC date and time
time( &UTC );
// Convert to local time
localtime_r( &UTC, &CurrentDate );
// Extract date
// Extract date only
Day = CurrentDate.tm_mday;
Month = CurrentDate.tm_mon + 1;
Year = CurrentDate.tm_year + 1900;
@@ -113,7 +147,31 @@ bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year )
}
//---------------------------------------------------------------------------
// Get the current date in a string
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;
@@ -172,12 +230,15 @@ char const * GetDateTimeStr( const char * DateSeparator, const char * TimeSepar
//---------------------------------------------------------------------------
// Get current time from real-time clock
bool ReadTime( const time_t EpochTime, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds )
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
localtime_r( &EpochTime, &CurrentTime );
if (LocalTime)
localtime_r( &EpochTime, &CurrentTime );
else
gmtime_r( &EpochTime, &CurrentTime );
// Extract time
Hours = CurrentTime.tm_hour;
@@ -188,12 +249,15 @@ bool ReadTime( const time_t EpochTime, unsigned char &Hours, unsigned char &Minu
}
//---------------------------------------------------------------------------
bool ReadDate( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year )
bool ReadDate( const time_t EpochTime, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year )
{
struct tm CurrentDate;
// Get current date and time
localtime_r( &EpochTime, &CurrentDate );
if (LocalTime)
localtime_r( &EpochTime, &CurrentDate );
else
gmtime_r( &EpochTime, &CurrentDate );
// Extract date
Day = CurrentDate.tm_mday;
@@ -204,13 +268,16 @@ bool ReadDate( const time_t EpochTime, unsigned char &Day, unsigned char &Month,
}
//---------------------------------------------------------------------------
bool ReadDateTime( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year,
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
localtime_r( &EpochTime, &CurrentTime );
if (LocalTime)
localtime_r( &EpochTime, &CurrentTime );
else
gmtime_r( &EpochTime, &CurrentTime );
// Extract time
Hours = CurrentTime.tm_hour;
@@ -247,7 +314,7 @@ bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char
Seconds = (TempSeconds != -1)? TempSeconds : (Maxtime)? 59 : 0;
// Check if success
return ((ItemsAssigned == 6)? true : false);
return ((ItemsAssigned >= 3)? true : false);
}
//---------------------------------------------------------------------------
@@ -271,7 +338,7 @@ bool ReadDateStr( const char *DateTimeStr, unsigned char &Day, unsigned char &M
Year = TempYear;
// Check if success
return ((ItemsAssigned == 6)? true : false);
return ((ItemsAssigned >= 3)? true : false);
}
//---------------------------------------------------------------------------
@@ -300,19 +367,19 @@ bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned cha
Seconds = (TempSeconds != -1)? TempSeconds : (Maxtime)? 59 : 0;
// Check if success
return ((ItemsAssigned == 6)? true : false);
return ((ItemsAssigned >= 3)? true : false);
}
//---------------------------------------------------------------------------
// Get the current date in a string
char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator )
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, Day, Month, Year );
ReadDate( EpochTime, LocalTime, Day, Month, Year );
// Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d",
@@ -323,14 +390,14 @@ char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator )
}
//---------------------------------------------------------------------------
char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator )
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, Hours, Minutes, Seconds );
ReadTime( EpochTime, LocalTime, Hours, Minutes, Seconds );
// Build String
sprintf( ReturnStr, "%02d%s%02d%s%02d",
@@ -340,7 +407,7 @@ char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator )
}
//---------------------------------------------------------------------------
char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparator, const char * TimeSeparator )
char const * BuildDateTimeStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator, const char * TimeSeparator )
{
unsigned char Day;
unsigned char Month;
@@ -350,7 +417,7 @@ char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparato
unsigned char Seconds;
// Get Date & Time
ReadDateTime( EpochTime, Day, Month, Year, Hours, Minutes, Seconds );
ReadDateTime( EpochTime, LocalTime, Day, Month, Year, Hours, Minutes, Seconds );
// Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d",