Thread: Getting systems date and time

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Question Getting systems date and time

    I want to take from the system,the hour(only the hour for example 03,12,23...),the month(01,02,......,12) the year(2006) and store them into integer variables.I have found some functions of the time library <time.h> such as strftime() which returns individual elements of the date and time ,but as i understand from its prototype ,it returns the elements in strings.The matter is that i want to edit the values and i can`t find a way doing this throught strings.



    P.S. Sorry for my english.Any help is acceptable!!!
    Maradona
    =======

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Edit the values? Do you mean change the time, or simply change variables set from the time?
    <ctime> defines
    Code:
    struct tm {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
    };
    The function
    Code:
    struct tm *localtime(const time_t *time)
    returns a pointer to this type of structure, based on the value of *time. If you want the time data:
    Code:
    struct tm tizzle;
    struct tm *t_ptr;
    t_ptr = localtime(time(NULL));
    tizzle.some_member = t_ptr->some_member;   //copy the data
    //........etc...........
    I don't know about the data that localtime points to, so I'd make sure to copy its data into another struct before being called again or something. I might be wrong, but my book doesn't say how to use the function correctly. Anyway, I hope this gives you a good idea.
    Last edited by CodeMonkey; 12-19-2006 at 06:15 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    by the way:
    Code:
    int tm_sec; /*seconds: 0-61*/
    int tm_min; /*minutes: 0-59*/
    int tm_hour; /*hours: 0-23*/
    int tm_mday; /*day of the month: 0-31*/
    int tm_mon; /*months since Jan: 0-11*/
    int tm_year; /*years since 1900*/
    int tm_wday; /*days since Sunday: 0-6*/
    int tm_yday; /*days since Jan 1: 0-365*/
    int tm_isdst; /*positive if Daylight Savings Time is in effect, zero if not, and negative if there is no information available*/
    Thanks to Herbert Schildt for this info (as I presented it).
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > Thanks to Herbert Schildt for this info

    Now there's something you don't see often. And the reason is, he's usually wrong about too many things. He's quiet good at that and apparently also well know because of it.

    So...

    >int tm_sec; /*seconds: 0-61*/
    Not really. Doesn't even make sense. 0-59 should be the one, but its actually 0-60 because of leap years.

    > int tm_mday; /*day of the month: 0-31*/
    1-31 is the one. Again 32 days wouldn't make sense. It's the only ordinal on the entire structure. It starts at 1, not 0.

    > int tm_year; /*years since 1900*/
    Not quiet. It's in fact year minus 1900. Negative values correspond to years before 1900

    EDIT: Do as I did some time ago and throw Herbert Schildt's C/C++ Programmer's Reference on the pile of useless books.
    Last edited by Mario F.; 12-19-2006 at 08:07 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [OT=slightly]Actually I believe these int members may be manipulated well outside of the 'reasonable' assumptions for valid values. It makes for another way to perform arithmetic on times and dates. Then recalculating the resulting date and time requires a little extra work.[/OT]

    And I don't know if years less than 1900 produce meaningful results (I should really experiment or look something up here.)

    [edit]All righty. Lifted from C99:
    The tm structure shall contain at least the following members, in any order. The semantics of the members and their normal ranges are expressed in the comments.265)
    int tm_sec; // seconds after the minute — [0, 60]
    int tm_min; // minutes after the hour — [0, 59]
    int tm_hour; // hours since midnight — [0, 23]
    int tm_mday; // day of the month — [1, 31]
    int tm_mon; // months since January — [0, 11]
    int tm_year; // years since 1900
    int tm_wday; // days since Sunday — [0, 6]
    int tm_yday; // days since January 1 — [0, 365]
    int tm_isdst; // Daylight Saving Time flag
    265) The range [0, 60] for tm_sec allows for a positive leap second.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Well crap, Mario. I guess I have to buy another book.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. How to get and set windows time and date
    By huwan in forum Windows Programming
    Replies: 18
    Last Post: 05-13-2008, 10:33 AM
  4. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM