C 将日期时间转换为时间戳
C++ converting datetime to timestamp
我知道这些值
unsigned char year = 17; // means 2017
unsigned char month = 8;
unsigned char day = 25;
unsigned char hour = 14;
unsigned char minute = 23;
unsigned char second = 54;
如何将它们转换为UNIX时间戳记?我不确定unsigned char
是否是表示值的正确方法,我只需要每个值是大小的1 byte。
ubervan回答了您的问题
将日期分解为其组件,即日,月,年,然后:
struct tm tm;
time_t rawtime;
time ( &rawtime );
tm = *localtime ( &rawtime );
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
mktime(&tm);
tm现在可以转换为time_t并进行操纵。
您的问题也在这里解决。
也许最简单,最高的性能是使用霍华德·辛南特(Howard Hinnant
#include "date.h"
#include <iostream>
date::sys_seconds
to_sys_time(unsigned char y, unsigned char m, unsigned char d,
unsigned char h, unsigned char M, unsigned char s)
{
using namespace date;
using namespace std::chrono;
return sys_days{year{y+2000}/m/d} + hours{h} + minutes{M} + seconds{s};
}
int
main()
{
std::cout << to_sys_time(17, 9, 25, 14, 23, 54).time_since_epoch().count() << 'n';
}
此输出:
1503671034
此库扩展了<chrono>
库来处理日历计算,甚至是为标准化而提出的。
这就是我的方式,它可以接受更多的C 11。
std::string timepointToString(std::chrono::system_clock::time_point const& t) {
time_t tt = std::chrono::system_clock::to_time_t(t);
struct tm tb;
size_t const len(21);
char buffer[len];
TRI_gmtime(tt, &tb);
::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &tb);
return std::string(buffer, len - 1);
}
std::chrono::system_clock::time_point stringToTimepoint(std::string const& s) {
if (!s.empty()) {
try {
std::tm tt;
tt.tm_year = std::stoi(s.substr(0, 4)) - 1900;
tt.tm_mon = std::stoi(s.substr(5, 2)) - 1;
tt.tm_mday = std::stoi(s.substr(8, 2));
tt.tm_hour = std::stoi(s.substr(11, 2));
tt.tm_min = std::stoi(s.substr(14, 2));
tt.tm_sec = std::stoi(s.substr(17, 2));
tt.tm_isdst = 0;
auto time_c = TRI_timegm(&tt);
return std::chrono::system_clock::from_time_t(time_c);
} catch (...) {}
}
return std::chrono::time_point<std::chrono::system_clock>();
}
#include <iostream>
#include <string>
#include <chrono>
#include <sstream>
#include <iomanip>
const std::string timeFormat{"%Y%m%d %H:%M:%S"};
int64_t getTimeStamp(const std::string& timeString)
{
std::istringstream m_istream{timeString};
std::tm m_tm { 0 };
std::time_t m_timet { 0 };
m_istream >> std::get_time(&m_tm, timeFormat.c_str());
m_timet = std::mktime(&m_tm);
m_timet *= 1000; // convert to milliseconds
return m_timet;
}
int main() {
const std::string timeString{ "20220509 10:07:38" };
int64_t result = getTimeStamp(timeString);
std::cout << "result:" << result << std::endl;
return 0;
}
相关文章:
- 将 unix 时间戳转换为人类可读的日期
- 使用 C++将时间戳转换为格式化的日期时间
- 如何将 time-uuid(存储在 boost uuid 中)转换为时间戳/自纪元以来的时间?
- 将日期字符串转换为 unix 时间戳
- 将带有参考时间的时间戳转换为格式化时间
- 如何将纳米第二个时期时间戳转换为可读格式
- 使用 std::chrono / date::gps_clock 将双 GPS 时间戳转换为 UTC/tai
- 将时间戳转换为纳秒 (Qt)
- C 将字符串时间戳转换为STD :: Chrono :: System_Clock :: Time_point
- 如何将WSTRING中的Unix时间戳转换为char数组中格式的日期
- 如何将 UTC 时间戳转换为本地时间,即整点后的几秒钟
- 时间戳转换错误
- 使用 std::chrono 将 32 位 unix 时间戳转换为 std::string
- C 将没有时区域区域区域的Postgres时间戳转换为Time_T
- 如何将负时间戳转换为字符串.C++
- C++ Boost将UNIX时间戳转换为MySQL兼容的DATETIME字符串
- 如何将时间戳t转换为实时
- 将unix时间戳(s)转换为c++中的星期几
- 64位Unix时间戳转换
- Unix时间戳转换在JS和QTDatetime的区别