Calculate with time?

Hay guys,

I retrieve a String "Alarmtime" wich looks like this:12:28:01
Where
Hours = 12
Minutes = 28
Seconds = 01

Now I want to minus it with 30 minutes.
So it should be: 11:58:01

How can I do that?
Is there a certain format where you just do time1-time2=time
Or something?

I searched alot, but could not find any solution.

If you are intrested in the whole code (It is obviously an alarmclock, and quite a bunch of code), here it is:

#include <DS3231.h> //RTC Library

String fullDataIn = ""; //Recieved String
String alarmTime = ""; //What time turn alarm on?
int alarmOn = 0; // Is alarm on?


DS3231  rtc(SDA, SCL); //Make new DS3231, on pin SDA and SCL
void setup()
{
  Serial.begin(9600);
  rtc.begin();
}

void loop()
{
  checkSerial();
  if (alarmOn == 0) {
    checkAlarm();
    Serial.println("Current time: " + (String)rtc.getTimeStr() + " -- Alarm Set: " + alarmTime);
  } else
  {
    Serial.println("DATATATA ALARMMM");
  }
  delay (100);
}

bool getSerial()
{
  while (Serial.available() > 0) //While Serial recieved something, check if format is correct. If so, return true.
  {
    char charRecieved = Serial.read();
    if (charRecieved != '#' && charRecieved != '%') {
      fullDataIn += charRecieved;
    }
    if (charRecieved == '%')
    {
      fullDataIn.replace("\n", "");
      fullDataIn.replace("\r", "");
      return true;
    }
  }
}

void checkSerial() {
  if (getSerial() == true)  //If getserial returns true (Serial recieved something in correct format), look if the message starts with a known command, otherwise ignore.
  {
    if (fullDataIn.startsWith("SETALARM_"))
    {
      setAlarm();
    }
    else if (fullDataIn.startsWith("RESETALARM"))
    {
      resetAlarm();
    }
    else if (fullDataIn.startsWith("RESETALL"))
    {
      resetAll();
    }
    else if (fullDataIn.startsWith("SETTIME_"))
    {
      setClockTime();
    }
    else if (fullDataIn.startsWith("GETTIME"))
    {
      getClockTime();
    }
    else if (fullDataIn.startsWith("GETALARM"))
    {
      getAlarmTime();
    } else
    {
      fullDataIn = "";
    }

  }
}
void checkAlarm() {
  String currentTime = (String)rtc.getTimeStr(); // If the current time equals the setted alarmtime Set alarm On.
  if (currentTime == alarmTime) {
    alarmOn = 1;
  }
}

void setAlarm()
{
  alarmTime = ""; //Clear alarmTime so we can rewrite it. (Somehow it does not overwrite the old data?..)
  alarmTime = fullDataIn.substring(9);
  fullDataIn = "";
}

void resetAll()
{
  alarmTime = "";
  fullDataIn = "";
  alarmOn = 0;
}

void resetAlarm()
{
  alarmOn = 0;
  fullDataIn = "";
}

void setClockTime()
{
  int hours = fullDataIn.substring(8, 10).toInt(); //Rip the data appart in hours,minutes and seconds so we can set the time with it.
  int minutes = fullDataIn.substring(11, 13).toInt();
  int seconds = fullDataIn.substring(14, 16).toInt();
  rtc.setTime(hours, minutes, seconds);
  fullDataIn = "";
}

void getClockTime()
{
  Serial.println((String)rtc.getTimeStr()); //Return the time.
  fullDataIn = ""; //Always need the serialstring, after executing.
}

void getAlarmTime()
{
  Serial.println(alarmTime); // Return the alarm time. (if alarmtime is set, otherwise empty return).
  fullDataIn = "";
}

Use the Time library.

aarg:
Use the Time library.

The time library does not provide calculations with the time like:
12:29:00 - 00:30:00 = 11:59:00

as far as I know, otherwise how?

Convert all times to epoch seconds and calculate your differences there - it's much simpler than trying to implement base 12/24/60 arithmetic yourself.

I do all my time calculations in epoch seconds. The Time library has a complete set of conversion functions to get the HMS and DMY values whenever I need them. It makes my life a whole lot easier, especially since I'm inter-operating with millis(), RTC, GPS and NTP time. Time storage, adjustment and comparison is a breeze that way.

A bonus is that the code is more readable when using the defined constants in Time.h.

First of all, if you are trying to do time manipulation as text (either as a String or as a char array), you are going to find it nightmarishly hard.

If I were you, I would work on the time as hours, minutes, and seconds. What library are you using to get the time? Where did you get that library? See if you can find a way to get at the hours, minutes, and seconds individually.

Once you have those three numbers (hours, minutes, and seconds), you can do arithmetic on them to get the time you are looking for.