Important update:

- Add new DateTimeCore file
- Provides functions to get/set system date
- Create Date, Time and Date/Time string with custom separators
This commit is contained in:
Charl Wentzel
2017-03-03 18:00:44 +02:00
parent 47577e9f4c
commit 02b43fe6b8
3 changed files with 210 additions and 1 deletions

View File

@@ -1,3 +1,3 @@
PROJECT(lib_redAcore)
ADD_LIBRARY(redAcore TimingCore.cpp LogCore.cpp SignalCore.cpp BufferCore.cpp FunctionCore.cpp DeviceCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp)
ADD_LIBRARY(redAcore TimingCore.cpp DateTimeCore.cpp LogCore.cpp SignalCore.cpp BufferCore.cpp FunctionCore.cpp DeviceCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp)

177
DateTimeCore.cpp Normal file
View File

@@ -0,0 +1,177 @@
/*
* DateTimeCore.cpp
*
* Created on: 3 Mar 2017
* Author: wentzelc
*/
// redA Libraries
#include "DateTimeCore.h"
// Standard C/C++ Libraries
#include <sys/time.h>
#include <stdio.h>
#include <cstdlib>
#include <string.h>
//#include <stdlib.h>
//#include <unistd.h>
//---------------------------------------------------------------------------
// Variable used to temp values with
char ReturnStr[20];
//---------------------------------------------------------------------------
// Set current time on real-time clock
bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds )
{
struct tm NewTime;
struct timeval tv;
struct timezone tz;
// Get current date and time
gettimeofday( &tv, &tz);
// Change to new time
localtime_r( &tv.tv_sec, &NewTime );
NewTime.tm_hour = Hours;
NewTime.tm_min = Minutes;
NewTime.tm_sec = Seconds;
// Convert back
tv.tv_sec = mktime( &NewTime );
tv.tv_usec = 0;
// Set date and new time
settimeofday( &tv, &tz);
return true;
}
//---------------------------------------------------------------------------
// Get current time from real-time clock
bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds )
{
struct tm CurrentTime;
time_t UTC;
// Get current date and time
time( &UTC );
localtime_r( &UTC, &CurrentTime );
// Extract time
Hours = CurrentTime.tm_hour;
Minutes = CurrentTime.tm_min;
Seconds = CurrentTime.tm_sec;
return true;
}
//---------------------------------------------------------------------------
// Set current date on real-time clock
bool SetDate( unsigned char Day, unsigned char Month, unsigned Year )
{
struct tm NewDate;
struct timeval tv;
struct timezone tz;
// Get current date and time
gettimeofday( &tv, &tz );
// Change to new time
localtime_r( &tv.tv_sec, &NewDate );
NewDate.tm_year = Year - 1900;
NewDate.tm_mon = Month - 1;
NewDate.tm_mday = Day;
// Convert back
tv.tv_sec = mktime( &NewDate );
tv.tv_usec = 0;
// Set date and new time
settimeofday( &tv, &tz);
return true;
}
//---------------------------------------------------------------------------
// Get current data from real-time clock
bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year )
{
struct tm CurrentDate;
time_t UTC;
// Get current date and time
time( &UTC );
localtime_r( &UTC, &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 * GetDateStr( const char * DateSeparator )
{
unsigned char Day;
unsigned char Month;
unsigned int Year;
// Get Date
GetDate( 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 * GetTimeStr( const char * TimeSeparator )
{
unsigned char Hours;
unsigned char Minutes;
unsigned char Seconds;
// Get Date & Time
GetTime( Hours, Minutes, Seconds );
// Build String
sprintf( ReturnStr, "%02d%s%02d%s%02d",
Hours, ((TimeSeparator)? TimeSeparator : ":"), Minutes, ((TimeSeparator)? TimeSeparator : ":"), Seconds );
return (ReturnStr);
}
//---------------------------------------------------------------------------
char const * GetDateTimeStr( 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
GetDate( Day, Month, Year );
GetTime( 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);
}
//---------------------------------------------------------------------------

32
DateTimeCore.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* DateTimeCore.h
*
* Created on: 3 Mar 2017
* Author: wentzelc
*/
#ifndef REDACORE_DATETIMECORE_H_
#define REDACORE_DATETIMECORE_H_
// redA Libraries
/* none */
// Standard C/C++ Libraries
#include <time.h>
//---------------------------------------------------------------------------
// Get and set System Date and Time
bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds );
bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds );
bool SetDate( unsigned char Day, unsigned char Month, unsigned Year );
bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year );
char const * GetDateStr( const char * DateSeparator = NULL );
char const * GetTimeStr( const char * TimeSeparator = NULL );
char const * GetDateTimeStr( const char * DateSeparator = NULL, const char * TimeSeparator = NULL );
//---------------------------------------------------------------------------
#endif /* REDACORE_DATETIMECORE_H_ */