1

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.

Engineer999
  • 3,683
  • 6
  • 33
  • 71
  • If tps is always being set to zero then it looks like the third party parse is failing. I recommend simplifying it further by just trying to parse the date for instance. Once you're able to get that piece working expand it out. Or move to another tool for parsing the date/time string. – Jeff Spencer Dec 15 '20 at 02:00
  • It would be helpful to know the operating system and compiler you are using. I'm not aware of any C++ standard time string to second functions. However there are such tools available elsewhere. For instance Visual Studio and the Windows API have functions available. Otherwise sscanf plus your own math is the only tool I know to suggest, besides what you have already found. – Jeff Spencer Dec 15 '20 at 02:07
  • @JeffSpencer I am using an ARM compiler for a device running embedded Linux. – Engineer999 Dec 15 '20 at 13:00
  • I suggest adding this information to your question. It may help someone else answer your question. I don't know that environment so can only provide general advice. Which would be to either debug the library you've chosen to use. Or implement your own function using sscanf and building it up slowly. For intance, just pass in the last few seconds and turn that into seconds. Then pass in minutes and seconds and use sscanf to get minutes and seconds into an integer then multiply minutes by 60 and add to seconds. That's probably easier than debugging some elses code. – Jeff Spencer Dec 16 '20 at 01:07
  • @JeffSpencer Thanks for your replies. Yes, the parse function is always resulting in 0.0000 in tps variable, so something is wrong there. I could write my own sure, but I guess I will still need to calculate time since epoch. – Engineer999 Dec 16 '20 at 01:12

0 Answers0