Trevor Hackett / Mbed OS get_time_EQ

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers timeHelp.h Source File

timeHelp.h

00001 #ifndef TIMEHELP_H
00002 #define TIMEHELP_H
00003 
00004 #include <stdlib.h>
00005 #include <string>
00006 
00007 // return true is seconds is even, false if not
00008 // currTime is in the form HH:MM:SS.s
00009 bool secondsAreEven(char currTime[]) {
00010     char seconds[3];
00011     seconds[0] = currTime[6];
00012     seconds[1] = currTime[7];
00013     seconds[2] = 0;
00014     
00015     int secondsInt = atoi(seconds);
00016     
00017     return (secondsInt % 2) == 0;
00018 }
00019 
00020 // given a starting time (starttime) in the form HH:MM:SS.s
00021 // and a number of seconds (secsToAdd), add the seconds to that
00022 // time and put the resulting string into resultTime ... both
00023 // startTime and resultTime must be character arrays of length 10
00024 void addSecondsToCurrentTime(char startTime[], char resultTime[], float secsToAdd) {
00025 
00026     int decsToAdd=0,newSecsToAdd=0,minutesToAdd=0,hoursToAdd=0;
00027     
00028     // if there are enough seconds to add, that an hour
00029     // if being added, then do that and subtract what you added
00030     if ((int)secsToAdd / 3600 > 0) {
00031         hoursToAdd = (int)(secsToAdd / 3600);
00032         secsToAdd -= hoursToAdd * 3600;
00033     }
00034     
00035     // there may be minutes to add if there are enough seconds
00036     if ((int)secsToAdd / 60 > 0) {
00037         minutesToAdd = (int)(secsToAdd / 60);
00038         secsToAdd -= minutesToAdd * 60;   
00039     }
00040     
00041     if ((int)secsToAdd > 0) {
00042         newSecsToAdd = (int)secsToAdd;
00043         secsToAdd -= newSecsToAdd;
00044     }
00045     
00046     decsToAdd = (int)(secsToAdd * 10);
00047 
00048     // now we have to add them to currTime one at a time
00049     // becausse if any of the currTime slots become more
00050     // than 10/60/60 then we have to add them to the next
00051     // highest interval
00052     
00053     // decimal
00054     char prevDecimal[2];
00055     prevDecimal[1] = 0;
00056     prevDecimal[0] = startTime[9];
00057     int newDecimal = atoi(prevDecimal) + decsToAdd;
00058     
00059     if (newDecimal >= 10) {
00060         newSecsToAdd++;
00061         newDecimal = newDecimal % 10;
00062     }
00063     
00064     // seconds
00065     char prevSeconds[3];
00066     prevSeconds[0] = startTime[6];
00067     prevSeconds[1] = startTime[7];
00068     prevSeconds[2] = 0;
00069     int newSeconds = atoi(prevSeconds) + newSecsToAdd;
00070     
00071     if (newSeconds >= 60) {
00072         minutesToAdd++;
00073         newSeconds = newSeconds % 60;
00074     }
00075 
00076     // minutes
00077     char prevMinutes[3];
00078     prevMinutes[0] = startTime[3];
00079     prevMinutes[1] = startTime[4];
00080     prevMinutes[2] = 0;
00081     int newMinutes = atoi(prevMinutes) + minutesToAdd;
00082     
00083     if (newMinutes >= 60) {
00084         hoursToAdd++;
00085         newMinutes = newMinutes % 60;
00086     }
00087     
00088     // hours
00089     char prevHours[3];
00090     prevHours[2] = 0;
00091     prevHours[0] = startTime[0];
00092     prevHours[1] = startTime[1];
00093     int newHours = atoi(prevHours) + hoursToAdd;
00094     newHours = newHours % 12;
00095     
00096     // no 00 o'clock, only 1 through 12
00097     if (newHours == 0) {
00098         newHours = 12;
00099     }
00100     
00101     // now we have newDecimal, newSeconds, newMinutes, and newHours
00102     // so we need to build up the string again
00103     sprintf(resultTime, "%02d:%02d:%02d.%d",newHours,newMinutes,newSeconds,newDecimal);
00104 }
00105 
00106 // given the current time in the form HH:MM:SS.S
00107 // give me back the number of seconds from 00:00:00.0
00108 // at that time
00109 float currTimeSecs(char currentTime[]) {
00110     // hours
00111     char hours[3];
00112     hours[2] = 0;
00113     hours[0] = currentTime[0];
00114     hours[1] = currentTime[1];
00115     
00116     // minutes
00117     char minutes[3];
00118     minutes[0] = currentTime[3];
00119     minutes[1] = currentTime[4];
00120     minutes[2] = 0;
00121     
00122     // seconds
00123     char seconds[3];
00124     seconds[0] = currentTime[6];
00125     seconds[1] = currentTime[7];
00126     seconds[2] = 0;
00127     
00128     // decimal
00129     char decimal[2];
00130     decimal[1] = 0;
00131     decimal[0] = currentTime[9];
00132     
00133     return (atoi(hours)%12)*360.0 + (atoi(minutes)%60)*60.0 + (atoi(seconds)%60)*1.0 + (atoi(decimal)%10)*.1;
00134 }
00135 
00136 #endif