开发者

Date comparison to find which is bigger in C

开发者 https://www.devze.com 2023-02-16 22:03 出处:网络
I want to know how to find which is bigger date using a C program kindly开发者_StackOverflow社区 help me out plz....You can use the difftime function:

I want to know how to find which is bigger date using a C program

kindly开发者_StackOverflow社区 help me out plz....


You can use the difftime function:

#include <time.h>
#include <stdio.h>

int main(void) {
  time_t date1, date2;
  // initialize date1 and date2...

  double seconds = difftime(date1, date2);
  if (seconds > 0) {
    printf("Date1 > Date2\n");
  }

  return 0;
}

If your dates are not of type time_t, you can use the function mktime to convert them.


#include <stdio.h>

struct date 
{
   int month;
   int date;
   int year;
};


int main(void)
{
    int i=compare_dates (struct date d1, struct date d2);
    switch(i)
    {
       case -1:
         printf("%d/%d/%d is earlear date than %d/%d %d", D1.day, D1.month, D1.year, D2.day
       case 1: 
         printf("%d/%d/%d is later date than %d/%d/%d",D1.day,D1.month,D1.year,D2.day…
       case 0: 
         printf("%d/%d/%d is the same date than %d/%d/%d", D1.day, D1.month, D1.year, D2.day
     }
   return 0;
}


int compare_dates (struct date d1, struct date d2)
{
    if (d1.year < d2.year)
       return -1;

    else if (d1.year > d2.year)
       return 1;

    if (d1.year == d2.year)
    {
         if (d1.month<d2.month)
              return -1;
         else if (d1.month>d2.month)
              return 1;
         else if (d1.day<d2.day)
              return -1;
         else if(d1.day>d2.day)
              return 1;
         else
              return 0;
    }
}


If you just want to know which is bigger, you don't need go through all this. You can just prioritize the values and compare them. Just add coefficients to month and year that is bigger than highest day possible. For example, say that a month is 100 times more important than the day and a year is 2000 times more important than a day. Just calculate the score of the dates and compare them.

#include <stdio.h>

int main()
{
    int day1, day2, month1, month2, year1, year2;

    printf("Enter first date (dd/mm/yyyy) => "); scanf("%d/%d/%d", &day1, &month1, &year1);
    int prioritedScore1 = day1 + month1*100 + year1*2000;
    printf("Enter second date (dd/mm/yyyy) => "); scanf("%d/%d/%d", &day2, &month2, &year2);
    int prioritedScore2 = day2 + month2*100 + year2*2000;

    if(prioritedScore1 > prioritedScore2){
        printf("Bigger date is => %d/%d/%d", day1, month1, year1);
    } else if(prioritedScore2 > prioritedScore1){
        printf("Bigger date is => %d/%d/%d", day2, month2, year2);
    } else{
        printf("Dates are same.");
    }

    return 0;
}

You can always use a long integer in order to get rid of possible bugs when user enters so high numbers like 2147484 as year.


Can you give more information about what you want to achieve ? Because comparing date is really easy. After all, they are just number of seconds (or milli, micro, nano, ...) since a given past date, or a structure containing year, month, day, ... Whatever the format, the comparison should be pretty easy to perform.

Maybe you want to compare two date given by the user as strings (something like "2011-03-12 18:38") ? Then, you can use strptime to convert the string to a struct tm, and then do the comparison.

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int parse_date(char* date, struct tm* tm)
{
    char* format;
    char* formats[] = {
        "%F %I", /* "2011-03-12 06:38:05 AM" */
        "%F %T", /* "2011-03-12 18:38:05" */
        "%F %R", /* "2011-03-12 18:38" */
        NULL,
    };

    for (format = formats[0]; format; ++ format) {
        if (strptime(date, format, &tm)) {
            return 1;
        }
    }

    return 0;
}

int main(int argc, char** argv)
{
    float diff;

    char* date1;
    char* date2;

    struct tm tm1;
    struct tm tm2;

    time_t time1;
    time_t time2;

    if (argc != 3) {
        fprintf(stderr, "usage: compare-date date1 date2\n");
        exit(1);
    }

    date1 = argv[1];
    date2 = argv[2];

    if (!parse_date(date1, &tm1)) {
        fprintf(stderr, "unsupported date: %s\n", date1);
        exit(1);
    }

    if (!parse_date(date2, &tm1)) {
        fprintf(stderr, "unsupported date: %s\n", date2);
        exit(1);
    }

    time1 = mktime(&tm1);
    time2 = mktime(&tm2);
    diff = difftime(time1, time2);

    printf("%s %c %s\n",
        date1,
        (diff < 0 ? '<' : (diff > 0 ? '>' : '==')),
        date2);

    return 0;
}


You didn't say in which format you have the date, so I will name two common examples:

  • If you are using GNU lib-c (or MinGW) and query the time with:

    time_t time (time_t *result)
    

    Then time_t is just a long int, numbers of seconds since epoch and you can subtract one date from the other to find out the number of seconds difference.

  • If you are using the Windows API and have a filetime-structure:

    typedef struct _FILETIME {
        DWORD dwLowDateTime;
        DWORD dwHighDateTime;
    } FILETIME, *PFILETIME;
    

    you can cast the pointer to a pointer to ULARGE_INTEGER, and subtract that one giving you the number of 100-nanosecond intervals difference.

0

精彩评论

暂无评论...
验证码 换一张
取 消