[英]Convert `time_t` to decimal year in C++
How can I convert a time_t
structure to years as decimal? 如何将time_t
结构转换为十进制年份?
For example, for a date 2015-07-18 00:00:00
I would like to get 2015.625
. 例如,对于2015-07-18 00:00:00
的日期,我想获得2015.625
。
Following my comment seeking more information about how you came to .625
, I will assume that you actually meant .55
because July 18th, 2015 is the 199th day of the year. 在我的评论中寻求关于你如何来到.625
更多信息,我将假设你实际上意味着.55
因为2015年7月18日是一年的第199天。
You need to first use get_time
to get time from a string into a std::tm
structure. 您需要首先使用get_time
从字符串中获取时间到std::tm
结构。 Then, with some help from mktime
we should be able to get the day of the year. 然后,在mktime
帮助下,我们应该能够获得一年中的这一天。 Following that, we can perform a quick calculation to see if the year is a leap year, and then perform division to get our ratio: 接下来,我们可以执行快速计算以查看年份是否为闰年,然后执行除法以获得我们的比率:
#include <ctime>
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <string.h>
int main()
{
std::tm theTime = {};
// initialize timeToConvert with the Year-Month-Day Hours:Minutes:Seconds string you want
std::string timeToConvert = "2015-07-18 00:00:00";
std::istringstream timeStream(timeToConvert);
// need to use your locale (en-US)
timeStream.imbue(std::locale("en_US.UTF-8"));
timeStream >> std::get_time(&theTime, "%Y-%m-%d %H:%M:%S");
if (timeStream.fail())
{
std::cerr << "Parse failed\n";
exit(0);
}
// call mktime to fill out other files in theTime
std::mktime(&theTime);
// get years since 1900
int year = theTime.tm_year + 1900;
/* determine if year is leap year:
If the year is evenly divisible by 4, go to step 2. ...
If the year is evenly divisible by 100, go to step 3. ...
If the year is evenly divisible by 400, go to step 4. ...
The year is a leap year (it has 366 days).
The year is not a leap year (it has 365 days).
*/
bool isLeapYear = year % 4 == 0 &&
year % 100 == 0 &&
year % 400 == 0;
// get number of days since January 1st
int days = theTime.tm_yday+1; // Let January 1st be the 1st day of year, not 0th
// get number of days in this year (either 365 or 366 if leap year)
int daysInYear = isLeapYear ? 366 : 365;
double yearAsFloat = static_cast<double>(year) + static_cast<double>(days)/static_cast<double>(daysInYear);
std::cout << timeToConvert << " is " << yearAsFloat << std::endl;
}
Output: 输出:
2015-07-18 00:00:00 is 2015.55 2015-07-18 00:00:00是2015.55
You could convert it into a broken down time using eg std::gmtime
or std::localtime
. 您可以使用例如std::gmtime
或std::localtime
将其转换为细分时间 。
The broken down time structure contains the year, and a tm_yday
member which is the days since January 1. You could use this tm_yday
member to calculate the part after the decimal point. 细分时间结构包含年份, tm_yday
成员是自1月1日以来的日期。您可以使用此tm_yday
成员计算小数点后的部分。
If you want higher resolution, use the hours, minutes and seconds too. 如果您想要更高的分辨率,请使用小时,分钟和秒。
Use strftime 使用strftime
struct stat info;
char buff[20];
struct tm * timeinfo;
stat(workingFile, &info);
timeinfo = localtime (&(info.st_mtime));
strftime(buff, 20, "%b %d %H:%M", timeinfo);
printf("%s",buff);
Formats: 格式:
%b - The abbreviated month name according to the current locale.
%d - The day of the month as a decimal number (range 01 to 31).
%H - The hour as a decimal number using a 24-hour clock (range 00 to 23).
%M - The minute as a decimal number (range 00 to 59).
Use the following steps: 使用以下步骤:
std::gmtime
. 使用std::gmtime
将月份与月份分开。 X
= number of days in the day and month (keeping in mind that the year could be a leap year ); 计算X
=日期和月份的天数(请记住,年份可能是闰年 ); For example, for the 3rd of February X
will always be 31 + 3 = 34;
例如,对于2月3日, X
总是31 + 3 = 34;
For 3rd of March, it would be 31 + 28 + 3 = 62
or 31 + 29 + 3 = 63
for leap years; 3月3日,闰年为31 + 28 + 3 = 62
或31 + 29 + 3 = 63
; Y
= the number of days for the year 计算Y
=一年中的天数 (X * 100.) / Y
and display it with three decimals 使用公式计算百分比: (X * 100.) / Y
并显示三位小数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.