eis/RICS/utility.h

91 lines
2.3 KiB
C++
Raw Permalink 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.

#pragma once
/**
* @file utility.h
* @brief 时间工具函数
* @author your name (you@domain.com)
* @version 0.1
* @date 2023-12-22
*
* Copyright: Baosight Co. Ltd.
* DO NOT COPY/USE WITHOUT PERMISSION
*
*/
#include <chrono>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <nlohmann/json.hpp>
#include <sstream>
#include <filesystem> // C++17 文件系统库
namespace fs = std::filesystem; // 简化命名空间
using namespace std::chrono;
using std::chrono::system_clock;
using TimePoint = system_clock::time_point;
using TimeDur = milliseconds;
namespace UtilityTools {
using json = nlohmann::json;
/**
* @brief 时间转当日凌晨时间
* @param time_point My Param doc
* @return TimePoint
*/
TimePoint time_point2zero(TimePoint time_point);
/**
* @brief 时间点 转 时间戳秒 (当日凌晨)
* @param time_point My Param doc
* @return int
*/
int time_point2second_zero(TimePoint time_point);
/**
* @brief 时间戳秒 转 时间点
* @param seconds My Param doc
* @return TimePoint
*/
TimePoint second_zero2time_point(int seconds);
TimePoint millsecond_zero2time_point(int64_t millseconds);
/**
* @brief 时间差 天
* @param stime My Param doc
* @param etime My Param doc
* @return int
*/
int diff_days(TimePoint stime, TimePoint etime);
/**
* @brief str时间转chrono时间
* @param date My Param doc
* @param fmt My Param doc
* @return TimePoint
*/
TimePoint string2TimePoint(std::string date,
const std::string &fmt = "%Y-%m-%d %H:%M:%S");
/**
* @brief 保存json文件
* @param path_ruleid My Param doc
* @param file_name_date My Param doc
* @param content My Param doc
* @param root_dir My Param doc
* @return int
* @return 0--保存成功1--文件已存在;-1--保存异常
*/
int save_json_file(std::string path_ruleid,std::string file_name_date, json content,
std::string root_dir = "/dscdata/RuleNormData");
/**
* @brief json文件读取到 json
* @param file_path My Param doc
* @param sjson My Param doc
* @return true
* @return false
*/
bool jsonReader(std::string file_path, json &sjson);
bool create_path(std::string path);
bool is_file_exists(std::string file_path);
} // namespace UtilityTools