C++时间函数用法总结(部分)
来源:互联网 发布:mysql配置文件my.ini 编辑:程序博客网 时间:2024/05/20 18:55
1、获取1970以来的时间戳
#include "stdafx.h"#include <iostream> #include <time.h> #include <sys/timeb.h> using namespace std; int main() { long long time_last; time_last = time(NULL);cout<<time_last<<endl; //1970时间戳秒数方法1 struct timeb t1; ftime(&t1);cout<<t1.time<<endl; //1970时间戳秒数方法2 cout<<t1.time*1000 + t1.millitm<<endl; //总毫秒数 system("pause"); return 0; }
2、FILETIME转SYSTEMTIME
FILETIME是1601年1月1日至今的数字(单位是100纳秒即0.1微秒)。
SYSTEMTIME是系统时间,该结构体成员分别是现今时间体系的年、月、星期几、日、小时、分钟、秒、毫秒。
#include "stdafx.h"#include <windows.h>#include <tchar.h>#include <strsafe.h>// GetLastWriteTime - Retrieves the last-write time and converts// the time to a string//// Return value - TRUE if successful, FALSE otherwise// hFile - Valid file handle// lpszString - Pointer to buffer to receive stringBOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString, DWORD dwSize){FILETIME ftCreate, ftAccess, ftWrite;SYSTEMTIME stUTC, stLocal;DWORD dwRet;// Retrieve the file times for the file.if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))return FALSE;// Convert the last-write time to local time.FileTimeToSystemTime(&ftWrite, &stUTC);SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);// Build a string showing the date and time.dwRet = StringCchPrintf(lpszString, dwSize, TEXT("%02d/%02d/%d %02d:%02d"),stLocal.wMonth, stLocal.wDay, stLocal.wYear,stLocal.wHour, stLocal.wMinute);if( S_OK == dwRet )return TRUE;else return FALSE;}int _tmain(int argc, TCHAR *argv[]){HANDLE hFile;TCHAR szBuf[MAX_PATH];//if( argc != 2 )//{//printf("This sample takes a file name as a parameter\n");//return 0;//}char fileName[128] = {"C:\\config.ini"};//注意需要C盘存在该文件hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL,OPEN_EXISTING, 0, NULL);if(hFile == INVALID_HANDLE_VALUE){printf("CreateFile failed with %d\n", GetLastError());return 0;}if(GetLastWriteTime( hFile, szBuf, MAX_PATH ))_tprintf(TEXT("Last write time is: %s\n"), szBuf);}
3、时间戳转字符串时间
方法1
string Timestamp2StrTime(time_t lTimestamp){struct tm *pTime;lTimestamp += 8*3600;//8小时pTime = gmtime(&lTimestamp);//GMT+8*3600=CST,获取CST时间char sTime[80] = {0};//strftime(sTime, 80, "%Y-%m-%d %H:%M:%S", pTime);//方法1//sprintf(sTime,"%04d-%02d-%02d %02d:%02d:%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday,//pTime->tm_hour,pTime->tm_min,pTime->tm_sec );sprintf(sTime,"%04d-%02d-%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday);return sTime;}
方法2
string Timestamp2StrTime2(time_t lTimestamp){struct tm *pTime;pTime = localtime(&lTimestamp);//char sTime[80] = {0};//strftime(sTime, 80, "%Y-%m-%d %H:%M:%S", pTime);//方法1sprintf(sTime,"%04d-%02d-%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday);return sTime;}
4、时间戳转tm和tm转时间戳
#include "stdafx.h"#include <time.h>#include <stdio.h>int main( void ){time_t lTimestamp;time(&lTimestamp);struct tm *pTime; pTime = localtime(&lTimestamp);//时间戳转tmlong lTimestamp2 = mktime(pTime);//tm转时间戳int days = 60;pTime->tm_mday = pTime->tm_mday + days;char sTime[80] = {0}; //strftime(sTime, 80, "%Y-%m-%d %H:%M:%S", pTime);//方法1 sprintf(sTime,"%04d-%02d-%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday); getchar();return 0;}
5、文件时间、系统时间转时间戳
long FileTime2Timestamp(FILETIME ftTime){SYSTEMTIME stTime = {0}; //最近访问时间if( !::FileTimeToLocalFileTime(&ftTime, &ftTime))//转换成本地FileTime,防止相差8个小时return 0;if( !::FileTimeToSystemTime( &ftTime, &stTime))return 0;struct tm structTime;structTime.tm_year = stTime.wYear-1900;structTime.tm_mon = stTime.wMonth-1;structTime.tm_mday = stTime.wDay;structTime.tm_hour = 0;structTime.tm_min = 0;structTime.tm_sec = 0;//structTime.tm_hour = stTime.wHour;//structTime.tm_min = stTime.wMinute;//structTime.tm_sec = stTime.wSecond;long lTimestamp = mktime(&structTime);return lTimestamp;}
6、CST时间(邮件时间)转字符串时间
#include <string>#include <iostream>#include <time.h>#include <sys/timeb.h>using namespace std;//string mailTime = "Tue, 15 Nov 2016 19:33:18 +0800";string CSTTime2StrTime(string mailTime){char xingqi[4];//星期几struct tm st;time_t tt;char mon[4];char tmpbuf[128];//string stdStrTime;char mn[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};sscanf(mailTime.c_str(), "%3s, %2d %3s %4d %2d:%2d:%2d +0800", xingqi,&st.tm_mday, mon, &st.tm_year, &st.tm_hour, &st.tm_min, &st.tm_sec);for (int i=0;i<12;i++) if (0==stricmp(mn[i],mon)) {st.tm_mon=i; break;}st.tm_year-=1900;tt=mktime(&st);//获取时间戳if (-1!=tt) {strftime(tmpbuf,128,"%Y-%m-%d %H:%M:%S\n",localtime(&tt));//时间戳转字符串时间//stdStrTime = tmpbufprintf(tmpbuf);//2011-12-08 15:25:03} else {printf("[%s] is Invalid time string!\n",mailTime.c_str());}return tmpbuf;}int main( ) {using namespace std;string mailTime = "Tue, 15 Nov 2016 19:33:18 +0800";string strStdTime = CSTTime2StrTime(mailTime);}
0 0
- C++时间函数用法总结(部分)
- C/C++ 时间、日期函数 用法总结
- PHP部分时间函数总结
- C语言字符串函数总结(部分常见字符串函数)
- 黑马程序员------C语言函数部分总结(一)
- 黑马程序员------C语言函数部分总结(二)
- Oracle中的时间函数用法(to_date、to_char) (总结)
- Oracle中的时间函数用法(to_date、to_char) (总结)
- Oracle中的时间函数用法(to_date、to_char) (总结)
- Oracle中的时间函数用法(to_date、to_char) (总结)
- C++tolower函数用法总结
- assert()函数用法总结(c++)
- sql 获取时间函数用法总结
- 标准C语言各种时间函数用法
- 标准C语言各种时间函数用法
- 【C/C++】assert()函数用法总结
- C#.net时间函数的用法 时间格式输出
- C语言 获得系统时间以及时间函数的用法。
- Linux多线程的同步与互斥
- Swoole数据库的使用
- 正则表达式——反向引用
- 一个诚实的软件下载网站
- 洛谷 P2239 螺旋矩阵
- C++时间函数用法总结(部分)
- 无缝滚动的实现
- 洛谷 P1078 文化之旅
- pip install error
- 不变的自己
- java 两个栈实现队列与两个列实现栈
- struts2表单错误处理和数据验证
- 接口的特点
- JavaScript 表达式与运算符(2017-1-10更新)