diff --git a/DateTimeCore.cpp b/DateTimeCore.cpp index f49df29..628401e 100644 --- a/DateTimeCore.cpp +++ b/DateTimeCore.cpp @@ -171,3 +171,92 @@ char const * GetDateTimeStr( const char * DateSeparator, const char * TimeSepar } //--------------------------------------------------------------------------- +// Get current time from real-time clock +bool ReadTime( const time_t EpochTime, 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; + + return true; +} +//--------------------------------------------------------------------------- + +bool ReadDate( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year ) +{ + struct tm CurrentDate; + + // Get current date and time + localtime_r( &EpochTime, &CurrentDate ); + + // Extract date + Day = CurrentDate.tm_mday; + Month = CurrentDate.tm_mon + 1; + Year = CurrentDate.tm_year + 1900; + + return true; +} +//--------------------------------------------------------------------------- + +// Get the current date in a string +char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator ) +{ + unsigned char Day; + unsigned char Month; + unsigned int Year; + + // Get Date + ReadDate( EpochTime, Day, Month, Year ); + + // Build String + sprintf( ReturnStr, "%04d%s%02d%s%02d", + Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day ); + + // Return value + return (ReturnStr); +} +//--------------------------------------------------------------------------- + +char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator ) +{ + unsigned char Hours; + unsigned char Minutes; + unsigned char Seconds; + + // Get Date & Time + ReadTime( EpochTime, Hours, Minutes, Seconds ); + + // Build String + sprintf( ReturnStr, "%02d%s%02d%s%02d", + Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds ); + + return (ReturnStr); +} +//--------------------------------------------------------------------------- + +char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparator, const char * TimeSeparator ) +{ + unsigned char Day; + unsigned char Month; + unsigned int Year; + unsigned char Hours; + unsigned char Minutes; + unsigned char Seconds; + + // Get Date & Time + ReadDate( EpochTime, Day, Month, Year ); + ReadTime( EpochTime, Hours, Minutes, Seconds ); + + // Build String + sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d", + Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day, + Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds ); + + return (ReturnStr); +} diff --git a/DateTimeCore.h b/DateTimeCore.h index 6d0b99c..dea4957 100644 --- a/DateTimeCore.h +++ b/DateTimeCore.h @@ -29,4 +29,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 ); + +char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator = "/" ); +char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator = ":" ); +char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparator = "/", const char * TimeSeparator = ":" ); + +//--------------------------------------------------------------------------- + #endif /* REDACORE_DATETIMECORE_H_ */ diff --git a/JSONparseCore.cpp b/JSONparseCore.cpp index e38081d..5bcd52f 100644 --- a/JSONparseCore.cpp +++ b/JSONparseCore.cpp @@ -100,7 +100,7 @@ bool CJSONparse::WriteToFile( const char * BasePath, const char * FilePath, cons } // Open file - if ((Handle = open( FilePath, O_CREAT|O_WRONLY|O_TRUNC, 660 )) < 0) { + if ((Handle = open( FilePath, O_CREAT|O_WRONLY|O_TRUNC, 0660 )) < 0) { Error = true; sprintf( ErrorText, "Could not open file" ); return false;