Mbed Clock application using an NTP connection to get internet time and a terminal interface to send commands

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient mbed-rtos mbed SDFileSystem wavfile

Clock.cpp

Committer:
dudanian
Date:
2014-12-02
Revision:
0:4e6ae21cbd31
Child:
2:c939d0501184

File content as of revision 0:4e6ae21cbd31:

#include "Clock.h"

bool Clock::isSet = false;

Clock::Clock() : timezone(UTC) {
    if (!isSet) {
        isSet = true;
        set_time(1388534400);
    }
}

/**
 * Sets the time and stores it in UTC time
 */
void Clock::setTime(int hour, int minute, int period) {
    time_t rawtime = time(NULL);
    struct tm *timeinfo = localtime(&rawtime);
    if (hour == 12)
        hour = 0;
    timeinfo->tm_hour = (((period == AM) ? hour : (hour + 12)) - timezone) % 24;
    timeinfo->tm_min = minute;
    timeinfo->tm_sec = 0;
    set_time(mktime(timeinfo));
}

/**
 * Sets the timezone. Since the time is stored in UTC, the system time 
 * is not modified
 */
void Clock::setTimezone(int timezone) {
    
    this->timezone = timezone;
}

/**
 * Uses an NTP Client to set the time to UTC
 */
int Clock::syncTime() {
    NTPClient ntp;
    return ntp.setTime("0.pool.ntp.org");
}

/**
 * Gets the system time in UTC and converts it according to the given timezone
 */
time_t Clock::getTime() {
    time_t rawtime = time(NULL);
    struct tm *timeinfo = localtime(&rawtime);
    timeinfo->tm_hour = (timeinfo->tm_hour + timezone) % 24;
    return mktime(timeinfo);
}

int Clock::getTimezone() {
    return timezone;
}