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日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...
随机推荐
- Ext.js 中 25种类型的Ext.panel.Tool
通过Ext.panel.Panel的tools配置项来设置Ext.panel.Tool实例. 要注意的一点是,Ext框架提供的Ext.panel.Tool仅包含按钮图标而具体的点击事件处理函数需要我们 ...
- 修改gnome-shell扩展“Applications Menu”的菜单区域宽度。
sudo打开 /usr/share/gnome-shell/extensions/apps-menu@gnome-shell-extensions.gcampax.github.com/extensi ...
- Flex入门(一)——基本理论
Flash作为一款美丽动画的产品,是大家所喜爱的,他能够给我们带来更好的视觉效果.可是怎样使其非常快非常好的让Java程序猿,.net程序猿等所接受,并进行相关开发.Macromedia公司在2004 ...
- BZOJ 2179 FFT快速傅立叶 题解
bzoj 2179 Description 给出两个n位10进制整数x和y,你需要计算x*y. [题目分析] 高精裸题.练手. [代码] 1.手动高精 #include<cstdio> # ...
- 探寻C++最快的读取文件的方案 ——C++ IO优化
在竞赛中,遇到大数据时,往往读文件成了程序运行速度的瓶颈,需要更快的读取方式.相信几乎所有的C++学习者都在cin机器缓慢的速度上栽过跟头,于是从此以后发誓不用cin读数据.还有人说Pascal的re ...
- 数字锁相环Octave仿真
clc; clear all; % 仿真数据长度 SimLens = 1000; % 载波信号 Fs = 2400; Ts = 1 / Fs; Fsig = 60; % 随机初相 Delta_Phas ...
- Informatica 常用组件Source Qualifier之四 SQL Query
源限定符转换提供 SQL 查询选项以覆盖默认的查询.您可以输入您的源数据库支持的 SQL 语句.输入查询之前,请连接您要在映射中使用的所有输入和输出端口. 编辑 SQL 查询时,您可以生成并编辑默认查 ...
- c#如何操作ppt的播放 【转】
C#控制ppt的代码 包括打开ppt文件.读取幻灯页,插入幻灯片,自动播放等 using System.Collections.Generic; using System.Linq; using Sy ...
- 引导工具GRUB详解
导读 引导程序是驻留在硬盘第一个扇区(MPR.主引导记录)的程序.GRUB是一个功能强大的多系统引导程序,专门处理Linux与其它操作系统共存的问题.下面就由我介绍一下grub.conf文件里的具体内 ...
- Kettle中txt类型数据源作为输入需要注意的地方
文本类型在kettle中作为数据源的时候,需要注意的几点,ktr的机构如下图 1:txt文本的格式 2:文本输入控件的设置 --2.1:选择文件所在物理位置 --2.2:设置分隔符,注意头部数量去掉, ...