Thread: How to check "struct tm" variable is empty?

  1. #1
    Registered User
    Join Date
    Jun 2009

    How to check "struct tm" variable is empty?


    I have a struct tm variable named "file_timestamp" and I used it in different functions.

    Two issues I face here,

    Issue 1) Sometimes the date is printed (converted to string using strftime(), then used printf()) as 1900-01-00 00:00:00.
    I do memset at few places, memset (&file_timestamp, '\0', sizeof (struct tm));
    Will this memset trigger setting time as epoch time ?

    Issue 2)
    Before assigning some date&time to this variable "file_timestamp", I want to ensure that the variable is empty.
    I want to do assigning date & time to this stuct tm variable only very first time. How can we do this?

    I just tried this as follows. Will this work always?

    Issue 2 code is as follows,

    struct tm file_timestamp; if (!(file_timestamp.tm_mday >= 1 && file_timestamp.tm_mday <= 31)) { /* Write the timestamp_str to struct tm file_timestamp */ strptime (timestamp_str, "%Y/%m/%d %H:%M:%S", &file_timestamp); }
    Thanking you

  2. #2
    Registered User
    Join Date
    Dec 2017
    Zeroing the tm struct sets all the fields to 0 and yields the date that you see (the year value has 1900 added to it and the month value has 1 added to it for display purposes). However, that date is not the epoch time. To see the epoch time (which is Midnight, Jan 1, 1970) you can do the following:
    #include <stdio.h>
    #include <time.h>
    int main() {
        time_t t = 0; // 0 time_t value represents the epoch
        printf("%s\n", asctime(gmtime(&t))); // Thu Jan  1 00:00:00 1970
        return 0;
    To ensure that the tm struct is zeroed (if that's what you mean by "empty") you need to use memset like you did. I guess you could check that tm_mday is still 0 to check that it hasn't been set since you zeroed it:
    if (t.tm_mday == 0)  // day has not been set
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Why don't you just do
    struct tm file_timestamp = { 0 };
    But normally, I would expect the thing that writes to file_timestamp to return a success/fail status of some sort (which you should check).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-06-2015, 10:23 PM
  2. Resize array values of "empty" indices
    By monkey_c_monkey in forum C++ Programming
    Replies: 2
    Last Post: 07-09-2012, 01:22 PM
  3. Replies: 12
    Last Post: 07-05-2012, 08:32 AM
  4. An interesting code about "struct" in "union"
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2008, 04:37 AM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM

Tags for this Thread