1 year ago

#210898

test-img

user18160927

difftime returns 0 in C++

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?

c++

datetime

mktime

difftime

0 Answers

Your Answer

Accepted video resources