I am using below method to validate Date.
How to format month in string ?
bool CDateTime :: IsValidDate(char* pcDate) //pcDate = 25-Jul-2012 15:08:23
{
bool bVal = true;
int iRet = 0;
struct tm tmNewTime;
iRet = sscanf_s(pcDate, "%d-%d-%d %d:%d:%d", &tmNewTime.tm_mon, &tmNewTime.tm_mday, &tmNewTime.tm_year, &tmNewTime.tm_hour, &tmNewTime.tm_min, &tmNewTime.tm_sec);
if (iRet == -1)
bVal = false;
if (bVal == true)
{
tmNewTime.tm_year -= 1900;
tmNewTime.tm_mon -= 1;
bVal = IsValidTm(&tmNewTime);
}
return bVal;
}
Using strptime:
#include <time.h>
char *str = "25-Jul-2012 15:08:23";
struct tm tm;
if (strptime (str, "%d-%b-%Y %H:%M:%S", &tm) == NULL) {
/* Bad format !! */
}
The C++11 way of doing this is:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
int main()
{
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
std::cout << "Now is " << std::put_time(std::localtime(&now_c), "%d-%b-%Y %H:%M:%S") << '\n';
}
Note: The stream I/O manipulator std::put_time is not implemented fully in all compilers yet. GCC 4.7.1 does not have it for example.
Related
I need to execute multiple methods with specified interval(1 millisecond). Is there any way to call the methods in a loop without sleep because I am not getting 1000 entries in a log file for a second
Currently I have implemented using sleep. Data collection also might take little time.
#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>
#include <csignal>
#include <chrono>
#include <thread>
#include <fstream>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <windows.h>
#include <atomic>
volatile std::sig_atomic_t gStatus{};
void signalHandler(int sig) {
gStatus = sig;
}
void feature1(std::string &output) { output = "feature1"; }
void feature2(std::string &output) { output = "feature2"; }
void feature3(std::string &output) { output = "feature3"; }
void feature4(std::string &output) { output = "feature4"; }
void feature5(std::string &output) { output = "feature5"; }
void feature6(std::string &output) { output = "feature6"; }
void processInfoRequest(std::string logFile, std::unordered_map<std::string, std::function<void(std::string &value)>> methodMap)
{
std::ofstream ofs(logFile);
std::uint64_t prevTime = 0, curTime = 0, diffTime = 0, tmpTime = 0, scheduleTime = 0, pollPeriod = 1;
prevTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
while (gStatus != SIGINT)
{
prevTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
prevTime = prevTime - scheduleTime;
**//get value**
auto timenow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
ofs << ctime(&timenow);
for(auto it : methodMap)
{
std::string output;
it.second(output);
ofs << output << ",";
}
ofs << "\n";
curTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
diffTime = curTime - prevTime;
if (diffTime < pollPeriod)
{
diffTime = pollPeriod - diffTime;
curTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::uint64_t i = 0;
std::uint64_t n = diffTime / 10;
std::uint64_t remainingMs = diffTime % 10;
for (i = 0; i < n; i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
std::this_thread::sleep_for(std::chrono::milliseconds(remainingMs));
tmpTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
scheduleTime = tmpTime - (curTime + diffTime);
}
else
{
scheduleTime = 0;
}
}
ofs.close();
}
int main()
{
std::signal(SIGINT, signalHandler);
std::string output;
std::unordered_map<std::string, std::function<void(std::string &value)>> methodMap = {
{"f1", feature1},
{"f2", feature2},
{"f3", feature3},
{"f4", feature4},
{"f5", feature5},
{"f6", feature6}
};
std::thread th1 = std::thread(processInfoRequest, "threadlog.csv", methodMap);
if (th1.joinable())
th1.join();
return 0;
}
I have char start_time[40] = "2020-04-01 12:00:00"; How can I convert the char array to timestamp in C++ without using strptime?
You can try this:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main(void) {
const char T[] = "2020-04-01 12:00:00";
time_t result = 0;
int year = 0, month = 0, day = 0, hour = 0, min = 0,sec=0;
if (sscanf(T, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &min,&sec) == 6) {
struct tm test_time = {0};
test_time.tm_year = year - 1900; /* years since 1900 */
test_time.tm_mon = month - 1;
test_time.tm_mday = day;
test_time.tm_hour = hour;
test_time.tm_min = min;
test_time.tm_sec = sec;
if ((result = mktime(&test_time)) == (time_t)-1) {
fprintf(stderr, "Cannot convert time to time_t\n");
return EXIT_FAILURE;
}
std::cout << result << '\n' ;
puts(ctime(&result));
struct tm *t_start = localtime(&result);
char date_time[30];
strftime( date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", t_start );
std::cout << date_time << '\n' ;
return EXIT_SUCCESS;
}
else {
fprintf(stderr, "The input was not a valid time format\n");
return EXIT_FAILURE;
}
}
I have coded a program that uses mmap as input to fill a integer 2D vector from a .txt file. The code is part of a larger program and will be submitted to a competition. Is there a way to improve the speed using mmap, or by using a different way all together? Here is the code:
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
// for mmap:
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
const char* map_file(const char* fname, size_t& length);
int main()
{
auto start = std::chrono::high_resolution_clock::now();
size_t length;
auto f = map_file("erasmus.in", length);
auto l = f + length;
int i = 0;
bool flag = false;
string lines;
vector<vector<int> > students(10000); //The number of lines is predefined
const char* temp;
while (f && f!=l) {
string element = "";
temp = static_cast<const char*>(memchr(f, '\n', l-f));
for(f = f; f<=temp; f++)
{
if(!isspace(*f))
{
element += *f;
flag = true;
}
if(isspace(*f) && flag == true)
{
flag = false;
int assigned_element = stoi(element);
students[i].push_back(assigned_element);
element = "";
}
}
i++;
temp++;
}
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed1 = finish - start;
FILE* output = fopen("erasmus.out", "w");
for (int i = 0; i < students.size(); i++)
{
for (int j = 0; j < students[i].size(); j++)
{
fprintf(output, "%d ", students[i][j]);
}
fprintf(output, "\n");
}
std::cout << "Elapsed time: " << elapsed1.count() << " s\n";
return 0;
}
void handle_error(const char* msg) {
perror(msg);
exit(255);
}
const char* map_file(const char* directory, size_t& length)
{
int fileDirectory = open(directory, O_RDONLY);
if (fileDirectory == -1)
handle_error("open");
// obtain file size
struct stat sb;
if (fstat(fileDirectory, &sb) == -1)
handle_error("fstat");
length = sb.st_size;
const char* map = static_cast<const char*>(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fileDirectory, 0u));
if (map == MAP_FAILED)
handle_error("mmap");
return map;
}
The file will be executed on a linux system, if this helps to find the optimal answer. At the end of each line of the .txt
there is a space character (' ') and a newline('\n')
Hello i compliled my c++ program and if i start the .exe i got a error
Image from the error
This is my source(main.cpp):
#include <iostream>
#include <string>
#include <Windows.h>
#include <dos.h>
#include <stdio.h>
#include <fstream>
#include <ctime>
// using
using namespace std;
bool fexists(const char *filename);
int main() {
try {
HANDLE h;
string cClipboard = "";
CreateDirectory("C:\\Program Files\\Clipboard Logger", NULL);
if (!fexists("C:\\Program Files\\Clipboard Logger\\log.txt")) {
FILE *fp;
fp = fopen("C:\\Program Files\\Clipboard Logger\\log.txt", "w");
fclose(fp);
}
while (true) {
if (!OpenClipboard(0)) {
Sleep(2000);
continue;
}
h = GetClipboardData(CF_TEXT);
CloseClipboard();
if ((char *)h == cClipboard) {
Sleep(2000);
continue;
}
cClipboard = (char *)h;
time_t t = time(0);
struct tm * now = localtime(&t);
FILE *fp;
fp = fopen("C:\\Program Files\\Clipboard Logger\\log.txt", "a");
int day = now->tm_mday;
int month = now->tm_mon + 1;
int year = now->tm_year + 1900;
int sec = now->tm_sec;
int min = now->tm_min;
int hour = now->tm_hour;
char logLine[sizeof((char *)h) + 64];
sprintf(logLine, "%d.%d.%d %d.%d.%d %s\n", hour, min, sec, day, month, year, (char *)h);
fprintf(fp, (char *)logLine);
fclose(fp);
cout << (char *)logLine << endl;
Sleep(2000);
}
getchar();
return 0;
} catch (...) {
}
}
bool fexists(const char *filename) {
ifstream ifile(filename);
if (ifile)
return true;
return false;
}
iam new in c++ and i dont know how to fix it, because if i debug the program all works fine but with the exe it doesnt work.
Given:
filesystem::path toDir("./");
ptime oldTime;
ptime now(second_clock::local_time());
How can I determine which files were created in the time period between oldTime and now?
The names of such "fresh" files should be streamed to cout.
Update:
Well based on given answer I made a small programm:
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <boost/timer.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
using namespace boost::filesystem;
using namespace boost::posix_time;
using namespace boost::local_time;
using namespace boost::gregorian;
using namespace std;
path file_service_default_path;
boost::timer timerFame;
int64_t desiredTimeFame;
int64_t spendedTimeFame;
ptime oldTime;
ptime nowTime;
bool first_time;
string file_service_get_dif_path(path base_path, path new_path)
{
path sdiffpath;
path stmppath = new_path;
while(stmppath != base_path) {
sdiffpath = path(stmppath.stem().string() + stmppath.extension().string())/ sdiffpath;
stmppath = stmppath.parent_path();
}
string diff_path =sdiffpath.string();// boost::lexical_cast<string>(sdiffpath);
diff_path = diff_path.substr(0, (diff_path.length()));
std::replace(diff_path.begin(), diff_path.end(), '\\', '/');
return diff_path;
}
void is_file(path p)
{
std::string a = file_service_get_dif_path(file_service_default_path, p);
std::cout << "File: " << a << std::endl;
}
void is_new_file(path p)
{
std::time_t t = boost::filesystem::last_write_time( p );
ptime lastAccessTime = from_time_t( t );
if ( lastAccessTime >= oldTime && lastAccessTime <= nowTime )
{
std::string a = file_service_get_dif_path(file_service_default_path, p);
std::cout << "File: " << a << " is new to us." << std::endl;
}
}
void is_dir(path dir)
{
boost::filesystem::directory_iterator dirIter( dir ), dirIterEnd;
while ( dirIter != dirIterEnd )
{
if ( boost::filesystem::exists( *dirIter ) && !boost::filesystem::is_directory( *dirIter ) )
{
if (first_time)
{
is_file((*dirIter));
}
else
{
is_new_file((*dirIter));
}
}
else
{
is_dir((*dirIter));
}
++dirIter;
}
}
void files_walker()
{
while(true)
{
timerFame.restart();
oldTime = nowTime;
nowTime = second_clock::local_time() ;
file_service_default_path = file_service_default_path;
is_dir(file_service_default_path);
first_time = false;
spendedTimeFame = (int64_t)timerFame.elapsed();
cout << spendedTimeFame << std::endl;
if (spendedTimeFame < desiredTimeFame)
boost::this_thread::sleep(boost::posix_time::milliseconds(desiredTimeFame - spendedTimeFame));
}
}
int main()
{
desiredTimeFame = (int64_t)(5000.0f);
first_time = true;
file_service_default_path = "./new";
boost::thread workerThread(files_walker);
cin.get();
}
But it seems not to show any new files=( how to fix it?
Update 2:
Solved with nowTime = second_clock::universal_time();
Update 3:
fixed code:
#include <sstream>
#include <stdio.h>
#include <time.h>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <boost/timer.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
using namespace boost::filesystem;
using namespace boost::posix_time;
using namespace boost::local_time;
using namespace boost::gregorian;
using namespace std;
path file_service_default_path;
boost::timer timerFame;
int64_t desiredTimeFame;
int64_t spendedTimeFame;
ptime oldTime;
ptime nowTime;
bool first_time;
string file_service_get_dif_path(path base_path, path new_path)
{
path sdiffpath;
path stmppath = new_path;
while(stmppath != base_path) {
sdiffpath = path(stmppath.stem().string() + stmppath.extension().string())/ sdiffpath;
stmppath = stmppath.parent_path();
}
string diff_path =sdiffpath.string();// boost::lexical_cast<string>(sdiffpath);
diff_path = diff_path.substr(0, (diff_path.length()));
std::replace(diff_path.begin(), diff_path.end(), '\\', '/');
return diff_path;
}
void is_file(path p)
{
std::string a = file_service_get_dif_path(file_service_default_path, p);
std::cout << "File: " << a << std::endl;
}
void is_new_file(path p)
{
std::time_t t = boost::filesystem::last_write_time( p );
ptime lastAccessTime = from_time_t( t );
if ( lastAccessTime >= oldTime && lastAccessTime <= nowTime )
{
std::string a = file_service_get_dif_path(file_service_default_path, p);
std::cout << "File: " << a << " is new to us." << std::endl;
}
}
void is_dir(path dir)
{
if(!exists(dir))
{
return;
}
boost::filesystem::directory_iterator dirIter( dir );
boost::filesystem::directory_iterator dirIterEnd;
while ( dirIter != dirIterEnd )
{
if ( boost::filesystem::exists( *dirIter ) && !boost::filesystem::is_directory( *dirIter ) )
{
if (first_time)
{
is_file((*dirIter));
}
else
{
is_new_file((*dirIter));
}
}
else
{
is_dir((*dirIter));
}
++dirIter;
}
}
void files_walker()
{
while(true)
{
timerFame.restart();
oldTime = nowTime;
nowTime = second_clock::universal_time();
is_dir(file_service_default_path);
first_time = false;
spendedTimeFame = (int64_t)timerFame.elapsed();
cout << spendedTimeFame << std::endl;
if (spendedTimeFame < desiredTimeFame)
boost::this_thread::sleep(boost::posix_time::milliseconds(desiredTimeFame - spendedTimeFame));
}
}
int main()
{
desiredTimeFame = (int64_t)(5000.0f);
first_time = true;
file_service_default_path = "./new";
boost::thread workerThread(files_walker);
cin.get();
}
As Emile Cormier stated, you will be unable to get the file creation time in Unix. However, you can still look for 'fresh' files, depending on your definition of 'fresh', by looking at last access time. This is achieved by a call as follows:
boost::filesystem::path p( "somefile" );
std::time_t t = boost::filesystem::last_write_time( p );
boost::ptime lastAccessTime = boost::ptime::from_time_t( t );
So if you then look at the boost::filesystem::directory_iterator you are able to iterate over a directory, i.e. "./" as you state in your question, and compare lastAccessTime given in the example above with the range specified in your question.
So the way I would do this, assuming oldTime has been previously defined, is as follows:
boost::filesystem::path dir( "./" );
boost::ptime nowTime( boost::second_clock::local_time() );
boost::filesystem::directory_iterator dirIter( dir ), dirIterEnd;
while ( dirIter != dirIterEnd )
{
if ( boost::filesystem::exists( *dirIter ) && !boost::filesystem::is_directory( *dirIter ) )
{
std::time_t t = boost::filesystem::last_write_time( *dirIter );
boost::ptime lastAccessTime = boost::ptime::from_time_t( t );
if ( lastAccessTime >= oldTime && lastAccessTime <= nowTime )
{
std::cout << "File meets criteria: " << (*dirIter).filename() << std::endl;
}
}
++dirIter;
}
Hope this helps!
You could refine this by persistently storing the last time you checked the directory and/or the last list of files present in the directory when you last checked, and use this as a point of comparison instead. Without more information as to your intentions I can't provide further guidance.
Relevant docs:
boost::filesystem Reference, boost::ptime Reference