Files
redAcore/DateTimeCore.cpp
Charl Wentzel 2f81d1fbbe Important Update:
- Library Clean up:
  - Removed all unused C/C++ libraries from source
  - First C/C++ libraries then redA libraries
- Library changes:
  - renamed BufferCore -> CharBufferCore
  - added ItemBufferCore
- CharBufferCore:
  - Derive RollingBuffer & ShiftBuffer from common class CharBuffer
  - CharBuffer is mostly a virtual class (interface)
2017-07-22 17:46:05 +02:00

174 lines
4.4 KiB
C++

/*
* DateTimeCore.cpp
*
* Created on: 3 March 2017
* Author: wentzelc
*/
// Standard C/C++ Libraries
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
// redA Libraries
#include "DateTimeCore.h"
//---------------------------------------------------------------------------
// Variable used to temp values with
static 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);
}
//---------------------------------------------------------------------------