Skip to content

Commit

Permalink
fix: date clone
Browse files Browse the repository at this point in the history
  • Loading branch information
kekxv committed Jun 16, 2024
1 parent 4ab3ca6 commit 9915d6c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
22 changes: 16 additions & 6 deletions src/date/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ std::shared_ptr<date> date::parseMicroseconds(const std::time_t &time_) {
return parse(time_ / 1000);
}

std::shared_ptr<date> date::clone(const char *format_string) const {
const auto t = format(format_string);
auto format_string_ = std::regex_replace(format_string, std::regex{"--"}, "||");
format_string_ = std::regex_replace(format_string_, std::regex{"[+-]\\d+"}, "");
format_string_ = std::regex_replace(format_string_, std::regex{"||"}, "-");
return parse(t, format_string_.c_str());
std::shared_ptr<date> date::clone(const char *calc_format_string, const char *format_string) const {
const auto t = format(calc_format_string == nullptr ? format_string : calc_format_string);
return parse(t, format_string);
}

void date::update(const char *calc_format_string, const char *format_string) {
const auto t = format(calc_format_string == nullptr ? format_string : calc_format_string);
m_time_point = parse(t, format_string)->m_time_point;
}

/**
Expand Down Expand Up @@ -133,6 +135,14 @@ std::string date::format(const char *format_string) const {
return result;
}

std::shared_ptr<tm> date::tm() const {
const std::time_t c_time_t = system_clk::to_time_t(m_time_point);
const auto t_ = std::localtime(&c_time_t);
auto t = std::make_shared<struct tm>();
memcpy(t.get(), t_, sizeof(struct tm));
return t;
}

std::string date::to_string(const char *format_string) const {
return format(format_string);
}
Expand Down
23 changes: 19 additions & 4 deletions src/date/date.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace clangTools {
// 重命名time_point类型
using _time_point = std::chrono::time_point<system_clk>;

private:
_time_point m_time_point;
public:
static std::shared_ptr<date> now();

Expand Down Expand Up @@ -74,6 +72,12 @@ namespace clangTools {
*/
std::string format(const char *format_string = "yyyy-MM-dd HH:mm:ss") const;

/**
* 获取tm格式
* @return tm 格式
*/
std::shared_ptr<struct tm> tm() const;

/**
* 转字符串
* @param format_string 格式
Expand All @@ -83,10 +87,19 @@ namespace clangTools {

/**
* 克隆一个对象
* @param format_string 格式,支持 "yyyy+1-MM-1-dd HH:mm:ss" 如果是 "yyyy-01-01 HH:mm:ss",需要改为 "yyyy--01--01 HH:mm:ss",否则解析错误
* @param calc_format_string 计算格式,支持 "yyyy+1-MM-1-dd HH:mm:ss" 如果是 "yyyy-01-01 HH:mm:ss",需要改为 "yyyy--01--01 HH:mm:ss",否则解析错误
* @param format_string 格式
* @return 新的对象
*/
std::shared_ptr<date> clone(const char *format_string = "yyyy-MM-dd HH:mm:ss") const;
std::shared_ptr<date> clone(const char *calc_format_string = "yyyy-MM-dd HH:mm:ss", const char *format_string = "yyyy-MM-dd HH:mm:ss") const;

/**
* 更新当前对象
* @param calc_format_string 计算格式,支持 "yyyy+1-MM-1-dd HH:mm:ss" 如果是 "yyyy-01-01 HH:mm:ss",需要改为 "yyyy--01--01 HH:mm:ss",否则解析错误
* @param format_string 格式
* @return 新的对象
*/
void update(const char *calc_format_string, const char *format_string);

public:
date &operator=(const date &date_) = default;
Expand Down Expand Up @@ -150,6 +163,8 @@ namespace clangTools {

static std::string replace_format(const char *format_string = "yyyy-MM-dd HH:mm:ss");

private:
_time_point m_time_point{};
};
}

Expand Down

0 comments on commit 9915d6c

Please sign in to comment.