Proper uses of data type time_t and struct tm

[hr]I am writing command-line programs in C. Given the following...

[code]#include

#define MAX 200

struct mystruct {
/* Various structure members here */
struct tm date; /* Following members of tm used: tm_mday, tm_mon,
tm_year. This data is [italic]not[/italic]
obtained from time(), [italic]but is entered
by the user[/italic]. Think of this as part
of a stamp collection program, for example.
The user would have to [italic]enter[/italic]
the "date of issue", since this would have
nothing at all to do with the system time. */
} data[MAX];[/code]

...I later realized that (I think) [blue]struct tm[/blue] is not generally used for [italic]storing[/italic] time-related data, but rather for [italic]using[/italic] it (display date/time, etc.). To [italic]store[/italic] time-related data, isn't the data type [blue]time_t[/blue] generally more appropriate?

Yes, I know that in the kind of situation above (user-entered data) one might just as well use integers instead of anything provided by [blue]time.h[/blue] (i.e., declare [blue]int date, int month, int year[/blue]). But from a pedagogical perspective, I'm trying to get in the habit of learning and sticking to standard practice, with a view to developing large, portable, maintainable and extensible programs.[hr]

Comments

  • : [hr]I am writing command-line programs in C. Given the following...
    :
    : [code]#include
    :
    : #define MAX 200
    :
    : struct mystruct {
    : /* Various structure members here */
    : struct tm date; /* Following members of tm used: tm_mday, tm_mon,
    : tm_year. This data is [italic]not[/italic]
    : obtained from time(), [italic]but is entered
    : by the user[/italic]. Think of this as part
    : of a stamp collection program, for example.
    : The user would have to [italic]enter[/italic]
    : the "date of issue", since this would have
    : nothing at all to do with the system time. */
    : } data[MAX];[/code]
    :
    : ...I later realized that (I think) [blue]struct tm[/blue] is not generally used for [italic]storing[/italic] time-related data, but rather for [italic]using[/italic] it (display date/time, etc.). To [italic]store[/italic] time-related data, isn't the data type [blue]time_t[/blue] generally more appropriate?
    :
    : Yes, I know that in the kind of situation above (user-entered data) one might just as well use integers instead of anything provided by [blue]time.h[/blue] (i.e., declare [blue]int date, int month, int year[/blue]). But from a pedagogical perspective, I'm trying to get in the habit of learning and sticking to standard practice, with a view to developing large, portable, maintainable and extensible programs.[hr]
    :

    Yes, use time_t myvar; or you can just just long myvar; if its just storing.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion