#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日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...
随机推荐
-
documentbodyscrollTop的值总为零的解决办法
有一个功能需要判断返回顶部按钮是否显示. JS代码如下: var sTop = document.body.scrollTop; if(sTop>100){ document.getElemen ...
-
ASP.NET(C#)中的try catch异常处理机制
在开发一个Umbraco平台系统的过程中,遇到了问题. 写的代码如下 fileUrl = MediaHelper.GetMediaUrl(Convert.ToInt32(publishedConten ...
-
(转)Linux下tomcat JVM内存设置步骤
java.lang.OutOfMemoryError: PermGen space java.lang.OutOfMemoryError: Java heap space -------------- ...
-
backbone showcase
http://www.mhtml5.com/2012/06/5119.html http://tieba.baidu.com/p/2389371223 http://www.jdon.com/tags ...
-
算法题丨Remove Element
描述 Given an array and a value, remove all instances of that value in-place and return the new length ...
-
[破解] IPhone 5S icloud dns bypass
http://ui.iclouddnsbypass.com/deviceservices/buddy/barney_activation_help_en_us.buddyml http://www.j ...
-
Linux常用内核参数
Linux常用内核参数 TCP状态描述 CLOSED:无连接是活动的或正在进行的 LISTEN:服务器在等待进入呼叫 SYN-RECV:一个连接请求已经到达,等待确认 SYN-SENT:应用已经开始, ...
-
阿里云云主机swap功能设置实战案例
阿里云云主机swap功能设置实战案例 阿里云提供的云服务器(Elastic Compute Service,简称 ECS),是云主机的一种,当前采用的虚拟化驱动是Xen(这一点可以通过bios ven ...
-
mongo blancer
在 sharded cluster 体系结构中,Balancer 进程的作用是转移数据,当一个 shard 中的数据比其它 shard 的数据多并达到一定条件时,Balancer 进程触发. 为了减少 ...
-
html的framset使用
frameset主要用在显示多个页面的需求下: 看代码: <html> <head> <title>html frameset test</title> ...