From 55168dece29bf6ecee2e27d67d2744ab119f632e Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Mon, 22 Jul 2019 10:59:24 +0200 Subject: [PATCH] Important update: - DateTimeCore: - Bug fix: BuildDateTimeStr() incorrect date - New methods: ReadDateTime() convert DateTimeStr -> time_t --- DateTimeCore.cpp | 27 +++++++++++++++++++++++++-- DateTimeCore.h | 2 ++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/DateTimeCore.cpp b/DateTimeCore.cpp index ee3cf3b..1aa388f 100644 --- a/DateTimeCore.cpp +++ b/DateTimeCore.cpp @@ -293,6 +293,29 @@ bool ReadDateTime( const time_t EpochTime, bool LocalTime, unsigned char &Day, u } //--------------------------------------------------------------------------- +bool ReadDateTime( const char * DateTimeStr, bool LocalTime, time_t &EpochTime ) +{ + int ItemsAssigned; + struct tm NewTime; + + // Read time + ItemsAssigned = sscanf( DateTimeStr, "%d%*[/-]%d%*[/-]%d%*[ ]%d%*[:]%d%*[:]%d", + &(NewTime.tm_year), &(NewTime.tm_mon), &(NewTime.tm_mday), + &(NewTime.tm_hour), &(NewTime.tm_min), &(NewTime.tm_sec) ); + if (ItemsAssigned != 6) + return false; + + // Update time + NewTime.tm_year -= 1900; + NewTime.tm_mon -= 1; + NewTime.tm_zone = "UTC"; + + // Convert to Epoch Time + EpochTime = mktime( &NewTime ) + ((LocalTime)? 0 : NewTime.tm_gmtoff); + return true; +} +//--------------------------------------------------------------------------- + // Read components from date/time string bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime ) { @@ -416,7 +439,7 @@ char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char * ReadDate( EpochTime, LocalTime, Day, Month, Year ); // Build String - return BuildTimeStr( Year, Month, Day, DateSeparator ); + return BuildDateStr( Day, Month, Year, DateSeparator ); } //--------------------------------------------------------------------------- @@ -447,5 +470,5 @@ char const * BuildDateTimeStr( const time_t EpochTime, bool LocalTime, const cha ReadDateTime( EpochTime, LocalTime, Day, Month, Year, Hours, Minutes, Seconds ); // Build String - return BuildDateTimeStr( Year, Month, Day, Hours, Minutes, Seconds, DateSeparator, TimeSeparator ); + return BuildDateTimeStr( Day, Month, Year, Hours, Minutes, Seconds, DateSeparator, TimeSeparator ); } diff --git a/DateTimeCore.h b/DateTimeCore.h index b734c40..1fe92ff 100644 --- a/DateTimeCore.h +++ b/DateTimeCore.h @@ -39,6 +39,8 @@ bool ReadDate( const time_t EpochTime, bool LocalTime, unsigned char &Day, un 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 ); +bool ReadDateTime( const char * DateTimeStr, bool LocalTime, time_t &EpochTime ); + bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime = false ); 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,