简体   繁体   English

在C / C ++中将月/日/年/时间转换为时间

[英]convert month/day/year/time to timeval in C/C++

In C/C++, suppose I have the following values: 在C / C ++中,假设我有以下值:

int year = 2010;
int month = 6;
int day = 15;
int hour = 14;
int minute = 52;
int seconds = 34;
int microseconds = 141231;

What's the easiest way to convert this to a timeval? 将此转换为时间段的最简单方法是什么? I think timeval is time since Jan 1, 1970? 我认为timeval是自1970年1月1日以来的时间? But to calculate this manually everytime seems very tedious. 但每次手动计算这个似乎非常繁琐。 What's the easiest (and fastest) way to do this? 什么是最简单(也是最快)的方法?

Thanks 谢谢

You can use mktime(3) to convert a struct tm structure into a time_t . 您可以使用mktime(3)struct tm结构转换为time_t After you have the time_t , you can copy it and your microseconds value directly into a struct timeval structure. 获得time_t ,可以将它和您的微秒值直接复制到struct timeval结构中。

您可能想要查看std::mktime()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM