2013-02-01 134 views
-2

如何比较格式为yyyy-mm-dd hh-mm-ss的两个时间戳以获得总分钟差异?时间戳在分钟之间的差异

时间戳从MySQL中检索。到目前为止,我已经尝试过使用time_t,甚至分解整个字符串进行比较,但后者的问题是它不能识别天的差异。

在此先感谢!

edit1: 通过比较我需要检查差异是否大于x分钟的时间戳。

,如:

timestamp1 = getTimestampFunction1(); 
timestamp2 = getTimestampFunction2(); 

difference = timestamp1 - timestamp2; //difference in minutes 

if (difference > 60) 
{ 
    do this; 
} 
else 
{ 
    do that; 
} 
+2

您可以通过编写一些C++开始。 –

+0

现在你想要什么?分钟差异?天差异?或只是比较? –

+0

对不起,没有睡了2天,阅读大量的文章也没有帮助:p – Gerdinand

回答

0

这可以用升压完成

#include <boost/date_time/local_time/local_time.hpp> 

#include <string> 
using namespace std; 
using namespace boost::local_time; 
using namespace boost::posix_time; 

class DateTimeDiff { 
    istringstream ss; 
    local_time_input_facet* facet; 
public: 
    DateTimeDiff(char const * format) 
    : facet(new local_time_input_facet(format)) { 
     ss.imbue(locale(ss.getloc(), facet)); 
    } 
    double delta_minutes(string t0, string t1) { 
     local_date_time ldt0(not_a_date_time), ldt1(not_a_date_time); 
     ss.str(t0); ss >> ldt0; 
     ss.str(t1); ss >> ldt1; 
     return time_duration(ldt1 - ldt0).total_seconds()/60.0; 
    } 
}; 

int main() { 
    DateTimeDiff dt("%Y-%m-%d %H-%M-%S"); 

    const string t0 = "2013-01-02 13-14-27", 
       t1 = "2013-02-01 12-56-09"; 

    double diff = dt.delta_minutes(t0,t1); 

    cout << t0 << " and " 
     << t1 << " are " 
     << diff << " minutes apart." 
     << endl; 

    cout << diff << " minutes is "; 
    if (diff > 60) { 
     cout << "more than"; 
    } else { 
     cout << "less than or equal to"; 
    } 
    cout << " one hour." << endl; 

    return 0; 
} 

输出是

2013-Jan-02 13:14:27 UTC and 2013-Feb-01 12:56:09 UTC are 43181.7 minutes apart. 
43181.7 minutes is more than one hour. 
+0

真棒!没有尝试之前(因为我不想使用其他库),现在它的工作就像一个魅力。谢谢!我确实有很多使用boost的“隐式转换失去整数精度”。任何方式我可以解决这个问题? – Gerdinand

+0

也许你正在使用Apple LLVM/Xcode?我发现这个参考文献 https://svn.boost.org/trac/boost/ticket/6635 我没有得到gcc 4.6.3的警告。 – amdn

+0

关闭有关隐式转换的警告“已修复”此:) – Gerdinand

2

你提到两/三的问题,目前还不清楚那些你有什么实际的问题。

如果你只是想比较于其中,你知道,他们有你提到的格式,你可以做一个简单的字符串比较日期:

const std::string A = "2012-11-11 01-01-59", 
        B = "2011-11-11 01-01-59"; 

if (A < B) {} // A lies before B 
if (A > B) {} // A lies after B 

这工作,因为两个字符串的长度相同,以及数字从最重要到最不重要的顺序排列。

0

怎么是这样的:

char d1[] = "2013-02-01 12:56:09"; 
char d2[] = "2013-01-02 13:14:27"; 

char *parts1[6]; 
char *parts2[6]; 
char *p1, *p2; 

for(int i = 0; i < 6; i++) 
{ 
    parts1[i] = strtok_r(i?0:d1, "-: ", &p1); 
    parts2[i] = strtok_r(i?0:d2, "-: ", &p2); 
} 

struct tm t1, t2; 

t1.year = strtol(parts1[0], 0, NULL); 
t1.month = strtol(parts1[1], 0, NULL); 
t1.day = strtol(parts1[2], 0, NULL); 

t1.hour = strtol(parts1[3], 0, NULL); 
t1.hour = strtol(parts1[4], 0, NULL); 
t1.hour = strtol(parts1[5], 0, NULL); 

t2.year = strtol(parts2[0], 0, NULL); 
t2.month = strtol(parts2[1], 0, NULL); 
t2.day = strtol(parts2[2], 0, NULL); 

t2.hour = strtol(parts2[3], 0, NULL); 
t2.hour = strtol(parts2[4], 0, NULL); 
t2.hour = strtol(parts2[5], 0, NULL); 

time_t tt1, tt2; 

tt1 = mktime(&t1); 
tt2 = mktime(&t2); 

double diff = difftime(tt1, tt2)/60; // Seconds -> make it minutes.