Important update:

- DateTimeCore:
  - Bug fix: BuildDateTimeStr() incorrect date
  - New methods: ReadDateTime() convert DateTimeStr -> time_t
This commit is contained in:
Charl Wentzel
2019-07-22 10:59:24 +02:00
parent fb28f86ae9
commit 55168dece2
2 changed files with 27 additions and 2 deletions

View File

@@ -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 // Read components from date/time string
bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime ) 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 ); ReadDate( EpochTime, LocalTime, Day, Month, Year );
// Build String // 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 ); ReadDateTime( EpochTime, LocalTime, Day, Month, Year, Hours, Minutes, Seconds );
// Build String // Build String
return BuildDateTimeStr( Year, Month, Day, Hours, Minutes, Seconds, DateSeparator, TimeSeparator ); return BuildDateTimeStr( Day, Month, Year, Hours, Minutes, Seconds, DateSeparator, TimeSeparator );
} }

View File

@@ -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, 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 ); 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 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 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, bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year,