Data structure for storing dates

This is a discussion on Data structure for storing dates within the C Programming forums, part of the General Programming Boards category; what data type can be used to get the "DATE" from the user??? say, for example. date of joining of ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    28

    Data structure for storing dates

    what data type can be used to get the "DATE" from the user???

    say, for example. date of joining of an employee

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by phoneix_hallows View Post
    what data type can be used to get the "DATE" from the user???

    say, for example. date of joining of an employee
    One you make up. (I suppose you could use a struct tm from <time.h>, if you really wanted to.) Not sure what that has to do with exit(), though.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Here's the details on struct tm from <time.h>:

    Code:
    struct tm
        Represents the components of calendar time:
    
        int tm_sec;
            seconds after the minute
        int tm_min;
            minutes after the hour
        int tm_hour;
            hours since midnight
        int tm_mday;
            day of the month
        int tm_mon;
            months since January
        int tm_year;
            years since 1900
        int tm_wday;
            days since Sunday
        int tm_yday;
            days since January 1
        int tm_isdst;
            Daylight Saving Time flag : is positive if DST is in effect, zero if not in effect, negative if information not known.
    
        Implementations may change field order and include additional fields.
    You could also define your own struct - if you haven't learnt about them yet, you should..

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    actually i've to use the date inside a structure only... so thought it ' d further mess up the code by using nested structures......

    thanks for your suggestions

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    that was really helpful...... thanks... this forum is really amazing!!!!!

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Well unless you want to go UNIX-style and use a single large integer to represent the number of seconds since a certain date (UNIX counts the number of seconds since January 1, 1970), you're out of luck - as there's no native type for dealing with that, that's what structs are for. There's nothing inherently evil about nested structs - they can be messy, sure, but sometimes you need them.

    edit: as discussed in this thread: time_t

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    1
    Here are some of my dumb questions (i'm new to linux):

    Is it possible to include a header file in linux C? I mean where are the header files located? Is the code "#include<time.h>" is enough for adding the header file to the program?

    Why is it we have to use <int main> instead of <void main> everytime?

    Should we have to put the "\n" at the end of the <printf> everytime?


    Questions regarding this thread:
    What's the advantage of using a "tm" struct? I can create my own format of date structure and use it to get the date values from the user.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Is it possible to include a header file in linux C? I mean where are the header files located? Is the code "#include<time.h>" is enough for adding the header file to the program?
    Sometime you need to tell the compiler to link to the appropriate libraries. You shouldn't need to for time.h, though.

    Why is it we have to use <int main> instead of <void main> everytime?
    Because the standard says so! As to why it says so, its because programs in convention are designed to return an integer exit code. All programs do this on all popular operating systems, particularly unix which is most relevant to the development of C. The return value of main is this exit code.

    Should we have to put the "\n" at the end of the <printf> everytime?
    \n tells printf to start the next print statement on the next line. If you don't want to do this, don't end in \n.

    What's the advantage of using a "tm" struct? I can create my own format of date structure and use it to get the date values from the user.
    Just that it's standard and can work with other functions not written by you.
    For most cases I would probably create a date struct.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    But how do i make use of the predefined struct tm. I tried but it does not seem to work.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
    	struct tm;
    
    	/*
    	    int tm_sec;
            seconds after the minute
        int tm_min;
            minutes after the hour
        int tm_hour;
            hours since midnight
        int tm_mday;
            day of the month
        int tm_mon;
            months since January
        int tm_year;
            years since 1900
        int tm_wday;
            days since Sunday
        int tm_yday;
            days since January 1
        int tm_isdst;
            Daylight Saving Time flag : is positive if DST is in effect, zero if not in effect, negative if information not known.
    	*/
    
    	//tm time;
    	//time.tm_sec = 45;
    	tm.tm_sec = 45;
    
    	printf("\n Seconds after min: %d", tm.tm_sec);
    
    	return 0;
    }

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    It's a struct (a type), but you haven't created an instance of that type yet!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    But i did create an instance of the struct tm using

    tm time;
    time.tm_sec = 45;

    and then i tried printing it out and it gave me an error. Its commented out in my program. I did that initially but it didnt work.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    AFAIK you need to specify that you want a struct of the type tm. Your declaration is:

    struct tm;

    It aught to be something like:

    struct tm my_tm_struct;

  13. #13
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by Subsonics View Post
    AFAIK you need to specify that you want a struct of the type tm. Your declaration is:

    struct tm;

    It aught to be something like:

    struct tm my_tm_struct;
    Yes i did that

    struct tm;

    Then i created an instance of it using

    tm time;

    Its commented out in my program because it was not working.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by roaan View Post
    Yes i did that

    struct tm;

    Then i created an instance of it using

    tm time;

    Its commented out in my program because it was not working.
    I would have thought by now that you had figured variables out. You don't declare a variable of type int by putting
    Code:
    int;
    in your program; you put the variable name in there like "int foo". You need to do the same:
    Code:
    struct tm time;

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by roaan
    Yes i did that

    struct tm;
    That is just a declaration of the type struct tm.

    Quote Originally Posted by roaan
    Then i created an instance of it using

    tm time;
    That should be:
    Code:
    struct tm time;
    but then you should be using some other identifier instead of time since that is a name of a function declared in <time.h>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parent in a binary search tree
    By roaan in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 07:08 PM
  2. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  3. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 11:42 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 05:03 PM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21