1

I am trying to calculate the elapsed time between the date a book was borrowed and today, but difftime always returns 0.

This is what I have :

#include <string>
#include <iostream>
#include <locale>
#include <stdio.h>
#include <ctime>
#include <time.h>
#include "library.h"
using namespace std;

int max_inventory = 3;
int i;

void Menu(){
  book_inventory book[3];
  book[0] = {1, "Lord of the Flies", 1954, "Golding, William", "2022-02-01", 0001, false};
  book[1] = {2, "The Fault in Our Stars", 2012, "Green, John", "2022-01-28", 0002, true};
  book[2] = {3, "Dune", 1965,  "Herbert, Frank", "2022-01-28", 0001, false};


  struct tm* b_date;
  int i;

  //getting current date/time
  struct tm  today = {0};
  struct tm lt = {0};

  time_t now;
  time(&now); // get current time; same as: now = time(NULL)
  lt = *localtime(&now);

  //Defining "today" - making sure it is in the current year and current month
  today.tm_year = 1900 + lt.tm_year;
  today.tm_mon = 1 + lt.tm_mon;
  today.tm_mday =  lt.tm_mday;
  today.tm_hour = 0;
  today.tm_min = 0;
  today.tm_sec = 0;

  std::cout << "\nToday's date:" << today.tm_year <<"."<< today.tm_mon <<"."  << today.tm_mday << endl;

  tm tm1 = {0};

  std::cout << "\nList of books checked out\n" << endl;

  for (i = 0; i < 3; i++){
    if (book[i].availability == false){ //if a book is borrowed, the book title is displayed
      std::cout << book[i].title << endl;

      const char* lstbrddate_str = book[i].lastborroweddate;
       //getting the dates from the string
      sscanf(lstbrddate_str, "%4d-%2d-%2d", &tm1.tm_year,&tm1.tm_mon,&tm1.tm_mday);
      std::cout << "Date checked out:" << tm1.tm_year <<"."<< tm1.tm_mon <<"."  << tm1.tm_mday << endl;
      tm1.tm_sec = 0;
      tm1.tm_min = 0;
      tm1.tm_hour = 0;

      long long b_days;

      b_days = difftime(mktime(&today), mktime(&tm1)); //calculating the elapsed time between today and tm1
      printf("Checked out for %lld seconds\n\n", b_days);
    }
  }
}


int main() {
  Menu();
     return 0;
  }

and here is library.h:

#include <iostream>
#include <string>
#include <locale>
#include <ctime>

using namespace std;

struct book_inventory {
  int id;
  string title;
  int year;
  string author;
  const char* lastborroweddate;
  int lastborrowerid;
  bool availability;
};

The result I get is the following :

Today's date:2022.2.22

List of books checked out

Lord of the Flies
Date checked out:2022.2.1
Checked out for 0 seconds

Dune
Date checked out:2022.1.28
Checked out for 0 seconds

From what I've read I suspect it might be an error with mktime though I am uncertain. What am I doing wrong?

  • Cannot reproduce, I get values: https://godbolt.org/z/3EPdYbv5Y , a correct one for the `2022.2.1`, but a wrong one for the `2022.1.28`. – mch Feb 22 '22 at 12:33
  • 1
    From the documentation, the return from mktime is `A time_t value corresponding to the calendar time passed as argument. If the calendar time cannot be represented, a value of -1 is returned.`. So if you're getting a 0 then if might be failing both times. Use a debugger to see what mktime is returning. – Skizz Feb 22 '22 at 12:34
  • Probably have to specify what OS is used. Also is is possible UB because use of `using namespace std`. There are functions and types defined in `ctime` AND in `time.h`, ones are POSIX versions , others are ISO. Not guaranteed that they use same definition. On POSIX difftime might be a macro. – Swift - Friday Pie Feb 22 '22 at 13:13
  • @Skizz you're right, I get -1 for both `mktime(&today)` and `mktime(&tm1)` – user18160927 Feb 22 '22 at 13:36
  • You are setting the `tm_year` incorrectly. As a result, `today` is the year 5822 and `tm1` is the year 3922. Both are wildly out of range, so `mktime` returns `-1`. (You are also setting the month incorrectly in `tm1`.) – Raymond Chen Feb 22 '22 at 15:08

0 Answers0