CS001143 - Converting date and time to string in Open C++
From Forum Nokia Wiki
(Redirected from Converting date and time to string in Open C++)
| ID | CS001143 | Creation date | October 14, 2008 |
| Platform | S60 3rd Edition, FP2 | Tested on devices | Nokia 6220 Classic |
| Category | Open C/C++ | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): time_t, tm, stringstream, use_facet, time_put, time_put::put(), time(), localtime(), asctime() |
Overview
This code snippet shows how to convert a date and time to a string. The easiest way to get this string is to use the library function asctime() from the <ctime> header. The returned string has the following format: "Www Mmm dd hh:mm:ss yyyy". Another possibility is to use the time_put template class from the <locale> header and use a custom formatting for the date and time string.
Note: In order to use this code, you need to install the Open C/C++ plug-in.
This snippet can be self-signed.
MMP file
The following libraries are required:
STATICLIBRARY libcrt0.lib LIBRARY libstdcpp.lib LIBRARY libc.lib LIBRARY euser.lib
Source file
#include <iostream> #include <string> #include <sstream> #include <locale> #include <ctime> using namespace std; string datetime_to_string(const tm& time, const char* format) { stringstream datetime; // retrieve the time_put facet installed in the stream const time_put<char>& writer = use_facet< time_put<char> >(datetime.getloc()); int len = strlen(format); //formats the contents of the tm time into the output stream datetime if (writer.put(datetime, datetime, ' ', &time, format, format + len).failed( )) { throw runtime_error("formatting date time failed!"); } return datetime.str(); } int main( ) { string time_string; time_t time_now = time(NULL); struct tm * timeinfo; timeinfo = localtime (&time_now); //-- using the library function asctime() cout << "Current local time and date: " << endl << asctime(timeinfo) << endl; //-- using the own function datetime_to_string() /* The list of % format specifiers: a Abbreviated weekday name A Full weekday name b Abbreviated month name B Full month name c Date and time d Day of the month H Hour of the 24-hour day I Hour of the 12-hour day j Day of the year m Month of the year M Minutes after the hour p AM/PM indicator S Seconds after the minute U Week of the year (Sunday) w Day of the week W Week of the year (Monday) x Date MM/DD/YY X Time HH/MM/SS (24-hour) y Year of the century Y Year Z Time zone name */ try { time_string = datetime_to_string(*timeinfo, "%A %B %d %H:%M:%S %Y"); cout << time_string << endl; time_string = datetime_to_string(*timeinfo, "%Y/%m/%d %H:%M:%S"); cout << time_string << endl; time_string = datetime_to_string(*timeinfo, "%x %X"); cout << time_string << endl; } catch (const exception& e) { cout << "Exception: " << e.what() << endl; } //getchar(); return 0; }
Postconditions
The current date and time have been converted to four different formats and are shown on the screen.
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| get log local time ? | ali3635 | Python | 10 | 2008-10-21 05:28 |
| Retrieval Of date variable from a Database | mihirrath | General Symbian C++ | 3 | 2003-06-26 04:47 |
| Urgent Help Needed-Alert/Scheduler Application | jonnystewart | Mobile Java General | 1 | 2004-07-31 00:20 |
| date editor! | georgica1979 | General Symbian C++ | 2 | 2003-05-14 15:00 |
| encoding of date field | eee94180 | General Messaging | 1 | 2002-07-31 11:05 |

