Marco Basolu
Marco Basolu

Reputation: 41

How to comparison two date without seconds in C

I want to comparison two dates, one at the current datetime and one memorized in a list. If these dates have the same year, month and day, I add a value to the old date in the list: if not, I create a new elements in the list.

struct entry{
time_t date;
int quantity;
struct entry* next;
};

I call the find_list, to find and compare the current date and the date memorized

    int find_list(struct entry* head, time_t data, int quantita){

    struct tm* data_value, *data_list;  
    struct entry* pointer = head;
    time(&data);    
    data_value = gmtime(&data); 

    while(pointer){     
        data_list = gmtime(&pointer->date);
        //printf("Tempo value:%s\nTempo lista: %s\n", asctime(data_value), asctime(data_list));
        if(data_value->tm_year == data_list->tm_year && .....){
            pointer->quantity += quantita;
            return 1; 
        }
        pointer = pointer->next;        
    }
    return 0;   
};

but if I printf the two value, I print equals value

Upvotes: 0

Views: 69

Answers (1)

pmg
pmg

Reputation: 108978

gmtime() returns a pointer. Consecutive calls to gmtime() (all returning a pointer) may return a pointer to the same memory.

Copy the memory pointed to to a real object.

struct tm data_list;                 // not a pointer!
data_list = *gmtime(&pointer->data); // copy value

Note: POSIX introduced gmtime_r(), which behaves as expected.

// for POSIX systems
struct tm data_list;                  // still not a pointer
gmtime_r(&pointer->data, &data_list);

Upvotes: 4

Related Questions