473,626 Members | 3,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parsing string with std::stringstre am

Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:

'YYYY-MM-DD HH:MM:SS AM'

Jul 18 '07 #1
7 13623
"Grey Alien" <gr**@andromeda .comwrote in message
news:Hs******** *************** *******@bt.com. ..
Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:

'YYYY-MM-DD HH:MM:SS AM'
There were 2 replies, one of mine included, to your previous post showing
how to parse this string using std::stringstre am. Did you not read the
replies, or did you not like them?
Jul 18 '07 #2


Jim Langston wrote:
"Grey Alien" <gr**@andromeda .comwrote in message
news:Hs******** *************** *******@bt.com. ..
>>Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:

'YYYY-MM-DD HH:MM:SS AM'


There were 2 replies, one of mine included, to your previous post showing
how to parse this string using std::stringstre am. Did you not read the
replies, or did you not like them?

Apologies - my original post (and any subsequent replies) for some
reason - did not show up on my newsreader. Hence my repost yesterday. I
did not get any reply yesterday to my repost - hence this post today.
Could you kindly post your soln to this thread - I don't understand why
the other posts did not appear. tx
Jul 18 '07 #3

"Grey Alien" <gr**@andromeda .comwrote in message
news:D4******** *************** *******@bt.com. ..
>

Jim Langston wrote:
>"Grey Alien" <gr**@andromeda .comwrote in message
news:Hs******* *************** ********@bt.com ...
>>>Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:

'YYYY-MM-DD HH:MM:SS AM'


There were 2 replies, one of mine included, to your previous post showing
how to parse this string using std::stringstre am. Did you not read the
replies, or did you not like them?

Apologies - my original post (and any subsequent replies) for some
reason - did not show up on my newsreader. Hence my repost yesterday. I
did not get any reply yesterday to my repost - hence this post today.
Could you kindly post your soln to this thread - I don't understand why
the other posts did not appear. tx
"Erik Wikström" <Er***********@ telia.comwrote in message
news:D2******** ********@newsb. telia.net...
On 2007-07-17 18:19, Grey Alien wrote:
>I want to parse the various time elements from a string. The input string
has the ff format:

'YYYY-MM-DD HH:MM:SS AM'

Can anyone show me how to do this?

This is the only way that I know of that don't require any extra
libraries/extensions:

#include <iostream>
#include <sstream>
#include <string>

template <class T>
T stoa(const std::string& s)
{
T t;
std::istringstr eam iss(s);
if (!(iss >t))
throw "Can't convert";
return t;
}

int main()
{
std::string input;
std::getline(st d::cin, input);

int year, mon, day, hour, min, sec;

year = stoa<int>(input .substr(0, 4));
mon = stoa<int>(input .substr(5, 2));
day = stoa<int>(input .substr(8, 2));
hour = stoa<int>(input .substr(11, 2));
min = stoa<int>(input .substr(14, 2));
sec = stoa<int>(input .substr(17, 2));

if (input.substr(2 0, 2) == "PM")
hour += 12;
}

Don't forget to apply error checking
This works also, although you should throw in error checking.

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::cout << "Enter Date/Time format: YYYY-MM-DD HH:MM:SS AM: ";
std::string DateTime;
std::getline( std::cin, DateTime );

std::stringstre am Stream( DateTime );
int Year, Month, Day, Hour, Minute, Second;
std::string AmPm;
char ThrowAway;
Stream >Year >ThrowAway >Month >ThrowAway >Day;
Stream >Hour >ThrowAway >Minute >ThrowAway >Second;
Stream >AmPm;

std::cout << Year << "-" << Month << "-" << Day << " " << Hour << ":" <<
Minute << ":" << Second << " " << AmPm;

}
Jul 18 '07 #4
Thanks Jim, I'll study the code and make any suitable
checks/modifications/error handling.
Jul 18 '07 #5

Grey Alien <gr**@andromeda .comwrote in message...
>
Apologies - my original post (and any subsequent replies) for some
reason - did not show up on my newsreader.
It's those dang iPhones screwing up the net!! <G>

Some ISPs are slow to propagate (and getting worse (spam filters, etc.)).

--
Bob R
POVrookie
Jul 18 '07 #6
On Jul 18, 8:25 am, Grey Alien <g...@andromeda .comwrote:
Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:

'YYYY-MM-DD HH:MM:SS AM'
Sure. Try something like this.

std::stringstre am ss;
ss << "2007-07-19 11:18:01 AM";
int year, month, day, hour, min, sec;
char c;
ss >year >c >month >c >day >hour >c >min >c >>
sec;
std::cout << " " << year << " " << month << " " << day << " " <<
hour << " " << min << " " << sec;

Jul 19 '07 #7
On 18 Jul, 07:25, Grey Alien <g...@andromeda .comwrote:
Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:

'YYYY-MM-DD HH:MM:SS AM'
sscanf(myString .c_str(), "%4d-%2d-%2d %2d:%2d:%2d %s",
&year, &month, &day, &hour, &min, &sec, ampm_str);

Jul 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
15920
by: Ellarco | last post by:
Im sorry for asking a question that is surely in the archives somewhere, but I have been unable to locate it. Its about string memory management. I need to dynamically construct a C-style string and was planning to go about it this way; char* foo() { std::stringstream ss; ss << ...
6
36510
by: Giampiero Gabbiani | last post by:
Is it possible to reset a std::stringstream in order to reuse it once more? I tried with flush() method without any success... Thanks in advance Giampiero
5
2386
by: Christopher Benson-Manica | last post by:
If std::stringstreams are unavailable, how would you go about adding an integer (or unsigned integer) to a string? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
4
15910
by: Dylan | last post by:
Hi again, In the following program I expected step 3 to assign the values 1 and 2 to iVal1 and iVal2 yet it appears ss.seekg(0, std::ios::beg) does not move the read position back to the beginning of the stream as expected. //----------------------------------------------------------------------------- #include <iostream> #include <sstream>
5
12052
by: Mr Fish | last post by:
Is it possible for me to record floats in a std::stringstream with 100% accuracy? If so how? Here's a little prog that demonstrates how the default ss loses accuracy //----------------------------------------------------------------------------- #include <sstream> #include <iostream> using namespace std;
5
3599
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
1
2883
by: magix | last post by:
I got this reply in my previous post a month ago: May I know, how can I automatically create the folder if it doesn't exist ? In previous reply, it said: ------------------------- std::stringstream stream; stream << "C:\\Output\\Output.txt" << std::flush; std::ofstream SaveFile(stream.str().c_str()); The "C:\Output" must already exist?
0
1423
by: ziyanjoe | last post by:
Hi! I am writing a program for the robot that runs as a daemon on a linux machine. Since debug output cannot be seen on stdout, I want to create an iostream to handle all the output messages. However, I do not want to use a file or output to the clog since I want to achieve the debug message remotely through TCP connection. I've already built a TCP server for transmitting data, all i need is an "endless" stringstream. I tried to code this...
7
3598
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent tho network to a client so that errors and debug messages can be displayed simultaneously anywhere. I tried to use a std::stringstream to do the job. I created the stringstream in main() and pass it as pointer into a few threads. Those threads...
0
8205
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8370
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.