TRC

Dependents:   RTC_terminal HorlogeSimple

RTC_IUT.cpp

Committer:
gr91
Date:
2020-05-29
Revision:
0:f17b3622fae1
Child:
1:b85409cf9f7b

File content as of revision 0:f17b3622fae1:

/// @file TimeUtilities.cpp contains a real time clock interface for the mbed
///
///

#include "mbed.h"


#define VERSION "1.04"

#include "RTC_IUT.h"
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

Rtc::Rtc()
{
}

Rtc::~Rtc()
{
}

time_t Rtc::GetTimeValue()
{
    return time(NULL);
}

void Rtc::SetTimeValue(time_t t)
{
    set_time(t);
}

void Rtc::GetTimeString(char *buf)
{
    time_t seconds = time(NULL);
    strftime(buf, 32, "%d/%m/%Y %H:%M:%S", localtime(&seconds));
}

void Rtc::SetYear(unsigned short yyyy)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    date->tm_year=yyyy-1900;
    epoch=mktime(date);
    //printf("epoch = %ud \n",epoch);
    set_time(epoch);
}

unsigned short Rtc::GetYear(void)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    return date->tm_year+1900;
}

void Rtc::SetMon(unsigned char y)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    date->tm_mon=y-1;
    epoch=mktime(date);
    //printf("epoch = %ud \n",epoch);
    set_time(epoch);
}

unsigned char Rtc::GetMon(void)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    return date->tm_mon+1;
}

void Rtc::SetDay(unsigned char y)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    date->tm_mday=y;
    epoch=mktime(date);
    //printf("epoch = %ud \n",epoch);
    set_time(epoch);
}

unsigned char Rtc::GetDay(void)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    return date->tm_mday;
}

void Rtc::SetHour(unsigned char y)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    date->tm_hour=y;
    epoch=mktime(date);
    //printf("epoch = %ud \n",epoch);
    set_time(epoch);
}

unsigned char Rtc::GetHour(void)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    return date->tm_hour;
}

void Rtc::SetMin(unsigned char y)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    date->tm_min=y;
    epoch=mktime(date);
    //printf("epoch = %ud \n",epoch);
    set_time(epoch);
}


unsigned char Rtc::GetMin(void)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    return date->tm_min;
}

void Rtc::SetSec(unsigned char y)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    date->tm_sec=y;
    epoch=mktime(date);
    //printf("epoch = %ud \n",epoch);
    set_time(epoch);
}

unsigned char Rtc::GetSec(void)
{
    struct tm *date;
    time_t epoch=time(NULL);
    date=localtime(&epoch);
    return date->tm_sec;
}

// DD/MM/YYYY HH:MM:SS
bool Rtc::SetTimeString(char * timestring)
{
    bool success = false;
    char * p;
    time_t seconds = time(NULL);
    struct tm *t = localtime(&seconds);

    p = strtok(timestring," /");
    if (p != NULL) {
        t->tm_mday = atoi(p) ;
        p = strtok(NULL, " /");
        if (p != NULL) {
            t->tm_mon = atoi(p)-1;
            p = strtok(NULL, " /");
            if (p != NULL) {
                t->tm_year = atoi(p) - 1900;
                p = strtok(NULL, " :");
                if (p != NULL) {
                    t->tm_hour = atoi(p);
                    p = strtok(NULL, " :");
                    if (p != NULL) {
                        t->tm_min = atoi(p);
                        p = strtok(NULL, " (:");
                        if (p != NULL) {
                            t->tm_sec = atoi(p);
                            success = true;         // if we get to here, we're good
                            seconds = mktime(t);
                            //seconds = seconds ;
                            //printf("ss%u \n",seconds);
                            set_time(seconds);

                        }
                    }
                }
            }
        }
    }
    return success;
}