How to work with Dates in C?

0

Talk to your people! I got a question here on the internet to make it come out nothing at all. It asks for the following: Do you want to make a program that reads the name of the book that will be borrowed, the type of (student or teacher) and the book's loan date. The program should teacher has ten days to return the book and the student only five days. If any of them returns with delay, the program shall calculate the fine for that delay considering the amount of R $ 0.50 per day of delay The program should read the effective date of the return and at the end of the execution, it should print a receipt as shown below:

Book name: XXXXX

User Type: XXXXX

Loan date: XX / XX / XXXX

Due date of return: XX / XX / XXXX

Date of return: XX / XX / XXXX

Fine for delay: R $ XXX, XX

-Book Name: -User Type: -Date of loan: -Date of return: -Date of return: -Make it late:

My biggest problem is with the dates because I do not know how to operate them.

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

int main(){
    setlocale(LC_ALL, "portuguese");
    char usuario[10], livro[30];
    int dia, mes, ano;

        printf("Insira o nome do livro: ");
        scanf("%s", &livro);
        printf("Insira o tipo do usuário: ");
        scanf("%s", &usuario);
        printf("Insira a data do emprestimo: ");
        scanf("%d%*c%d*c%d", &dia, &mes, &ano);



            if((usuario[0]=='A')&&(usuario[1]=='L')&&(usuario[2]=='U'))
            {
            Nessa função "if" eu iria colocar um for para contar os dias de 
            atraso entre data de emprestimo e data da devolução, por exemplo:          

            dataemp=dataemp+5 //Pois alunos tem 5 dias para devolver    
            for(i = 0; dataemp<=datadev; i++)
            multa = i * 0.50;


            }
            if((usuario[0]=='P')&&(usuario[0]=='R')&&(usuario[0]=='O'))
            {

            dataemp=dataemp+10 //Pois professor tem 10 dias para devolver    
            for(i = 0; dataemp<=datadev; i++)
            multa = i * 0.50;

            }


}

How can I get the user's date, subtract those dates, and add days to the date entered by the user? I read about time.h but I did not quite understand it to the point of implementing my code with it. Fuck, seriously, 4 days trying to do this and do not step from the algorithm I wrote above.

    
asked by anonymous 15.04.2017 / 14:24

1 answer

1

In C, there are no actual dates. The type that makes up the date in C is actually a int which counts the number of seconds elapsed since January 1, 1970 UTC and the date in question . But to parse a date of type DD/MM/YYYY , you have to use another type and another function of <time.h> , which is the mktime() .

mktime() gets a structure type struct tm and returns the corresponding time_t :

time_t
ler_data(const char data[11]) {
    struct tm tm = { 0 }; /* zera a struct toda */

    if (sscanf(data, "%d/%d/%d", &tm.tm_mday, &tm.tm_mon, &tm.tm_year) < 3) {
        fprintf(stderr, "Erro de formatação ao ler data %s\n", data);
        exit(1);
    }
    tm.tm_mon --; /* tm.mon espera o número de meses desde janeiro (0–11) */
    tm.tm_year -= 1900; /* tm.year espera o número de anos desde 1900 */

    return mktime(&tm);
}

Given this, the difference between dates A and B in days is (B - A) / 86400 , since there is 24 × 60 × 60 = 86,400 seconds in a day. In fact it is better to #define SEGUNDOS_EM_UM_DIA 86400 and then say (B - A) / SEGUNDOS_EM_UM_DIA . Also, to add n days to date A you A + n * SEGUNDOS_EM_UM_DIA .

To make the reverse path, you'll have to write the date in a buffer using localtime() to convert time_t to struct tm and then strftime() or snprintf() to convert struct tm to string:

void
escrever_data(char * buf, size_t len, time_t data) {
    struct tm * tm = localtime(&data);
    buf[0] = '
time_t
ler_data(const char data[11]) {
    struct tm tm = { 0 }; /* zera a struct toda */

    if (sscanf(data, "%d/%d/%d", &tm.tm_mday, &tm.tm_mon, &tm.tm_year) < 3) {
        fprintf(stderr, "Erro de formatação ao ler data %s\n", data);
        exit(1);
    }
    tm.tm_mon --; /* tm.mon espera o número de meses desde janeiro (0–11) */
    tm.tm_year -= 1900; /* tm.year espera o número de anos desde 1900 */

    return mktime(&tm);
}
'; if (snprintf(buf, len, "%02d/%02d/%04d", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900) < 10) { fprintf(stderr, "Erro de formatação ao escrever data 0x%08X\n", data); exit(1); } }
    
17.04.2017 / 20:57