Home > Blockchain >  Getting Current Date inside a C process running 24*7
Getting Current Date inside a C process running 24*7

Time:01-03

I have a backend process running 24*7 mostly built using C and I need to validate if an input date (in format YYYYMMDD) belongs in a set of next 5 business days. The input date is not a clear indicator of the current date so I am using the following function to get the current date and then calculating the next 5 business days from it.

const std::string& CurrentDateStr() {
        static const std::string sDate = []() {
            time_t currTime = time(NULL);
            struct tm timeinfo;
            localtime_r(&currTime, &timeinfo);
            char buffer[16]="";
            strftime(buffer, sizeof(buffer), "%Y%m%d", &timeinfo);
            return std::string(buffer);
        } ();
        return sDate;
    }

This function returns me the correct current date if the process was started today but if the process continues running till tomorrow then it will return me yesterday's date as current date due to which calculation of next 5 business days from current date goes for a toss.

Is this expected ? Is there some workaround for it or is there a better way to implement the requirement using standard C

CodePudding user response:

Your issue is the static variable. You should read up on that, because you're going to encounter it a lot. This is what the comments were trying to get you to do. You can fix your issue by just removing it:

const std::string& CurrentDateStr() {
            time_t currTime = time(NULL);
            struct tm timeinfo;
            localtime_r(&currTime, &timeinfo);
            char buffer[16]="";
            strftime(buffer, sizeof(buffer), "%Y%m%d", &timeinfo);
            return std::string(buffer);
    }

For a more modern solution, as suggested in the comments as well, read up on chrono. Especially system_clock::now().

CodePudding user response:

one way to do it using chrono:

#include <iostream>
#include <ctime>
#include <chrono>
#include <thread>

int main()
{
    while (true)
    {
        theTime currentTime = time(nullptr);
        tm* date = gmtime(&currentTime);
        // Print the date and time
        std::cout << "Current date and time: " << date->theDay << "/" << date->theMon   1 << "/" << date->theYear   1900;
        std::cout << " " << date->theHour << ":" << date->theMmin << ":" << date->theSec << std::endl;
        // Wait for 1 minute
        std::this_thread::sleep_for(std::chrono::minutes(1));
    }
}

OR Use the sleep method.

#include <iostream>
#include <ctime>
#include <unistd.h>

int main()
{
    while (true)
    {
        time_t currentTime = time(nullptr);
        tm* date = gmtime(&currentTime);
        std::cout << "Current date and time: " << date->tm_mday << "/" << date->tm_mon   1 << "/" << date->tm_year   1900;
        std::cout << " " << date->tm_hour << ":" << date->tm_min << std::endl;
        // Wait for 1 minute (60 seconds)
        sleep(60);
    }
}
  • Related