Contains necessary classes and functions for ELEC351

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dateAndTime.cpp Source File

dateAndTime.cpp

00001 #include "mbed.h"
00002 #include "dateAndTime.hpp"
00003 
00004 TDS_DT C_DT::getDT()            // Returns TDS_DT Date & Time format
00005 {
00006     time_t secondsElapsed;      // Object of type time_t
00007     struct tm *tempTimeInfo;    // Pointer to a embeded structure containing calendar dates
00008 
00009     time(&secondsElapsed);      // Get time is seconds since epoch. Also hope people are ready for UNIX milenium bug
00010     tempTimeInfo = localtime (&secondsElapsed); // Convert current epoch time to calendar time acounting for timezones & daylight saving
00011 
00012     // Reasign date components to custom data type
00013     this-> date_time.day = tempTimeInfo->tm_mday;
00014     this-> date_time.mnt = 1+tempTimeInfo->tm_mon;      // Months indexed at 1
00015     this-> date_time.yr = 1900 + tempTimeInfo->tm_year; // Years since epoch
00016 
00017     // Reasign time components to custom data type
00018     this-> date_time.sec = tempTimeInfo->tm_sec;
00019     this-> date_time.min = tempTimeInfo->tm_min;
00020     this-> date_time.hr = tempTimeInfo->tm_hour;
00021 
00022     return date_time;   // Return custom data type containing Date & Time
00023 }
00024 
00025 
00026 void C_DT::setD(int day, int mnt, int yr)    // Updates RTC date
00027 {
00028     time_t secondsElapsed;      // Object of type time_t
00029     struct tm *tempTimeInfo;    // Pointer to a embeded structure containing calendar dates
00030 
00031     time(&secondsElapsed);      // Get time is seconds since epoch.
00032     tempTimeInfo = localtime (&secondsElapsed); // Convert current epoch time to calendar time acounting for timezones & daylight saving
00033 
00034     tempTimeInfo->tm_mday = day;            // Update day
00035     tempTimeInfo->tm_mon  = mnt - 1;        // Update month / Indexed at 0
00036     tempTimeInfo->tm_year = yr - 1900 ;     // Update year  / Years since epoch
00037 
00038     secondsElapsed = mktime(tempTimeInfo);  // Convert to epoch time
00039     set_time(secondsElapsed);               // Update RTC
00040 }
00041 
00042 
00043 void C_DT::setT(int hr, int min, int sec)   // Updates RTC time
00044 {
00045     time_t secondsElapsed;      // Object of type time_t
00046     struct tm *tempTimeInfo;    // Pointer to a embeded structure containing calendar dates
00047 
00048     time(&secondsElapsed);      // Get time is seconds since epoch.
00049     tempTimeInfo = localtime (&secondsElapsed); // Convert current epoch time to calendar time acounting for timezones & daylight saving
00050 
00051     tempTimeInfo->tm_sec = sec; // Update seconds
00052     tempTimeInfo->tm_min = min; // Update minutes
00053     tempTimeInfo->tm_hour = hr; // Update hours
00054 
00055     secondsElapsed = mktime(tempTimeInfo);  // Convert to epoch time
00056     set_time(secondsElapsed);               // Update RTC
00057 }