📄 util.cpp
字号:
#include <cstddef> // For size_t#include "util.hpp"int sign(const int& x) { if(x > 0) { return 1; } else if(x < 0) { return -1; } return 0;}time_t mkDate(const char* dt, const char* format) { struct tm time_tm; memset(&time_tm, '\0', sizeof(struct tm)); strptime(dt,format,&time_tm); return mktime(&time_tm);}char *time_convert(const double& x) { time_t x_timet = static_cast<time_t>(x); return ctime(&x_timet);}void printDate(const time_t& x, const char *fmt) { struct tm x_struct_tm; char ans[30]; localtime_r(&x, &x_struct_tm); strftime(ans,30,fmt,&x_struct_tm); //printf("%s",ans); cout << ans;}string date2string(const time_t& t, const char *fmt) { struct tm t_tm; char ans[30]; localtime_r(&t,&t_tm); strftime(ans,30,fmt,&t_tm); return string(ans);}struct tm double2tm(const double& x) { struct tm x_struct_tm; const time_t x_timet = static_cast<time_t>(x); localtime_r(&x_timet,&x_struct_tm); return x_struct_tm;}time_t make_POSIXct(const int& year, const int& month, const int& day, const int& hour, const int& minute, const int& second) { struct tm localtime_tm; // init struct memset(&localtime_tm, '\0', sizeof(struct tm)); // for years, 1970 is 70 localtime_tm.tm_year = year - 1900; // months are 0 to 11 localtime_tm.tm_mon = month - 1; // day of month is 1 to 31 localtime_tm.tm_mday = day; localtime_tm.tm_hour = hour; localtime_tm.tm_min = minute; localtime_tm.tm_sec = second; // have to do this so mktime will fix the dst field // do not remove this (it works) localtime_tm.tm_isdst = -1; // convert to POSIXct return mktime(&localtime_tm);}time_t string2POSIXct(const string& dt, const char *format) { struct tm tm; //cout << dt.c_str() << endl; memset(reinterpret_cast<char*>(&tm),'\0',sizeof(struct tm)); strptime(dt.c_str(),format,&tm); tm.tm_isdst = -1; //time_t x = mktime(&dt_convert); //printf("%s\n",ctime(&x)); //cout << x << endl; return mktime(&tm);} int match_string(const string& s, const vector<string>& strings) { int matchcol = -1; for(unsigned int i=0; i < strings.size(); i++) { if(strings[i]==s) { matchcol = (int)i; } } return matchcol;}unsigned int countCommas(const string& s) { unsigned int count = 0; const string comma(","); size_t i = s.find(comma,0); while(i != string::npos) { count++; i = s.find(comma,++i); } return count;}vector<string> splitString(const string& s, const char& delim) { vector<string> ans; size_t endpos = s.find(delim,0); // if the delimiter is not found // then return the entire string if(endpos==string::npos) { ans.push_back(s); return ans; } else { size_t startpos = 0; while(endpos != string::npos) { ans.push_back(s.substr(startpos,endpos-startpos)); startpos=endpos+1; endpos = s.find(delim,startpos); } // now copy from last delim to end of string unsigned int last_delim = s.rfind(delim,s.size()); if(last_delim < s.size()) ans.push_back(s.substr(last_delim+1)); return ans; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -