Thread: Getting systems date and time

1. 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!!!
=======

2. 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.

3. 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).

4. > 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.

5. [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.)

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.

6. Well crap, Mario. I guess I have to buy another book.