Use the time.h library for dates

-3

I wanted to use the time.h library to store a birth date type 26/10/2312 (in this format) in a given array, but I've never worked with this library. Can someone give me an explanation?

    
asked by anonymous 09.01.2018 / 15:35

1 answer

2

The library time.h contains the following:

  • The type size_t , which is the resulting type of the sizeof operator, and represents an amount in bytes. This type is also defined in several other libraries.

  • The type clock_t that represents processor cycle count.

  • The type time_t which typically represents a unix timestamp .

  • A struct tm , which is a structure used to obtain the system date and time.

  • Other functions and macros that receive or return data with the above formats.

In other words, none of the types provided by this library will be what you want, so it is best to create your own:

typedef struct date_type {
    int dia;
    int mes;
    int ano;
} date_type;

With this, you can create arrays like this:

date_type[20] minhas_datas; /* Array de 20 posições do tipo date_type. */

Or use pointers of type date_type * .

You can do functions that work with date_type to check if a date is valid, build a date, calculate the day of the week, integrate with time_t and struct tm , etc. I recommend reading the in this answer .

You can also use the strftime function along with a sscanf to work with time_t directly if you want.

    
09.01.2018 / 17:43