简体   繁体   English

如何比较“月日期hh:mm:ss”格式的两个时间戳来检查+ ve或-ve值

[英]How to compare two time stamp in format “Month Date hh:mm:ss” to check +ve or -ve value

I checked the stackoverflow site for my answer, i did not get, so i am posting it here. 我检查了stackoverflow网站的答案,我没有得到,所以我在这里发布。

My problem is: 我的问题是:

How to compare two time stamp in format "Month Date hh:mm:ss" ? 如何比较两个时间戳的格式为"Month Date hh:mm:ss"

I am writing program in C and C++ and the time is in displayable string format. 我用C和C ++编写程序,时间是可显示的字符串格式。

Example : 示例:

time1 = "Mar 21 11:51:20"
time2 = "Mar 21 10:20:05"

I want to compare time1 and tme2 and find out whether time2 is after time1 or not and I need output as true or false , like below: 我想比较time1和tme2并找出time2是否 time1 之后 ,我需要输出为truefalse ,如下所示:

 if time2 > time1 then 
       i need output as 1 
 or 
       0 or -1 anything

I used difftime(time2,time1) , but it returns the delta time diff between time1 and time2 . 我使用了difftime(time2,time1) ,但是它返回了time1time2之间的delta时间diff
I want to check greater or not. 我想检查更大或没有。

For any help, thanks in advance 如有任何帮助,请提前感谢

FIRST- use difftime to compare: 首先使用difftime进行比较:

you can simply use difftime() function to compare time and return 1 or -1 as follows: 你可以简单地使用difftime()函数来比较时间并返回1-1 ,如下所示:

int comparetime(time_t time1,time_t time2){
 return difftime(time1,time2) > 0.0 ? 1 : -1; 
} 

SECOND- Convert string into time: SECOND-将字符串转换为时间:

If you have difficulty to convert string into time_t struct, you can use two functions in sequence: 如果您很难 string 转换 time_t结构,则可以按顺序使用两个函数:

  1. char *strptime(const char *buf, const char *format, struct tm *tm); function. 功能。 to convert string into struct tm 将字符串转换为struct tm

    Example: to convert date-time string "Mar 21 11:51:20 AM" into struct tm you need three formate strings: 示例:要将日期时间字符串"Mar 21 11:51:20 AM"转换为struct tm您需要三个合成字符串:

    %b : Month name, can be either the full name or an abbreviation %b :月份名称,可以是全名或缩写
    %d : Day of the month [1–31]. %d :每月的某一天[1-31]。
    %r : Time in AM/PM format of the locale. %r :区域设置的AM / PM格式的时间。 If not available in the locale time format, defaults to the POSIX time AM/PM format: %I:%M:%S %p . 如果在区域设置时间格式中不可用,则默认为POSIX时间AM / PM格式: %I:%M:%S %p

  2. time_t mktime (struct tm * timeptr); function to convert struct tm* to time_t struct tm*转换为time_t函数

Below Is my example program: 以下是我的示例程序:

#include <stdio.h>
#include <time.h>
int main(void){
    time_t t1, t2;
    struct tm *timeptr,tm1, tm2;
    char* time1 = "Mar 21 11:51:20 AM";
    char* time2 = "Mar 21 10:20:05 AM";

    //(1) convert `String to tm`:  
    if(strptime(time1, "%b %d %r",&tm1) == NULL)
            printf("\nstrptime failed\n");          
    if(strptime(time2, "%b %d %r",&tm2) == NULL)
            printf("\nstrptime failed\n");

    //(2)   convert `tm to time_t`:    
    t1 = mktime(&tm1);
    t2 = mktime(&tm2);  

     printf("\n t1 > t2 : %d", comparetime(t1, t2));
     printf("\n t2 > t1 : %d", comparetime(t2, t1));
     printf("\n");
     return 1;
}

And it works as you desire: 它可以按你的意愿工作:

 $ ./a.out 
 t1 > t2 : 1
 t2 > t1 : -1

To calculate difference between two dates read: How do you find the difference between two dates in hours, in C? 要计算两个日期之间的差异,请阅读: 如何找到两个日期之间的差异,以小时为单位?

Look at strptime() 看看strptime()

It converts an ASCII string formatted date/time into a struct tm 它将ASCII字符串格式的日期/时间转换为struct tm

Here is a very easy way solution to solve your problem. 这是解决问题的一种非常简单的解决方案。 If in your case the comparing depends on the time. 如果在您的情况下,比较取决于时间。 If so, you can turn your time variables into string so you can compare them as a string. 如果是这样,您可以将时间变量转换为字符串,以便将它们作为字符串进行比较。 ONLY if the base of the comparison is the hour and minute and second elements and/or the day. 仅当比较的基础是小时和分钟和第二元素和/或日期时。 It will look like this: 它看起来像这样:

string time1 = "Mar 21 11:51:20";
string time2 = "Mar 21 10:20:05";

cout<<time1<< endl;
cout<<time2<< endl;
cout<<endl;

if(time1>time2)cout<<time1<< endl;
else cout << time2 << endl;

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

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