You are here:

C++/retrieving month, say and year separately

Advertisement


Question
Hi Ralph, I hope you are doing well. My question is about retrieving day, month, year separately. I will be requested by other module for date and as well as day, month and year individually. I know how to retrieve date by using time(), but was wondering if there are any C++ functions or built in methods for retrieving them one by one so that I can give back just month or just day or just year according to the request.

Answer
----------------------------------------------------
FOLOWUP
----------------------------------------------------

I would like to point out that your question did not mention that you were using an MS Windows based platform, and you asked a C++ expert and hence you got an answer pertaining to C++ not MS Windows.

Further, your solution will not be portable to other systems, such as UNIX, Linux or MacOS - all of which I have found to be a requirement from time to time, and often only after a product has targeted a single platform for some time (years). Thus stating that porting to some other platform will never be a requirement of your code may well be premature!

In general the less platform specific code and assumptions are embedded in a code base the easier it is to port. The harder it is to write in the first place though.

There are a related set of points for internationalisation and localisation of code.

----------------------------------------------------


In short no, but you can convert the time_t value returned by time() into a tm struct that has separate fields for seconds, minutes, hours, weekday, day of month, day of year, month and year plus an indicator whether daylight saving is in effect. You can convert a time_t values to a tm struct using the C/C++ library functions gmtime() and localtime(). The former returns the broken down time expressed as UTC (i.e. GMT) time and the latter returns the broken down time expressed as local time (i.e. it takes account of the time zone - presumably using the current (system) settings). They return a null pointer to a tm struct if they fail.

I am not going to go into the full descriptions of these and various other time functions and types as the information are already available in many C and C++ books and references (e.g. the MSDN library and UNIX man pages). In brief however here are the basics:

   #include <ctime> // <time.h> for C

Fields of the tm struct (C++ syntax):

   struct tm
   {
       int tm_sec;   // seconds after minute 0..60
       int tm_min;   // minutes after hour 0..59
       int tm_hour;  // hours since midnight 0..23
       int tm_mday;  // day of 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
   };


Declarations for gmtime and localtime functions (C++ syntax):

   tm *localtime(const time_t * timer);
   tm *gmtime(const time_t * timer);

Finally, you might like to look at the Boost (http://www.boost.org/) date and time library, the documentation can be found at http://www.boost.org/doc/html/date_time.html.

Hope this is of use.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.