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:
103
DateTimeCore.cpp
103
DateTimeCore.cpp
@@ -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
|
// Get the current date in a string
|
||||||
char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator )
|
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;
|
unsigned char Seconds;
|
||||||
|
|
||||||
// Get Date & Time
|
// Get Date & Time
|
||||||
ReadDate( EpochTime, Day, Month, Year );
|
ReadDateTime( EpochTime, Day, Month, Year, Hours, Minutes, Seconds );
|
||||||
ReadTime( EpochTime, Hours, Minutes, Seconds );
|
|
||||||
|
|
||||||
// Build String
|
// Build String
|
||||||
sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d",
|
sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d",
|
||||||
|
|||||||
@@ -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 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 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 * 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, const char * TimeSeparator = ":" );
|
||||||
|
|||||||
Reference in New Issue
Block a user