//-----------------------------------------------------------------------------
///
/// \brief  Functions for handling time information
///
///         ...
///
/// \date   20121107
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


#include <lepto/time.h>
#include <time.h>          // time_t


//---Implementation-----------------------------------------------------------


uint8_t CTime::bin2bcd(uint8_t Value) /* static */
{
  uint8_t bcdhigh = 0;

  while (Value >= 10)
  {
    bcdhigh++;
    Value -= 10;
  }

  return  ((uint8_t)(bcdhigh << 4) | Value);
}


uint8_t CTime::bcd2bin(uint8_t Value) /* static */
{
  uint8_t tmp = 0;
  tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  return (tmp + (Value & (uint8_t)0x0F));
}


timestamp_t CTime::currentTimeStamp() /*static*/
{
   timestamp_t timeStamp;

// Does not work for some reason, results in TRAP
#if defined(STM32F0) || defined(STM32F4) || defined(STM32F3) || defined(STM32F1)
   timeStamp=0;
#else
   time_t rawtime;
   struct tm * timeinfo;

   time ( &rawtime );
   timeinfo = localtime ( &rawtime );

   timeStamp=
         ((((((((((
           (timestamp_t)timeinfo->tm_year) * 100)
         + (timestamp_t)timeinfo->tm_mon ) * 100)
         + (timestamp_t)timeinfo->tm_mday) * 100)
         + (timestamp_t)timeinfo->tm_hour) * 100)
         + (timestamp_t)timeinfo->tm_min ) * 100)
         + (timestamp_t)timeinfo->tm_sec
         ;
#endif
   return(timeStamp);
}

//---fin----------------------------------------------------------------------
