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日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...
随机推荐
- ExtJS 表单 submit时错误处理
这里不提success,提提Extjs 表单提交的failure方法. 在表单的提交中,当发生异常行为时通常分为三种情况 1. 无法连接到服务器 2. 表单验证错误 3. 业务逻辑错误 对应下面的代码 ...
- Make a printer-port EEPROM programmer and dongle
You can easily use a PC's printer port for serial-EEPROM programming. You can use a device-programme ...
- 源码分析:动态分析 Linux 内核函数调用关系
源码分析:动态分析 Linux 内核函数调用关系 时间 2015-04-22 23:56:07 泰晓科技 原文 http://www.tinylab.org/source-code-analysi ...
- Java:初学 Java,遇到的问题及解决方案小结。
背景 失败是成功之母,前提是没有被遗忘的失败,本文记录这几天学习 Java,自己遇到的几个问题和解决方案,希望能达到强化记忆的作用. 开发环境是:Eclipse + Tomcat7.0. 第一个:依赖 ...
- jQuery -> filter使用方法
利用filter函数能够从wrapper set中过滤符合条件的DOM元素. 假设我们有一个内容例如以下的html文件,要获取类为external的<a>元素,使用filter能够非常ea ...
- [Android Studio] Android Studio中查看类的继承关系
转载自:http://blog.csdn.net/hyr83960944/article/details/38098091 查看类的继承关系的快捷键F4,在Android Studio常用快捷键这篇文 ...
- 4 cocos2dx 3.0 源码分析- scheduler
scheduler 这个类, 负责了引擎的自定义更新, 及定时更新相关的操作, 看看下面的代码,很熟悉吧. schedule(schedule_selector(HelloWorld::updat ...
- CSS:CSS层叠样式表的概述
CSS层叠样式表:Cascading Style Sheets 介绍: 现代网页的设计原则是内容和样式分离,降低它们之间的直接相互依存关系,解耦性,同时,同样的内容,可以通过不同的CSS样式表现出来. ...
- iOS:三种数据库的小总结
三种数据库总结:sqlite.FMDB.CoreData 1.sqlite数据库(C语言)需要方法和属性: (1)数据类型: –INTEGER 有符号的整数类型 –REAL 浮点类型 –TEXT ...
- Linux下C获取所有可用网卡信息
在Linux下开发网络程序时,经常会遇到需要取本地网络接口名.IP.广播地址.子网掩码或者MAC地址等信息的需求,最常见的办法是配合宏SIOCGIFHWADDR.SIOCGIFADDR.SIOCGIF ...