How to declare a date variable in C?

13

I need to create an abstract type of data that represents a person, including name, date of birth and CPF, and create a variable that is a pointer to this TAD (in the main program).

To a certain extent I know, but I do not know which is the right kind to put in the place of "?".

struct pessoa
{
    char nome;
    double cpf;
    ?   Data_Nasc;
}x,*ptr;
ptr = &x;
    
asked by anonymous 04.05.2016 / 18:48

5 answers

4
The solution pointed out by colleague @bigown from a data abstraction point of view is valid, but he reinvented the wheel because the standard library already has all the date and time abstractions and all the functions needed to manipulate it, the

The most appropriate to replace the ? would be the type time_t defined by the default library time.h . This type represents the number of seconds that has passed since the date of January 1, 1970 at 00:00:00 UTC, is considered the zero mark of the calendar system used by computers.

  

Although the Gregorian calendar facilitates chronological reasoning   for humans, when it is desired to make logical or   calculations with dates on computers, this type of calendar   making work difficult.

     

For example, for us, to know what happened first, if something went in   10/04/1977 12:45:15 or something on 10/03/1976 13:09:12 is something almost   automatic but to solve this on a computer all 6 fields   independently, although   almost instantaneously, it is a   extra that the processor could avoid if it made use of another format   of date. Imagine a database with thousands of records and the   processor getting a command to put everything in order   chronological, if we can make the comparisons with a single operation   per record instead of 6 operations / record the final time also   will tend to be 6 times smaller.

The following example illustrates the conversion of a value of type time_t to a human readable date and time:

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


int main( int argc, char * argv[] )
{
    time_t agora;
    char datahora[100];

    /* Recupera a quantidade de segundos desde 01/01/1970 */
    agora = time(NULL);

    /* Formata a data e a hora da forma desejada */
    strftime( datahora, sizeof(datahora), "%d.%m.%Y - %H:%M:%S", localtime( &agora ) );

    printf( "Data/Hora: %s\n", datahora );

    return 0;
}

Already another, it illustrates how to read a date and time in human format for the number of seconds from zero:

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

char datahora[] = "01.01.2016 - 12:00:00";  

int main( int argc, char * argv[] )
{
    struct tm tm; 
    int ano, mes; 

    sscanf( datahora, "%d.%d.%d - %d:%d:%d", &tm.tm_mday, &mes, &ano, &tm.tm_hour, &tm.tm_min, &tm.tm_sec );

    tm.tm_year = ano - 1900;
    tm.tm_mon = mes - 1;

    printf( "Segundos desde 01/01/1970: %ld\n", mktime( &tm ) );

    return 0;
}

The best part is when we need to compare dates, we can use comparison operators, in the same way we compare integers:

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

char datahora1[] = "12.01.2000 - 12:00:00"; 
char datahora2[] = "01.01.2015 - 13:10:20";  

int main( int argc, char * argv[] )
{
    time_t t1;
    time_t t2;

    struct tm tm; 
    int ano, mes; 

    sscanf( datahora1, "%d.%d.%d - %d:%d:%d", &tm.tm_mday, &mes, &ano, &tm.tm_hour, &tm.tm_min, &tm.tm_sec );

    tm.tm_year = ano - 1900;
    tm.tm_mon = mes - 1;

    t1 = mktime( &tm );

    sscanf( datahora2, "%d.%d.%d - %d:%d:%d", &tm.tm_mday, &mes, &ano, &tm.tm_hour, &tm.tm_min, &tm.tm_sec );

    tm.tm_year = ano - 1900;
    tm.tm_mon = mes - 1;

    t2 = mktime( &tm );

    if( t1 < t2 )
    {
        printf( "Data1 esta antes de Data2!\n");
    }
    else if( t1 > t2 )
    {
        printf( "Data1 esta depois Data2!\n");
    }
    else
    {
        printf( "Data1 eh iguaal a Data2!\n");
    }

    return 0;
}

I hope I have helped!

    
08.05.2016 / 10:43
12

You do not do this. C is not a language that facilitates both abstractions. Of course, nothing prevents you from creating a type to save and manipulate dates, as you did in your previous question . Although the name probably is not appropriate structure and does not have ancillary functions to manipulate the structure, this is basically what should be done: create a structure, which is a type composed of members, and use it inside another structure when necessary .

Outside this can use some kind of ready library, but for learning purposes I do not find it suitable.

A base not wanting to be complete, optimized to the maximum, would be:

typedef struct {
    int ano;
    short mes;
    short dia;
} Data
struct pessoa {
    char nome;
    double cpf;
    Data data_nasc;
} x,*ptr;

I do not quite understand why using double as a CPF. The correct one would be char * or char[12] , but at worst, if you have not yet learned to mess with it, be a long . The name is also wrong, or it would have to be a pointer or an array . The way it's only allows 1 character in the name.

    
04.05.2016 / 18:59
1

Note that it is not necessary for each data to be mapped to a variable in its structure. With this in mind, a simple solution is to have one variable for day, another for month plus one for year of birth.

struct pessoa
{
    char nome;
    double cpf;
    int dia_de_nascimento;
    int mes_de_nascimento;
    int ano_de_nascimento;
}

Note: The type of the nome field in your structure is probably wrong, unless your program only accepts one-character names.

    
04.05.2016 / 18:59
1

C has a data type that holds dates. Uilque mentioned the library in his response (time.h). Here is the official documentation in Portuguese:

link

You have an example of getting a current date on the original in English :

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

int main(void)
{
    struct tm start = {.tm_mday=1};
    mktime(&start);
    printf("%s\n", asctime(&start));
}

If you are doing this to learn on your own, I suggest starting with easier language such as C #, Java or Javascript (unless you already master them and really want to dive into C). Now if this is for a college or college assignment ... Your teacher is a madman or hangman. Continue to study to pass, but try to learn easier language like the ones I mentioned. The date and time structures in these languages are much easier to use for the beginning.

    
04.05.2016 / 20:33
0

According to this example in Wikipedia , it would look like this:

#include <time.h>

int main(void)
{
    time_t dataEHorario;

    // Você ainda pode setar a data para o atual do sistema
    current_time = time(NULL);

    exit(EXIT_SUCCESS);
}
    
04.05.2016 / 18:58