Important update:

- DateTimeCore:
  - Added ReadDateTime() method, combining ReadDate() & ReadTime()
  - Added Read(Time/Date/DateTime)Str() methods for decoding date string
This commit is contained in:
Charl Wentzel
2019-06-16 13:56:26 +02:00
parent ac649bf4fb
commit 8791e25d22
2 changed files with 108 additions and 2 deletions

View File

@@ -204,6 +204,106 @@ 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,
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;
// Extract date
Day = CurrentTime.tm_mday;
Month = CurrentTime.tm_mon + 1;
Year = CurrentTime.tm_year + 1900;
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 == 6)? 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 == 6)? 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 == 6)? true : false);
}
//---------------------------------------------------------------------------
// Get the current date in a string
char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator )
{
@@ -250,8 +350,7 @@ char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparato
unsigned char Seconds;
// Get Date & Time
ReadDate( EpochTime, Day, Month, Year );
ReadTime( EpochTime, Hours, Minutes, Seconds );
ReadDateTime( EpochTime, Day, Month, Year, Hours, Minutes, Seconds );
// Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d",

View File

@@ -31,6 +31,13 @@ char const * GetDateTimeStr( const char * DateSeparator = "/", const char * Tim
bool ReadTime( const time_t EpochTime, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds );
bool ReadDate( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year );
bool ReadDateTime( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds );
bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime );
bool ReadDateStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year );
bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime );
char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator = "/" );
char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator = ":" );