Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将“时间”转换为十进制年份(C+)+;_C++_Date_Time_Time T - Fatal编程技术网

C++ 将“时间”转换为十进制年份(C+)+;

C++ 将“时间”转换为十进制年份(C+)+;,c++,date,time,time-t,C++,Date,Time,Time T,如何将时间\u t结构转换为十进制年份 例如,对于日期2015-07-18 00:00:00,我希望获得2015.625使用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)

如何将
时间\u t
结构转换为十进制年份

例如,对于日期
2015-07-18 00:00:00
,我希望获得
2015.625

使用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);
格式:

%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).

您可以使用例如或将其转换为

分解的时间结构包含年份和一个
tm_yday
成员,即自1月1日起的天数。您可以使用此
tm_yday
成员计算小数点后的部分


如果您想要更高的分辨率,也可以使用小时、分钟和秒。

使用以下步骤:

  • 使用
    std::gmtime
    将年份与月份和日期分开
  • 计算
    X
    =一天和一个月中的天数(记住一年可以是);例如,2月3日的
    X
    将始终为
    31+3=34对于3月3日,闰年为
    31+28+3=62
    31+29+3=63
  • 计算
    Y
    =一年的天数
  • 使用以下公式计算百分比:
    (X*100.)/Y
    ,并以三位小数显示

在我的评论之后,我想了解更多关于你是如何来到
.625
的信息,我假设你的意思是
.55
,因为2015年7月18日是一年中的第199天

您需要首先使用
get_time
将时间从字符串获取到
std::tm
结构中。然后,在
mktime
的帮助下,我们应该能够获得一年中的某一天。接下来,我们可以进行快速计算,看看这一年是否是闰年,然后进行除法,得到我们的比率:

完整代码

包括 正确调用get_time 获取一年中的日期和天数 最后执行除法并打印结果值
double yearAsFloat=static_cast(年)+static_cast(天)/static_cast(天/年);

std::难道没有输出十进制年份的选项吗,%s是什么意思?%s表示您正在打印字符串您能描述一下如何获得
.625
?根据谷歌的说法,那是一年中的第199天,而且
199/365=0.545
memset是愚蠢的,你只是在前一天正确初始化了每个成员line@Cubbi:你说得对。我错误地认为它是可移植代码所需要的,因为我在没有它的VS中看到了一个问题,但是所说的问题是由于没有初始化到
{}
并且没有调用
memset
进行补偿而引起的。我编辑了这篇文章。
#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;
}