C++方式解析时间字符串和计算时间
#include "StdAfx.h"
#include "MySetTimeByVT.h"
#include <ATLComTime.h>
#include <OleAuto.h> //VariantTimeToSystemTime()
#include <comutil.h> //_variant_t
#include <iostream>
using namespace std;
MySetTimeByVT::MySetTimeByVT(void)
{
GetLocalTime(&m_systime);
}
MySetTimeByVT::~MySetTimeByVT(void)
{
}
MySetTimeByVT::MySetTimeByVT(SYSTEMTIME sysTime)
{
m_systime = sysTime;
}
MySetTimeByVT::MySetTimeByVT(const MySetTimeByVT &time)
{
this->m_systime = time.m_systime;
}
MySetTimeByVT::MySetTimeByVT(const char *string)
{
Parse(string);
}
SYSTEMTIME MySetTimeByVT::GetTime()
{
return m_systime;
}
void MySetTimeByVT::Parse(const char* timeString)
{
std::string str_Time = timeString;
Parse(str_Time);
}
std::string MySetTimeByVT::ToString(const char* format)
{
string str_Format(format);
return ToString(str_Format);
}
void MySetTimeByVT::AddYears(int year)
{
m_milliseconds = m_systime.wMilliseconds;
m_systime.wYear += year;
_variant_t v_time;
SystemTimeToVariantTime(&m_systime,&v_time.date);
VariantTimeToSystemTime(v_time.date,&m_systime);
m_systime.wMilliseconds = m_milliseconds;
}
void MySetTimeByVT::AddMonths(int month)
{
m_milliseconds = m_systime.wMilliseconds;
m_systime.wMonth += month;
_variant_t v_time;
SystemTimeToVariantTime(&m_systime,&v_time.date);
VariantTimeToSystemTime(v_time.date,&m_systime);
m_systime.wMilliseconds = m_milliseconds;
}
void MySetTimeByVT::AddDays(int day)
{
m_milliseconds = m_systime.wMilliseconds;
m_systime.wDay += day;
_variant_t v_time;
SystemTimeToVariantTime(&m_systime,&v_time.date);
VariantTimeToSystemTime(v_time.date,&m_systime);
m_systime.wMilliseconds = m_milliseconds;
}
void MySetTimeByVT::AddHours(int hour)
{
m_milliseconds = m_systime.wMilliseconds;
m_systime.wHour += hour;
_variant_t v_time;
SystemTimeToVariantTime(&m_systime,&v_time.date);
VariantTimeToSystemTime(v_time.date,&m_systime);
m_systime.wMilliseconds = m_milliseconds;
}
void MySetTimeByVT::AddMinutes(int minute)
{
m_milliseconds = m_systime.wMilliseconds;
m_systime.wMinute += minute;
_variant_t v_time;
SystemTimeToVariantTime(&m_systime,&v_time.date);
VariantTimeToSystemTime(v_time.date,&m_systime);
m_systime.wMilliseconds = m_milliseconds;
}
void MySetTimeByVT::AddSeconds(int seconds)
{
m_milliseconds = m_systime.wMilliseconds;
m_systime.wSecond += seconds;
_variant_t v_time;
SystemTimeToVariantTime(&m_systime,&v_time.date);
VariantTimeToSystemTime(v_time.date,&m_systime);
m_systime.wMilliseconds = m_milliseconds;
}
void MySetTimeByVT::AddMilliseconds(int milliseconds)
{
int nowmilliseconds = m_systime.wMilliseconds + milliseconds;
m_systime.wMilliseconds = nowmilliseconds % 1000;
int addSeconds = nowmilliseconds / 1000;
AddSeconds(addSeconds);
}
void MySetTimeByVT::Parse(std::string& timeString)
{
if (timeString.empty())
{
cout<<"输入的字符串不能为空!"<<endl;
return;
}
static const basic_string <char>::size_type npos = -1;
try
{
int search_e = timeString.length();
int search_s = search_e;
int search_n = search_s;
//////////////////////////////////////////////////////////////////////////
//年-月-日 时:分:秒.毫秒
//////////////////////////////////////////////////////////////////////////
//时 时:分 时:分:秒 .毫秒 秒.毫秒 分:秒.毫秒 时:分:秒.毫秒
//取毫秒
search_s = timeString.rfind('.');
if (search_s != npos)
{
m_systime.wMilliseconds = stoi(timeString.substr(search_s + 1,search_e));
search_e = search_s;
search_n = timeString.rfind(' ',search_e - 1);
if (search_n != npos)
{
search_s = timeString.rfind(':',search_e - 1);
if (search_s != npos)
{
//取秒
m_systime.wSecond = stoi(timeString.substr(search_s + 1,search_e));
search_e = search_s;
//取分、时
search_s = timeString.rfind(':',search_e - 1);
if (search_s != npos)
{
//取分
m_systime.wMinute = stoi(timeString.substr(search_s + 1,search_e));
search_e = search_s;
//取时
if (search_n + 1 < search_e)
{
m_systime.wHour = stoi(timeString.substr(search_n + 1,search_e));
}
else
{
m_systime.wHour = 0;
}
search_e = search_n;
}
else
{
//取分
m_systime.wMinute = stoi(timeString.substr(search_n + 1,search_e));
search_e = search_n;
m_systime.wHour = 0;
}
}
else //取秒(秒.毫秒)
{
m_systime.wHour = 0;
m_systime.wMinute = 0;
if (search_n + 1 < search_e)
{
m_systime.wSecond = stoi(timeString.substr(search_n + 1,search_e));
}
else
{
m_systime.wSecond = 0;
}
search_e = search_n;
}
}
else
{
m_systime.wHour = 0;
m_systime.wMinute = 0;
m_systime.wSecond = stoi(timeString.substr(0,search_e));
search_e = 0;
}
}
else
{
m_systime.wMilliseconds = 0;
//取时、分、秒
search_s = timeString.rfind(' ',search_e - 1);
if (search_s != npos)
{
//取时、分、秒
search_n = timeString.find(':',search_s + 1);
if (search_n != npos)
{
m_systime.wHour = stoi(timeString.substr(search_s + 1,search_n));
search_s = search_n;
//取分、秒
search_n = timeString.find(':',search_s + 1);
if (search_n != npos)
{
m_systime.wMinute = stoi(timeString.substr(search_s + 1,search_n));
//取秒
m_systime.wSecond = stoi(timeString.substr(search_n + 1,search_e));
}
else
{
if (search_s + 1 < search_e)
{
m_systime.wMinute = stoi(timeString.substr(search_s + 1,search_e));
}
else
{
m_systime.wMinute = 0;
}
m_systime.wSecond = 0;
}
search_n = timeString.rfind(' ',search_e - 1);
search_e = search_n;
}
else
{
m_systime.wHour = stoi(timeString.substr(search_s + 1,search_e));
m_systime.wMinute = 0;
m_systime.wSecond = 0;
search_e = search_s;
}
}
else
{
m_systime.wHour = 0;
m_systime.wMinute = 0;
m_systime.wSecond = 0;
}
}
//////////////////////////////////////////////////////////////////////////
//天 月-天 年-月-天
//取天
search_s = timeString.rfind('-',search_e - 1);
if (search_s != npos)
{
m_systime.wDay = stoi(timeString.substr(search_s + 1,search_e));
search_e = search_s;
//取月
search_s = timeString.rfind('-',search_e - 1);
if (search_s != npos)
{
m_systime.wMonth = stoi(timeString.substr(search_s + 1,search_e));
search_e = search_s;
//取年
search_s = 0;
m_systime.wYear = stoi(timeString.substr(search_s,search_e));
search_e = search_s;
}
else
{
search_s = 0;
if (search_s < search_e)
{
m_systime.wMonth = stoi(timeString.substr(search_s,search_e));
}
else
{
m_systime.wMonth = 0;
}
search_e = search_s;
m_systime.wYear = 0;
}
}
else
{
search_s = 0;
if (search_s < search_e)
{
m_systime.wDay = stoi(timeString.substr(search_s,search_e));
}
else
{
m_systime.wDay = 0;
}
search_e = search_s;
m_systime.wMonth = 0;
m_systime.wYear = 0;
}
//////////////////////////////////////////////////////////////////////////
}
catch(...)
{
cout<<"输入字符串有问题!"<<endl;
}
m_milliseconds = m_systime.wMilliseconds;
//////////////////////////////////////////////////////////////////////////
//存在的问题 超过标准数值的值 并未被自动转换为标准值
_variant_t v_time;
SystemTimeToVariantTime(&m_systime,&v_time.date);
VariantTimeToSystemTime(v_time.date,&m_systime);
//转换为标准值
time_t m_time_t = SystemTimeToTime_t(m_systime);
m_systime = Time_tToSystemTime(m_time_t);
m_systime.wMilliseconds = m_milliseconds;
}
std::string MySetTimeByVT::ToString(std::string format)
{
const int str_size = 128;
char c_date[str_size] = {0};
if (format.empty())
{
sprintf_s(c_date,str_size,"%04d-%02d-%02d %02d:%02d:%02d.%d",m_systime.wYear,m_systime.wMonth,\
m_systime.wDay,m_systime.wHour,m_systime.wMinute,m_systime.wSecond,m_systime.wMilliseconds);
return c_date;
}
static const basic_string <char>::size_type npos = -1;
//日期
if (format.find("-m") != npos || format.find("-M") != npos)
{
sprintf_s(c_date,str_size,"%04d-%02d",m_systime.wYear,m_systime.wMonth);
if (format.find("-d") != npos || format.find("-D") != npos)
{
sprintf_s(c_date,str_size,"%s-%02d",c_date,m_systime.wDay);
}
}
else if (format.find("m-") != npos || format.find("M-") != npos)
{
sprintf_s(c_date,str_size,"%02d-%02d",m_systime.wMonth,m_systime.wDay);
}
else if (format.find('y') != npos || format.find('Y') != npos)
{
sprintf_s(c_date,str_size,"%04d",m_systime.wYear);
}
else if ( format.find('M') != npos && format.find(":") == npos && format.find("-") == npos )
{
sprintf_s(c_date,str_size,"%02d",m_systime.wMonth);
}
else if (format.find('d') != npos || format.find('D') != npos)
{
sprintf_s(c_date,str_size,"%02d",m_systime.wDay);
}
//时间
if (format.find(":m") != npos || format.find(":M") != npos)
{
sprintf_s(c_date,str_size,"%s %02d:%02d",c_date,m_systime.wHour,m_systime.wMinute);
if (format.find("s.") != npos || format.find("S.") != npos)
{
sprintf_s(c_date,str_size,"%s:%02d.%d",c_date,m_systime.wSecond,m_systime.wMilliseconds);
}
else if (format.find(":s") != npos || format.find(":S") != npos)
{
sprintf_s(c_date,str_size,"%s:%02d",c_date,m_systime.wSecond);
}
}
else if (format.find("m:") != npos || format.find("M:") != npos)
{
sprintf_s(c_date,str_size,"%s %02d:%02d",c_date,m_systime.wMinute,m_systime.wSecond);
if (format.find("s.") != npos || format.find("S.") != npos)
{
sprintf_s(c_date,str_size,"%s.%d",c_date,m_systime.wMilliseconds);
}
}
else if (format.find("s.") != npos || format.find("S.") != npos)
{
sprintf_s(c_date,str_size,"%s %02d.%d",c_date,m_systime.wSecond,m_systime.wMilliseconds);
}
else if (format.find('h') != npos || format.find('H') != npos)
{
sprintf_s(c_date,str_size,"%s %02d",c_date,m_systime.wHour);
}
else if (format.find('m') != npos && format.find("-") == npos && format.find(":") == npos )
{
sprintf_s(c_date,str_size,"%s %02d",c_date,m_systime.wMinute);
}
else if (format.find('s') != npos || format.find('S') != npos)
{
sprintf_s(c_date,str_size,"%s %02d",c_date,m_systime.wSecond);
}
else if (format.find('f') != npos || format.find('F') != npos)
{
sprintf_s(c_date,str_size,"%s %02d",c_date,m_systime.wMilliseconds);
}
return c_date;
}
SYSTEMTIME MySetTimeByVT::Time_tToSystemTime(time_t t)
{
//tm temptm = *localtime(&t);
tm temptm;
localtime_s(&temptm,&t);
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;
}
time_t MySetTimeByVT::SystemTimeToTime_t( const SYSTEMTIME& st )
{
tm temptm = {st.wSecond,
st.wMinute,
st.wHour,
st.wDay,
st.wMonth - 1,
st.wYear - 1900,
st.wDayOfWeek,
0,
0};
return mktime(&temptm);
}
C++方式解析时间字符串和计算时间的更多相关文章
- Java—时间的原点 计算时间所使用的 Date类/DateFormat类/Calendar类
Date类 类 Date 表示特定的瞬间,精确到毫秒. 毫秒概念:1000毫秒=1秒 毫秒的0点: System.currentTimeMillis() 返回值long类型参数 用于获取当前日期的毫 ...
- js 时间字符串转化为时间
对于时间字符串格式为:"2017-03-03 12:23:55"; IE:显示无效的日期 new Date("2017-03-3 12:23:55") //[d ...
- sqlserver 时间字符串转化为时间格式
),),),),,)) select substring('D:\\files,3,len('D:\\files)-2) --去掉前两位路径D:
- oracle如何将am,pm时间字符串改为时间格式
问题: 解决办法: 1.param["OPT_DATE"] = DateTime.Parse(dt.Rows[0]["CREATED_ON"].ToString ...
- 常见的时间字符串与timestamp之间的转换 时间戳
这里说的字符串不是一般意义上的字符串,是指在读取日期类型的数据时,如果还没有及时解析字符串,它就还不是日期类型,那么此时的字符串该怎么与时间戳之间进行转换呢? ① 时间字符串转化成时间戳 将时间字符串 ...
- JS时间戳与时间字符串之间的相互转换
时间字符串 转 时间戳 /** * 时间字符串 转 时间戳 * @param {String} time_str 时间字符串(格式"2014-07-10 10:21:12") * ...
- jq 解析josn字符串
1. var obj = jQuery.parseJSON("${ruleModel.rules}"); 2. var obj = eval("("+" ...
- Java8获取当前时间、新的时间日期类如Java8的LocalDate与Date相互转换、ZonedDateTime等常用操作包含多个使用示例、Java8时区ZoneId的使用方法、Java8时间字符串解析成类
下面将依次介绍 Date转Java8时间类操作 ,Java8时间类LocalDate常用操作(如获得当前日期,两个日期相差多少天,下个星期的日期,下个月第一天等) 解析不同时间字符串成对应的Java ...
- Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式
要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...
随机推荐
- Wix使用整理(一)
由于工作需要,学习了一段时间Wix,总算小有起色.鉴于国内Wix 的普及和使用有限,这里将个人遇到得问题和解决方案记录下来,以便交流和相互促进. Wix :全称 Windows Installer ...
- .NET:CLR via C# The CLR’s Execution Model
The CLR’s Execution Model The core features of the CLR memory management. assembly loading. security ...
- python笔记21-列表生成式
前言 python里面[]表示一个列表,快速生成一个列表可以用range()函数来生成. 对列表里面的数据进行运算和操作,生成新的列表最高效快速的办法,那就是列表生成式了. range() 1.一个连 ...
- Python调用windows下DLL详解
Python调用windows下DLL详解 - ctypes库的使用 2014年09月05日 16:05:44 阅读数:6942 在python中某些时候需要C做效率上的补充,在实际应用中,需要做部分 ...
- datagridview 单元格类型转换【备忘】
datagridview 在设定列类型后,其下面所有行的该列都与设定的列类型相同. 在需要改变某一行的某个单元格时,遇到了一些问题,再次进行备忘: 之前在遇到该问题时参考别人的博客解决过,但是时间久 ...
- CC+语言 struct 深层探索——CC + language struct deep exploration
1 struct 的巨大作用 面对一个人的大型C/C++程序时,只看其对struct 的使用情况我们就可以对其编写者的编程经验进行评估.因为一个大型的C/C++程序,势必要涉及一些(甚至 ...
- Openwrt WIFI探针开发【一】
2017.9.26 公开源码(Apache2.0协议) https://github.com/769484623/WiFiProbe ————————————————————————————————— ...
- java 中 SVN 设置所有文件及子目录 needs-lock, svn 提交时自动设置 needs-lock, 及版本不一致问题
摘自: http://my.oschina.net/zhangzhihao/blog/72177 设置后的效果:文件会自动带上svn:needs-lock属性,默认是只读的要签出才能修改以防止修改完后 ...
- 分治算法——Karastsuba算法
分治(Divide and Conquer)算法:问题能够分解为子问题,每一个问题是能够独立的解决的,从子问题的解能够构建原问题. Divide:中间分.随机分.奇偶分等,将问题分解成独立的子问题 C ...
- 机器学习学习笔记之一:K最近邻算法(KNN)
算法 假定数据有M个特征,则这些数据相当于在M维空间内的点 \[X = \begin{pmatrix} x_{11} & x_{12} & ... & x_{1M} \\ x_ ...