C++实现日期转换类DateTime
概述
工作中我们在网络传输时使用time_t来传输时间,在显示时使用字符串来显示,下面是一个日期转换类的实现,方便以后使用:
// DateTime.hpp
#ifndef _DATETIME_H
#define _DATETIME_H
#include <ctime>
#include <string>
#include <type_traits>
class DateTime
{
public:
template<typename T>
static typename std::enable_if<std::is_same<std::string, T>::value, std::string>::type convert(time_t t)
{
return time2string(t);
}
template<typename T>
static typename std::enable_if<std::is_same<time_t, T>::value, time_t>::type convert(const std::string& timeStr)
{
return string2time(timeStr);
}
static std::string currentTime()
{
return time2string(time(nullptr));
}
private:
static std::string time2string(time_t t)
{
struct tm* tmNow = localtime(&t);
char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = {'\0'};
std::strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tmNow);
return timeStr;
}
static time_t string2time(const std::string& timeStr)
{
struct tm stTm;
sscanf(timeStr.c_str(), "%d-%d-%d %d:%d:%d",
&(stTm.tm_year),
&(stTm.tm_mon),
&(stTm.tm_mday),
&(stTm.tm_hour),
&(stTm.tm_min),
&(stTm.tm_sec));
stTm.tm_year -= 1900;
stTm.tm_mon--;
stTm.tm_isdst = -1;
return mktime(&stTm);
}
};
#endif
下面是DateTime的具体使用例子:
// main.cpp
#include <iostream>
#include "DateTime.hpp"
int main()
{
std::string timeStr = DateTime::convert<std::string>(time(nullptr));
std::cout << timeStr << std::endl;
std::cout << DateTime::convert<time_t>(timeStr) << std::endl;
std::cout << DateTime::currentTime() << std::endl << std::endl;
return 0;
}
该例子的github地址:https://github.com/chxuan/samples/tree/master/DateTime
C++实现日期转换类DateTime的更多相关文章
- C#日期转换类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...
- 日期转换类 DateConverter.java
package com.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.D ...
- asp中将文本框内的日期转换成datetime类型的数据
将字符类型的日期转化为DateTime类型主要有以下方法: 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss 方法二: ...
- Sql 中常用日期转换Convert(Datetime)
CONVERT(data_type,expression[,style]) convert(varchar(10),字段名,转换格式) 说明:此样式一般在时间类型(datetime,smalldate ...
- Sql 中常用日期转换Convert(Datetime) convert datetime
Convert(data_type,expression[,style]) Convert(varchar(10),字段名,转换格式) 说明:此样式一般在时间类型(datetime,smalldate ...
- mysql 查询 int类型日期转换成datetime类型
数据库日期类型是int类型的,该查询结果是datetime类型的 SELECT from_unixtime( `时间列名` ) FROM 表名 如果原来类型是datetime类型,查询结果要是int类 ...
- js moment.js日期操作类 datetime,日期操作,dayjs
http://momentjs.com/ JS时间处理插件MomentJS https://juejin.im/post/5a2bdc55f265da432b4abf5e Day.js 2kB超轻量时 ...
- C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)
在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...
- java日期转换
在java开发过程中,时间的转换时必须掌握的=========下面把时间转换做个总结,有可能不是很全面 时间格式只有两种 yyyy-MM-DD yyyy/MM/DD 时间的类型:字符串类型.sql类型 ...
随机推荐
- http协议要点
概念: HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web Consortium)和Internet工作小组 ...
- 安装phonegap
1.安装nodejs http://nodejs.org/ 2.安装git www.git-scm.com/download 3.安装jdk http://www.oracle.com/technet ...
- Ubuntu下gdb远程调试--warning: Could not load vsyscall page because no executable was specified解决方案
1. 首先安装gdbserver apt-get install gdbserver 2. 编译-g 程序 gcc -g test_gdb.c -o test_gdb 源码如下: #include & ...
- vim插件开发初步
[vim插件开发初步] 将如下代码存在helloworld.vim, 放在~/.vim/plugin目录下,插件即可生效.:w保存代码后, 用:source命令执行后,也可以使用Helloworld命 ...
- ilog
PCISV7-VHL [2015-11-13 13:51:36,038]>>>INFO>>>[ com.isoftstone.pcis.policy.app.pla ...
- AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)
使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...
- Android打开WIFI或者移动网络的代码实现
MainActivity如下: package wy.testnetwork; import java.lang.reflect.Field; import java.lang.reflect.Inv ...
- web.xml 详解
http://xmlns.jcp.org/xml/ns/javaee 重定向为 http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javae ...
- M站 confirm 插件
/*弹出提示*/.pop-error{position:absolute; left:25%; top:50%; width:200px; FILTER: progid:DXImageTransfor ...
- Fragment进阶
fragment之间的通信,fragment和Activity生命周期之间的关系 通过上一篇浅显的学习了一下,怎么在Activity中添加fragment.在介绍fragment之间的通信之前,我们来 ...