Files
HPPA/HPPA/utility_tc.cpp
2026-03-03 17:22:03 +08:00

178 lines
5.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "utility_tc.h"
#include <QDir>
QString formatTimeStr(char * format)
{
//获取系统时间
time_t timer;//time_t就是long int 类型
struct tm *tblock;
timer = time(NULL);//返回秒数(精度为秒)从1970-1-1,00:00:00 可以当成整型输出或用于其它函数
tblock = localtime(&timer);
//printf("Local time is: %s\n", asctime(tblock));
//格式化时间为需要的格式
char timeStr_tmp[256] = { 0 };
strftime(timeStr_tmp, sizeof(timeStr_tmp), format, tblock);//
QString timeStr2(timeStr_tmp);
// qDebug() << "time is:" << timeStr2;
return timeStr2;
}
QString getFileNameBaseOnTime()
{
using namespace std;
//获取系统时间
time_t timer;//time_t就是long int 类型
struct tm *tblock;
timer = time(NULL);//返回秒数(精度为秒)从1970-1-1,00:00:00 可以当成整型输出或用于其它函数
tblock = localtime(&timer);
//printf("Local time is: %s\n", asctime(tblock));
//格式化时间为需要的格式
char fileNameTmp[256] = { 0 };
char dirNameTmp[256] = { 0 };
strftime(fileNameTmp, sizeof(fileNameTmp), "%Y%m%d_%H%M%S", tblock);
strftime(dirNameTmp, sizeof(dirNameTmp), "%Y%m%d", tblock);
QString fileName(fileNameTmp);
QString dirName(dirNameTmp);
// QString fileName=QString::number(tblock->tm_year+1900)+QString::number(tblock->tm_mon+1)+QString::number(tblock->tm_mday)+"_"+QString::number(tblock->tm_hour)+QString::number(tblock->tm_min);//
//设置文件绝对路径
QDir path("/media/nvme");
if(!path.exists(dirName))
{
path.mkdir(dirName);
}
path.cd(dirName);
QString absoluteFilePath=path.path()+"/"+fileName;
// printf("absoluteFilePath is: %s\n", absoluteFilePath.c_str());
// qDebug() << "absoluteFilePath is:" << absoluteFilePath;
return absoluteFilePath;
}
void bubbleSort(unsigned short * a, int n)
{
// //排序实现1
// for(int i=0;i<numberOfPixel;i++)
// for(int j=1;j<numberOfPixel-i;j++)
// if(a[j-1]>a[j])
// swap(a+j-1,a+j);
//排序实现2
int j,k,flag;
flag = n;
while(flag>0)
{
k=flag;
flag=0;
for(j=1;j<k;j++)
if(a[j-1]>a[j])
{
swap(a+j-1,a+j);
flag=j;
}
}
}
void swap(unsigned short * a, unsigned short * b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
bool createDir(QString fullPath)
{
QDir dir(fullPath);
if (dir.exists()) {
return true;
} else {
bool ok = dir.mkpath(fullPath);//创建多级目录
return ok;
}
}
std::string removeFileExtension(std::string filename)
{
size_t lastDot = filename.find_last_of(".");
if (lastDot == std::string::npos) {
// 如果没有找到后缀,返回原字符串
return filename;
}
return filename.substr(0, lastDot);
}
// 从绝对路径中提取文件名(不包含扩展名)
std::string getFileNameFromPath(const std::string &fullPath)
{
// 找到最后一个路径分隔符,支持 '/' 和 '\\'
size_t lastSlash = fullPath.find_last_of("/\\");
size_t start = (lastSlash == std::string::npos) ? 0 : lastSlash + 1;
// 找到最后一个点,确保点在文件名范围内
size_t lastDot = fullPath.find_last_of('.');
if (lastDot == std::string::npos || lastDot < start) {
// 没有扩展名或点在路径之前,直接返回从 start 到结尾的子串
return fullPath.substr(start);
}
// 返回从 start 到 lastDot 之间的文件名(不含扩展名)
return fullPath.substr(start, lastDot - start);
}
QList<QString> getFileInfo(QString file)
{
QFileInfo fileInfo = QFileInfo(file);
QString fileName, fileSuffix, filePath;
filePath = fileInfo.absolutePath();//绝对路径
fileName = fileInfo.fileName();//文件名
fileSuffix = fileInfo.suffix();//文件后缀
// qDebug() << fileName <<endl
// << fileSuffix<< endl
// << filePath<< endl;
QList<QString> result;
result.append(filePath);
result.append(fileName);
result.append(fileSuffix);
return result;
}
long long getNanosecondsSinceMidnight()
{
// 获取当前系统时间
auto now = std::chrono::system_clock::now();
// 转换为 time_t秒级别的时间戳
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm local_tm = *std::localtime(&t); // 获取本地时间结构
// 将时间结构调整到当天 00:00:00
local_tm.tm_hour = 0;
local_tm.tm_min = 0;
local_tm.tm_sec = 0;
local_tm.tm_isdst = -1; // 让 mktime 自动判断夏令时
// 获取当天 00:00:00 的时间点
auto midnight = std::chrono::system_clock::from_time_t(std::mktime(&local_tm));
// 计算从当天 00:00:00 到现在的时间差
auto nanoseconds_since_midnight = std::chrono::duration_cast<std::chrono::nanoseconds>(now - midnight);
// 返回纳秒数
return nanoseconds_since_midnight.count();
}