时间和字符串的转换

1、常用的时间存储方式
1)time_t类型,这本质上是一个长整数,表示从1970-01-01 00:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。
2)tm结构,这本质上是一个结构体,里面包含了各时间字段

struct tm {
    int tm_sec;     /* seconds after the minute - [0,59] */
    int tm_min;     /* minutes after the hour - [0,59] */
    int tm_hour;    /* hours since midnight - [0,23] */
    int tm_mday;    /* day of the month - [1,31] */
    int tm_mon;     /* months since January - [0,11] */
    int tm_year;    /* years since 1900 */
    int tm_wday;    /* days since Sunday - [0,6] */
    int tm_yday;    /* days since January 1 - [0,365] */
    int tm_isdst;   /* daylight savings time flag */
};

其中tm_year表示从1900年到目前计时时间间隔多少年,如果是手动设置值的话,tm_isdst通常取值-1。

2、常用的时间函数

time_t time(time_t *t); //取得从1970年1月1日至今的秒数
char *asctime(const struct tm *tm); //将结构中的信息转换为真实世界的时间,以字符串的形式显示
char *ctime(const time_t *timep); //将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样
struct tm *gmtime(const time_t *timep); //将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针
struct tm *localtime(const time_t *timep); //和gmtime类似,但是它是经过时区转换的时间。
time_t mktime(struct tm *tm); //将struct tm 结构的时间转换为从1970年至今的秒数
int gettimeofday(struct timeval *tv, struct timezone *tz); //返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用
double difftime(time_t time1, time_t time2); //返回两个时间相差的秒数

3、时间与字符串的转换

需要包含的头文件如下

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>

1)unix/windows下时间转字符串参考代码

time_t t;  //秒时间
tm* local; //本地时间
tm* gmt;   //格林威治时间
char buf[128]= {0};

t = time(NULL); //获取目前秒时间
local = localtime(&t); //转为本地时间
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local);
std::cout << buf << std::endl;

gmt = gmtime(&t);//转为格林威治时间
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt);
std::cout << buf << std::endl;

2)unix字符串转时间参考代码

tm tm_;
time_t t_;
char buf[128]= {0};

strcpy(buf, "2012-01-01 14:00:00");
strptime(buf, "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间
tm_.tm_isdst = -1;
t_  = mktime(&tm_); //将tm时间转换为秒时间
t_ += 3600;  //秒数加3600

tm_ = *localtime(&t_);//输出时间
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_);
std::cout << buf << std::endl;

3)由于windows下没有strptime函数,所以可以使用scanf来格式化

time_t StringToDatetime(char *str)
{
    tm tm_;
    int year, month, day, hour, minute,second;
    sscanf(str,"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
    tm_.tm_year  = year-1900;
    tm_.tm_mon   = month-1;
    tm_.tm_mday  = day;
    tm_.tm_hour  = hour;
    tm_.tm_min   = minute;
    tm_.tm_sec   = second;
    tm_.tm_isdst = 0;

    time_t t_ = mktime(&tm_); //已经减了8个时区
    return t_; //秒时间
}
时间: 2024-08-05 15:23:02

时间和字符串的转换的相关文章

时间 字符串来回转换

import time, datetime # 时间到字符串的转换 print time.strftime("%Y-%m-%d %X", time.localtime()) # 字符串到时间的转换 t = time.strptime("2008-08-08 09:32:10", "%Y-%m-%d %X") print t[0:6] y, m, d, H, M, S = t[0:6] print datetime.datetime(y, m, d

C++时间与字符串转换

转自:http://blog.csdn.net/educast/article/details/17239735 1.常用的时间存储方式 1)time_t类型,这本质上是一个长整数,表示从1970-01-01 00:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒. 2)tm结构,这本质上是一个结构体,里面包含了各时间字段 1 struct tm { 2 int tm_sec; /* seconds after the minute - [0,59] */

c/c++日期时间处理与字符串string转换

在c/c++实际问题的编程中,我们经常会用到日期与时间的格式,在算法运行中,通常将时间转化为int来进行计算,而处理输入输出的时候,日期时间的格式却是五花八门,以各种标点空格相连或者不加标点. 首先,在c中,是有一个标准的日期时间结构体的,在标准库wchar.h内,我们可以看到结构体tm的声明如下: 1 #ifndef _TM_DEFINED 2 struct tm { 3 int tm_sec; /* seconds after the minute - [0,59] */ 4 int tm_

mysql时间、字符串、时间戳互相转换

时间转字符串select date_format(now(), ‘%Y-%m-%d %H:%i:%s’); 结果:2018-05-02 20:24:10时间转时间戳select unix_timestamp(now()); 结果:1525263383字符串转时间select str_to_date(‘2018-05-02’, ‘%Y-%m-%d %H’); 结果:2018-05-02 00:00:00字符串转时间戳select unix_timestamp(‘2018-05-02’); 结果:1

linux中常用时间和字符串之间相互转化

在Linux中经常会遇到时间和字符串相互转化的情形,有两个函数专门对应相应的转化. 1.时间转字符串函数strftime 函数原型:size_t strftime(char *s,size_t maxsize,char *format,conststruct tm *timeptr) strftime函数对timeptr指向的tm结构所代表的时间和日期进行格式编排,其结果放在字符串s中.该字符串的长度被设置为(最少)maxsize个字符.格式字符串format用来对写入字符串的字符进行控制,它包

C# 日期和时间的字符串表示形式转换为其等效的DateTime(stringToDateTime)

一. 标准的日期和时间字符串转换 将日期和时间的字符串表示形式转换为其等效的DateTime对象是开发中很常见的类型转换,我们最常使用的方式是: // 如果s为null,抛出ArgumentNullException异常 // 如果s 不包含的有效字符串表示形式的日期和时间,抛出FormatException DateTime DateTime.Parse(string s); bool DateTime.TryParse(string s, out DateTime result); Date

java,时间转时间戳的转换以及各种date格式的转化

package com.henry.test; import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map; import org.apache.commons.lang.StringUtils; import com.shopin.core.util.HttpClientUtil; public class test { /** * 说明: * @pa

IOS-时间与字符串互相转换

有时会遇到这样的问题,需要把时间和时间戳互相转换 比如把当前时间转换成 "20140716155436"这样的格式 或者是把"20140716155436"转换成"2014-07-16 15:54:36" 首先来第一个: 当前时间转换成 "20140716155436"这样的格式 /////////////////////////////// //获取当前时间 NSDate * today = [NSDate date]; N

12.时间日期,字符串,内部类,拆装箱

一.String,StringBuffer,StringBuilder1.String是一个不可变的常量,每次修改都会产生新的对象2.StringBuffer,StringBuilder都是可变字符序列,是字符串的增强版.3.StringBuffer,StringBuilder作用:主要是当业务大量修改字符串的时使用4.区别:StringBuffer是线程安全,StringBuild非线程安全 二.自动拆装箱1.基本类型转换为封装类型为装箱过程,反之则是拆箱过程2.八种基本数据类型对应的封装类型