Use time. h library for dates

Asked

Viewed 1,185 times

-3

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

1 answer

2


To library time.h contains the following:

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

  • The guy clock_t representing processor cycle count.

  • The guy time_t which in general represents a Unix timestamp.

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

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

That is, none of the types made available by this library will be what you want, and therefore, 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 like date_type *.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.