Kieren Searle
UKDragon.com - KBlog
21/04/09
Compare two dates in C++
When comparing two dates that are of the data type string they will need to be first converted to dates. Once this is done a simple comparison test is possible.
Code:
void compareDates(string date1, string date2) { | |
//create a time_t object | |
time_t rawtime; | |
time(&rawtime); | |
| |
//create a tm structure and load in date1 | |
struct tm * timeinfo1; | |
timeinfo1 = localtime(&rawtime); | |
timeinfo1->tm_year = atoi(date1.substr(6).c_str()) - 1900; | |
timeinfo1->tm_mon = atoi(date1.substr(3, 2).c_str()) - 1; | |
timeinfo1->tm_mday = atoi(date1.substr(0, 2).c_str()); | |
| |
//create a tm structure and load in date2 | |
struct tm * timeinfo2; | |
timeinfo2 = localtime(&rawtime); | |
timeinfo2->tm_year = atoi(date2.substr(6).c_str()) - 1900; | |
timeinfo2->tm_mon = atoi(date2.substr(3, 2).c_str()) - 1; | |
timeinfo2->tm_mday = atoi(date2.substr(0, 2).c_str()); | |
| |
//compare the dates | |
if(timeinfo1 > timeinfo2) cout << "date1 is the latest date"; | |
else cout << "date2 is the latest date"; | |
} |
This function takes two strings, date1 and date2. Both dates are in the format of dd-mm-yyyy. Note that the hyphens can be any character as the sub-strings ignores them.
The function works by creating two tm objects and loading in some demo time data from our time_t object at the beginning of the function. This creates two identical time/date objects. We then overwrite the dates in these objects with our two dates. Once complete we have our two identical date/time objects but now with our two dates overwriting the date parts.
The final lines simply compare the two dates and outputs whichever one is the latest.
02/04/09
Easy Peasy Persistent Installation
After upgrading my EEE PC 901 to Easy Peasy 1.0 I was prompted with the install dialog each time I logged in. Not only was this annoying it was taking up valuable space on my hard drive.
To remove this ‘feature’ all that is required is to uninstall Ubiquity. You can do this either with the package manager or by using the command
sudo apt-get remove ubiquity
This will stop the annoying prompt to install each time but will also free up over 10MB of space ![]()
06/01/09
MP3 support in K3b
Due to MP3 being a patented format and Ubuntu’s policy of not allowing patented software in their default installations K3b isn’t able to burn music CDs from MP3s out of the box.
Luckily adding MP3 support is very simple and only requires one extra package to be installed:
sudo apt-get install libk3b2-extracodecs
04/01/09
Converting between System::String and std::string
When I first started working with forms in Microsoft Visual Studio I had some problems getting to grips with their managed class. One of the more fustrating points was that text boxes returned text as type System::String and all my functions were expecting std::strings.
Converting from std:string to System::String was quite simple:
Code:
std::string myString = "Hello World" | |
| |
System::String out = gcnew System::String(myString .c_str()); |
However, converting System::String to std::string was a little more tricky:
Code:
System::String myString = "Hello World"; | |
| |
std::string out = (const char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(myString )).ToPointer(); |
09/07/08
Getting rid of gaps under images
As images are inline a small gap will appear around them relative to other CSS objects.
For instance, if you want to display an image at the bottom of a div element then there will be a gap between the bottom of the image and the border of the div.
You can stop this behavior using the CSS code:
Code:
display: block; |
:: Next Page >>
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
| << < | > >> | |||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | ||
Search
Categories
KBlog
Archives
- April 2009 (2)
- January 2009 (2)
- July 2008 (1)
- June 2008 (2)
- May 2008 (5)
- April 2008 (6)
- March 2008 (6)
- February 2008 (6)
- January 2008 (5)
- More...
