Strftime stops working

Hi All,

I have a strange one.

I am using getLocalTime(&timeinfo) to obtain the current time within my loop code.

This is working well.

I then need to store this value as part of an array (that later gets converted to JSON).

strftime(tmp, 25, "%Y-%m-%d %H:%M:%S", &timeinfo);
detectionData["detection_time"] = tmp;

What I am seeing is that after about 90 seconds, this value is no longer the time, but is null. Any ideas what might be the issue here?

Here's the code in question:

void loop() {

    char tmp[25];

    if (!getLocalTime(&timeinfo)) {
        Serial.println("Failed to obtain time");
    } else {
        Serial.println(&timeinfo, "%Y-%m-%d %H:%M:%S"); // This always shows a valid time value
        strftime(tmp, 25, "%Y-%m-%d %H:%M:%S", &timeinfo);
        detectionData["detection_time"] = tmp;
        Serial.printf("Current Time: %s\n", detectionData["detection_time"]); // After about 90 secs (consistently) this shows null
    }
}

This won't make sense until you provide some context, at least not to me.

Please post the entire sketch. Among all the questions that might answer is… what exact libraries are you using.

TIA

a7

Thanks @alto777 for the reply.

In the process of pulling out a bit more of a representative section of the code, I noticed that the scope of my "detectionData" array had been moved out of the "loop" function and was global.

I've moved this back inside just the loop function (that's the only place it's used, so no need for it to be global.

This has resolved my issue.

Not entirely sure why it was causing the behaviour described in my initial post, however it has solved my issue.

Thanks for your time.

Nathan

can you explain the above?
(common with associative arrays in other languages, but not C/C++)

It's quite common when using JSON.

Yeah, but explain that syntax. I'm guessing it is some kind of C++ voodoo exploits some feature of C++.

a7

does this compile (under C++) for you?

It would if I had the library installed. Presumably it overloads 'operator[]' . Standard C++ stuff.

what data type would detectionData be?

OP didn't post enough code to be sure. But, presumably a JsonDocument or a class that inherits from it:

class JsonDocument : public VariantOperators<const JsonDocument&> {

detectionData is DynamicJsonDocument

DynamicJsonDocument detectionData(1024);

detectionData["device_id"] = ScannerPreferences::DEVICE_ID;
detectionData["uuid"] = bleScanner.getDetectedtagUUID();
detectionData["major_id"] = bleScanner.getWhitelistedMajorID();
detectionData["minor_id"] = bleScanner.getDetectedtagId();
strftime(tmp, 25, "%Y-%m-%d %H:%M:%S", &timeinfo);
detectionData["detection_time"] = tmp;

serializeJson(detectionData, dataMessage);

dataMessage becomes:

{"device_id":"CONTACT_123","uuid":"eb6d4696-24be-4663-b152-30d46b0e9cc9","major_id":47,"minor_id":1402,"detection_time":"2022-08-24 12:09:50"}

@nathangoss are you starting to see why it's important to show your complete code or (better yet, a small, complete MRE that demonstrates the problem)?

Doing so really cuts down on the amount of unnecessary clutter traffic in your thread.

Imma guess that both @alto777 and @gcjr have done more with C than C++, more closer to the metal so to speak than at high remove from the actual machinery.

I see the point of C++, and I don't mind languages with build in features that are abstractions.

What kills me is the potential for abuse. One could literally make a new kind of integer and overload all the operators, so '+' would divide, '*' would subtract, '==' would test for greater than, and so forth.

Since those things are up to any individual, it can make reading code hard. One would have to poke around in the programmer's implementation or documnets, if s/he even bothered, as opposed to features in rich languages that "everyone" knows, that are 'splained in a slew of textbooks and so forth.

Like the standard template library. I think. Prolly gonna die knowing even less about it than I do this day. :expressionless:

There are a few things I'd like to do that I am certain C++ would cut like a blowtorch to butter, so

THX @gfvalvo, for dragging me one step closer to… the edge.

a7

yes, my experience is this low-level real-time code and haven't had much need to implement nor review higher level capabilities of C++ and am therefore not familiar with them

it's ironic that i'm trying to simulate some code the uses the feature being discussed. this is for accessing private elements in an array for class IPAddress.

        uint8_t& operator[](int index) {
            setV4();
            return *(raw_address() + index);
        }

so it was fortunate (for me) that this thread came along making me aware of the C++ capability and to look for it in IPAddress

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.