system date

V

Victor Bazarov

Wen said:
Is it easy to get the system date in c++?
Somethings like in SQL or VB?

// get the system calendar time
std::time_t tt = std::time(0);
// convert it into tm struct
std::tm ttm = *std::localtime(&tt);
// extract the values for day, month, year
int mday = ttm.tm_mday;
...

Victor
 
D

David Harmon

Thank you Victor,
Your codes are to difficult for me.

Each thing that follows "std::" is a name of something in the standard
library. Using Victor's post for clues, look up the names "time_t",
"time()", "localtime()" etc. in the index of your favorite reference.

See also section 13.12 thru 13.14 in Steve Summit's C FAQ. It is always
good to check the FAQ before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html
 
K

Kees Hoogendijk

David Harmon said:
Each thing that follows "std::" is a name of something in the standard
library. Using Victor's post for clues, look up the names "time_t",
"time()", "localtime()" etc. in the index of your favorite reference.

See also section 13.12 thru 13.14 in Steve Summit's C FAQ. It is always
good to check the FAQ before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html
Thanks David!

I've token a look in the C FAQ and found also these code's. (q20.31) But
still I dont kwow how to use it.
I'm a starter, use C++ about 2 months.
My compiler (dev c++, bloodshed 4.981) doenst know <time_t>, where can I get
the time libary?

Which include do I need? Is this a void of int ?

dayofweek(y, m, d) /* 0 = Sunday */
int y, m, d; /* 1 <= m <= 12, y > 1752 or so */
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

Best Regards,
Wen
 
V

Victor Bazarov

Kees Hoogendijk said:
Thanks David!

I've token a look in the C FAQ and found also these code's. (q20.31) But
still I dont kwow how to use it.
I'm a starter, use C++ about 2 months.

What book are you reading?
My compiler (dev c++, bloodshed 4.981) doenst know <time_t>, where can I get
the time libary?

Which include do I need? Is this a void of int ?

This is definitely a void of int. Use <get_yourself_a_good_C_book>
include. Once you used it, open the book and study. Nothing will
replace a good study.
 
K

Kees Hoogendijk

Hallo Victor,
That boek we has a Dutch name, in English is something like "to the battle
in C++"
I had about 25 hours lesson, now I must make a program for end
"examination" and I've got 2 weeks time to do it.
So, I need the help of this group.
Thanks voor your tip. If I've got more time I'll get a good c book.
Best Regards
Wen
 
V

Victor Bazarov

Kees Hoogendijk said:
Hallo Victor,
That boek we has a Dutch name, in English is something like "to the battle
in C++"
I had about 25 hours lesson, now I must make a program for end
"examination" and I've got 2 weeks time to do it.
So, I need the help of this group.
Thanks voor your tip. If I've got more time I'll get a good c book.

Kees,

A C++ book is unlikely to cover the C Standard library in depth
needed for your program. That's why I strongly recommend a C
book (even if you haven't got any time, find the time and visit
your favourite computer book shop).

Here is my code expanded to fully executable form:

#include <ctime>
#include <iostream>

int main()
{
std::time_t tt = std::time(0);
std::tm ttm = *std::localtime(&tt);
std::cout << "Today is day " << ttm.tm_mday
<< " of the month " << ttm.tm_mon + 1
<< " of the year " << ttm.tm_year + 1900
<< std::endl;
}

The same program in C would look like this

#include <time.h>
#include <stdio.h>

int main()
{
time_t tt = time(0);
struct tm ttm = *localtime(&tt);
printf("Today is day %d of the month %d of the year %d\n",
ttm.tm_mday, ttm.tm_mon + 1, ttm.tm_year + 1900);
return 0;
}

Now, if it doesn't give you enough information to proceed, you
will _have_ to ask more particular questions.

Victor
 
W

Wen

Victor,
Thank you very much!!Your code works!

Which book will you recommen, do you have a ISBN for me?
Best Regards,
Wen
 
V

Victor Bazarov

Wen said:
Which book will you recommen, do you have a ISBN for me?

Since it's a C book, I do not know the most recent advances
in the field of C programming and its description in books.
Please consider asking in comp.lang.c (and looking in their
FAQ first, then in archives, I am sure the "best book on C"
question has been asked more than once already) and visiting
http://www.accu.org for book reviews.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top