- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章time_t tm timeval 和 时间字符串的转换方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、常用的时间存储方式 。
1)tml">time_t类型,这本质上是一个长整数,表示从1970-01-01 00:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。 。
2)tm结构,这本质上是一个结构体,里面包含了各时间字段 。
1
2
3
4
5
6
7
8
9
10
11
|
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。 。
3)struct timeval结构体在time.h中的定义为 。
1
2
3
4
|
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
|
2、常用的时间函数 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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下时间转字符串参考代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
time_t t; //秒时间
tm* local; //本地时间
tm* gmt; //格林威治时间
char buf[128]= {0};
t = time(NULL); //或者time(&t);//获取目前秒时间
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字符串转时间参考代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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来格式化 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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_; //秒时间
}
|
4)timeval获取时间示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
struct timeval start_time, over_time, consume_time;
gettimeofday(&over_time, NULL);//get the current time
start_time = over_time;
do something.....
gettimeofday(&over_time, NULL);
timeval_subtract(&consume_time, &start_time, &over_time);//计算时间差104./**
* 计算两个时间的间隔,得到时间差
* @param struct timeval* resule 返回计算出来的时间
* @param struct timeval* x 需要计算的前一个时间
* @param struct timeval* y 需要计算的后一个时间
* return -1 failure ,0 success
**/
timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y)
{
if ( x->tv_sec>y->tv_sec )
return -1;
if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) )
return -1;
result->tv_sec = ( y->tv_sec-x->tv_sec );
result->tv_usec = ( y->tv_usec-x->tv_usec );
if (result->tv_usec<
0
)
{
result->tv_sec--;
result->tv_usec+=1000000;
}
return 0;
}
|
4、关于localtime与localtime_r的区别 。
struct tm *localtime(const time_t *timep),
这个函数在返回的时候,返回的是一个指针,实际的内存是localtime内部通过static申请的静态内存,所以通过localtime调用后的返回值不及时使用的话,很有可能被其他线程localtime调用所覆盖掉 。
struct tm *localtime_r(const time_t *timep, struct tm *result),
localtime_r则是由调用者在第二个参数传入一个struct tm result指针,该函数会把结果填充到这个传入的指针所指内存里面;成功的返回值指针也就是struct tm result.
多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的.
其他的时间函数,如asctime,asctime_r;ctime,ctime_r;gmtime,gmtime_r都是类似的,所以,<strong>时间函数的 _r 版本都是线程安全的。</strong> </span> 。
以上这篇time_t tm timeval 和 时间字符串的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
最后此篇关于time_t tm timeval 和 时间字符串的转换方法的文章就讲到这里了,如果你想了解更多关于time_t tm timeval 和 时间字符串的转换方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!