diff --git a/DateTimeCore.cpp b/DateTimeCore.cpp index 628401e..7370793 100644 --- a/DateTimeCore.cpp +++ b/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 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", diff --git a/DateTimeCore.h b/DateTimeCore.h index dea4957..7b3c198 100644 --- a/DateTimeCore.h +++ b/DateTimeCore.h @@ -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 = ":" );