RFC3339 and in UTC/Zulu string to integer C++

321 Views Asked by At

I am writing a C+ application for an embedded ARM device.

My application receives a date and time string in format RFC3339 and in UTC/Zulu. For example "2020-12-14T23:18:13Z"

I need to get the time from this in seconds, compare it to the current time in seconds and take action if there is a difference between those times of greater than 8 seconds.

I've included a third party header file to help, date.h from here : https://howardhinnant.github.io/date/date.html

I have to say i'm still struggling to properly get the seconds from the date and time string as an integer and do comparisons. It is always zero in my case. This is what I have been trying for example so far:

#include <iostream>
#include <string>
#include <sstream>
#include "date.h"

int main()
{
    std::string ts = "2020-11-14T22:14:16Z";  // Literal string to test at first

    std::istringstream infile1{ts};
    sys_seconds tps;
    infile1 >> parse("%FT%T%Ez", tps);

    auto stampSinceEpoch_s = tps.time_since_epoch();

    // Get the current time 
    auto now = system_clock::now();
    auto now_s = time_point_cast<seconds>(now);
    auto nowSinceEpoch_s = now_s.time_since_epoch();


    if ( (nowSinceEpoch_s .count() - stampSinceEpoch_s.count()) > 8)
    {
        // Time difference is greater than 8 seconds, take action
    }
}

Any help is greatly appreciated.

0

There are 0 best solutions below