filetimeToUnixTime
windos filetime 起于 1601-01-01T00:00:00Z , 单位 100 纳秒
UnixTime 起于 1970-01-01T00:00:00Z , 单位 秒
它们的起始时间差了 11644473600 秒
下面这个函数把 filetime 转换成 UnixTime的秒偏差, 返回值 相当于 time(0), ms 返回毫秒偏差
#define WINDOWS_TICK 10000000
#define SEC_TO_UNIX_EPOCH 11644473600LL
int64_t filetimeToUnixTime(int64_t windowsTicks, int* ms=NULL)
{
if (ms) {
*ms = (windowsTicks % WINDOWS_TICK) / (WINDOWS_TICK / 1000);
}
return (unsigned)(windowsTicks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);
}
再记录几个实用函数
1. 毫秒休眠
// msleep(200); 休眠 200ms
void msleep(int ms)
{
#ifdef WIN32
Sleep(ms);
#else
usleep(ms*1000);
#endif
}
2. 获取修改文件时间
// 获取文件时间
// windows 是 创建时间(create time) 而 linux 是 改变时间 (change time)
// 调用的时候注意程序运行的 平台
bool filetime(const string& path, time_t* mtime, time_t* atime=NULL, time_t* createtime=NULL, time_t* changetime=NULL);
bool change_filetime(const string& path, time_t mtime, time_t atime);
#ifdef WIN32
SYSTEMTIME TimeToSystemTime(time_t time)
{
tm temptm = *localtime(&time);
SYSTEMTIME st = { 1900 + temptm.tm_year, 1 + temptm.tm_mon,
temptm.tm_wday, temptm.tm_mday, temptm.tm_hour,
temptm.tm_min, temptm.tm_sec, 0 };
return st;
}
#endif
bool change_filetime(const string& path, time_t mtime, time_t atime)
{
#ifdef WIN32
HANDLE f = CreateFileA(path.c_str(), // LPCTSTR lpFileName,
GENERIC_READ | GENERIC_WRITE, // DWORD dwDesiredAccess,
FILE_SHARE_READ | FILE_SHARE_DELETE,
NULL, // LPSECURITY_ATTRIBUTES lpSecurityAttributes,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, // DWORD dwFlagsAndAttributes,
NULL);
FILETIME fmtime, fatime;
SYSTEMTIME local_mtime = TimeToSystemTime(mtime);
SYSTEMTIME local_atime = TimeToSystemTime(atime);
SYSTEMTIME sys_mtime, sys_atime;
TzSpecificLocalTimeToSystemTime(NULL, &local_mtime, &sys_mtime);
TzSpecificLocalTimeToSystemTime(NULL, &local_atime, &sys_atime);
SystemTimeToFileTime(&sys_mtime, &fmtime);
SystemTimeToFileTime(&sys_atime, &fatime);
SetFileTime(f, NULL, &fatime, &fmtime);
CloseHandle(f);
#else
struct utimbuf buf;
buf.modtime = mtime;
buf.actime = atime;
utime(path.c_str(), &buf);
#endif
return true;
}
bool filetime(const string& path, time_t* mtime, time_t* atime/*=NULL*/, time_t* createtime/*=NULL*/, time_t* changetime/*=NULL*/) {
struct stat st;
int ret = stat(path.c_str(), &st);
if (-1 == ret)
return false;
if (mtime) *mtime = st.st_mtime;
if (atime) *atime = st.st_atime;
#ifdef WIN32
if (createtime) *createtime = st.st_ctime;
if (changetime) // windows 下没有这个时间 参数错误
return false;
#else
if (changetime) *changetime = st.st_ctime;
if (createtime) // linux 下没有这个时间 参数错误
return false;
#endif
return true;
}
3. 时间字符串的几个转换
// time_t 转换成时间字符串
string time2date(time_t tm, const char* fmt = "%Y-%m-%d");
// yyyy-mm-dd hh:mm:ss --> time_t
// 假设你传入的时间一定是对的
time_t s2time(const string& s);
// 返回 20190528
string current_date(const char* fmt = "%d%02d%02d");
// 返回 [09:18:24.234]
string current_time();
string current_date(const char* fmt /*= "%d%02d%02d"*/)
{
char date[40] = { 0 };
#ifdef WIN32
//获得当前日期
SYSTEMTIME st;
GetLocalTime(&st);
sprintf_s(date, sizeof date, fmt, st.wYear, st.wMonth, st.wDay);
#else
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
struct tm *x = localtime(&(now.tv_sec));
snprintf(date, sizeof date, fmt, 1900 + x->tm_year, x->tm_mon + 1, x->tm_mday);
#endif
return date;
}
time_t s2time(const string& s) {
struct tm t;
int n = sscanf(s.c_str(), "%d-%d-%d %d:%d:%d",
&t.tm_year, &t.tm_mon, &t.tm_mday,
&t.tm_hour, &t.tm_min, &t.tm_sec);
if (n != 6)
return 0;
t.tm_year -= 1900;
t.tm_mon -= 1;
return mktime(&t);
}
string time2date(time_t tm, const char* fmt /*= "%Y-%m-%d"*/)
{
char ch[64] = {0};
strftime(ch, sizeof(ch) - 1, fmt, localtime(&tm));
return ch;
}
/* 这个函数好是好 就是不能精确到 毫秒
static string getCurrentTimeStr()
{
time_t t = time(NULL);
char ch[64] = {0};
strftime(ch, sizeof(ch) - 1, "%Y-%m-%d %H:%M:%S", localtime(&t)); //年-月-日 时-分-秒
return ch;
}
*/
string current_time()
{
char now[30] = { 0 };
#ifdef WIN32
//获得当前日期
SYSTEMTIME st;
GetLocalTime(&st);
sprintf_s(now, 30, "[%02d:%02d:%02d.%03li]", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
#else
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
struct tm *x = localtime(&(ts.tv_sec));
snprintf(now, 30, "[%02d:%02d:%02d.%03li]", x->tm_hour, x->tm_min, x->tm_sec, ts.tv_nsec / (int)1E6);
#endif
return now;
}