-1

First time poster so excuse me if I make any sort of mistake.

I am fairly new to the whole programming in C++ but I was wondering if it is possible to print out a calendar of a certain month (the current one) e.g. today it is June 2015 so I want to print out the monthly calendar of June 2015 in C++

If anyone has any suggestion how to make this possible that would be extremely helpful. I know how to post the current month with user input yet I want my program to look at the system date.

Thanks in advance.

nick3h
  • 3
  • 3
  • [Give this a read.](http://stackoverflow.com/questions/8034469/c-how-to-get-the-actual-time-with-time-and-localtime). After that, the hard part is getting the correct days of the month. – user4581301 Jun 15 '15 at 22:16
  • You might start out from [`std::chrono::timepoint`](http://en.cppreference.com/w/cpp/chrono/time_point). – πάντα ῥεῖ Jun 15 '15 at 22:17

1 Answers1

0

Use time(0) to get a time_t that is also somehow the number of seconds since xxxoopsxxxxx => epoch: Wed Dec 31 19:00:00 1969, so 1/1/1970

time_t linearTime = time(0);

Use localtime_r to convert the linearTime to

struct tm timeinfo = *localtime_r(&linearTime, &timeinfo);

Use struct tm to decode broken-down-time (in Linux, at /usr/include/time.h) and produce your calendar as you want.

Have fun.

Edit 2

Looked into chrono. Figured out how to measure duration in us.

BUT, the following chrono stuff is suspiciously similar to previous stuff.

std::time_t t0 = std::time(nullptr);
std::tm* localtime(const std::time_t* t0); // std::tm has the fields you expect

// convenient to text
std::cout << std::asctime(std::localtime(&t0)); // prints the calendar info for 'now'

I suspect not much different at all.

edit 3

I have doubts. I think the previous suspiciously familiar bit came from ctime, not chrono. So I plan digging around in chrono some more.

2785528
  • 5,438
  • 2
  • 18
  • 20
  • Hmmmm, that's not really standard c++ ... I'd rather stick to `std::chrono`. – πάντα ῥεῖ Jun 16 '15 at 03:11
  • Agree .. I have not yet learned std::chrono. I was being careful to not do the work for him ... This will be good practice for OP to learn both techniques. Hope I find time, too ;) – 2785528 Jun 16 '15 at 22:13
  • Thanks a lot though. I am indeed not looking for the whole answer. Thank you both for the answers you have provided. I will look into it. – nick3h Jun 18 '15 at 09:33