473,811 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DATETIME like YYYYMMDDHHMMSS

Hi,

How do you get the local date time as YYYYMMDDHHMMSS 14 digit string???

I am very new to C++, but if the MSDN tutorials are like that may be it
is better to go with GCC... :)

Ferhat
Jul 22 '05
55 9718

"White Wolf" <wo***@freemail .hu> wrote in message
news:cj******** **@phys-news1.kolumbus. fi...
Rob Williscroft wrote:
White Wolf wrote in news:cj******** **@phys-news1.kolumbus. fi in
comp.lang.c++:
Which part of the above is C++?


#include <time.h>
#include <stdio.h>

int main()
{
time_t t = time(0);
struct tm* lt = localtime(&t);
char time_str[15];
sprintf(time_st r, "%04d%02d%02d%0 2d%02d%02d",
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
lt->tm_hour, lt->tm_min, lt->tm_sec
);
printf( "%s\n", time_str );
}

All of it apparently.


Starting with two deprecated headers and followed by pure C code. Here
and there introducing the opportunity for buffer overruns. I see. It is
code which compiles with a C++ compiler. I would not call it C++ code.


I see no buffer overflow before the year 10000. I don't care about that.

I was mainly trying to demonstrate to the OP that the same solution was
possible in C and C++ (he seemed concerned). But if you compare the partial
solution given by Gary Labowitz with mine I would still prefer the C like
solution above. Right tool for the right job I would say.

John
Jul 22 '05 #11
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2s******** *****@uni-berlin.de...

"White Wolf" <wo***@freemail .hu> wrote in message
news:cj******** **@phys-news1.kolumbus. fi...
Rob Williscroft wrote:
White Wolf wrote in news:cj******** **@phys-news1.kolumbus. fi in
comp.lang.c++:

Which part of the above is C++?

#include <time.h>
#include <stdio.h>

int main()
{
time_t t = time(0);
struct tm* lt = localtime(&t);
char time_str[15];
sprintf(time_st r, "%04d%02d%02d%0 2d%02d%02d",
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
lt->tm_hour, lt->tm_min, lt->tm_sec
);
printf( "%s\n", time_str );
}

All of it apparently.
Starting with two deprecated headers and followed by pure C code. Here
and there introducing the opportunity for buffer overruns. I see. It is code which compiles with a C++ compiler. I would not call it C++ code.


I see no buffer overflow before the year 10000. I don't care about that.

I was mainly trying to demonstrate to the OP that the same solution was
possible in C and C++ (he seemed concerned). But if you compare the

partial solution given by Gary Labowitz with mine I would still prefer the C like
solution above. Right tool for the right job I would say.


I've taught C, and I teach C++. There is not a mention of sprintf and printf
in my C++ courses. Why should there be?
I don't teach backward compatibility. I don't even like having to explain
why <ctime> is used instead of just <time>.
Since the languages have diverged, there is no sense to me in trying to keep
them together.
--
Gary
Jul 22 '05 #12
I've taught C, and I teach C++. There is not a mention of sprintf and
printf in my C++ courses. Why should there be?
I don't teach backward compatibility. I don't even like having to
explain why <ctime> is used instead of just <time>.
Since the languages have diverged, there is no sense to me in trying to
keep them together.

Preach!
-JKop

Jul 22 '05 #13

"Gary Labowitz" <gl*******@comc ast.net> wrote in message
news:cv******** ************@co mcast.com...
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2s******** *****@uni-berlin.de...

"White Wolf" <wo***@freemail .hu> wrote in message
news:cj******** **@phys-news1.kolumbus. fi...
Rob Williscroft wrote:
> White Wolf wrote in news:cj******** **@phys-news1.kolumbus. fi in
> comp.lang.c++:
>
>> Which part of the above is C++?
>
> #include <time.h>
> #include <stdio.h>
>
> int main()
> {
> time_t t = time(0);
> struct tm* lt = localtime(&t);
> char time_str[15];
> sprintf(time_st r, "%04d%02d%02d%0 2d%02d%02d",
> lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
> lt->tm_hour, lt->tm_min, lt->tm_sec
> );
> printf( "%s\n", time_str );
> }
>
> All of it apparently.

Starting with two deprecated headers and followed by pure C code. Here and there introducing the opportunity for buffer overruns. I see. It is code which compiles with a C++ compiler. I would not call it C++ code.
I see no buffer overflow before the year 10000. I don't care about that.

I was mainly trying to demonstrate to the OP that the same solution was
possible in C and C++ (he seemed concerned). But if you compare the

partial
solution given by Gary Labowitz with mine I would still prefer the C like solution above. Right tool for the right job I would say.


I've taught C, and I teach C++. There is not a mention of sprintf and

printf in my C++ courses. Why should there be?
Well if it's your course it is up to you, and I can see why you would not
want to teach two methods of doing the same thing, so it makes perfect sense
not to teach sprintf etc. Nevertheless I still find sprintf useful for
certain situations.

1) The code is more compact when the formatting is complex, as it was in
this example.

2) With sprintf the format information is localised in a single string. This
can be useful when trying to write applications for different locales. Of
course C++ libraries exist with the same facility (e.g. boost format). The
very fact that boost format exists demonstrates that the approach taken by
sprintf is useful.

3) There is also considerably less overhead in sprintf than ostringstream. I
almost always prefer it to ostringstream when doing one off formatting tasks
like the OP's.
I don't teach backward compatibility. I don't even like having to explain
why <ctime> is used instead of just <time>.
Since the languages have diverged, there is no sense to me in trying to keep them together.


I'm not trying to keep anything together, just picking what I see as the
right tools for this particular task. Obviously it a judgement call, and
different people will choose different methods. I'm not trying to say my
method is the best, just that it is valid.

john
Jul 22 '05 #14
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2s******** *****@uni-berlin.de...

"Gary Labowitz" <gl*******@comc ast.net> wrote in message <<snip>> I'm not trying to keep anything together, just picking what I see as the
right tools for this particular task. Obviously it a judgement call, and
different people will choose different methods. I'm not trying to say my
method is the best, just that it is valid.


Thanks, John, it's a reasonable response.
Look, when I get a call to do some Access 2 work (and I did about a year
ago) I had to explain that I had long ago uninstalled that version and moved
on. The guy was very disappointed. I had a job updating a FoxPro program a
while back, and they wouldn't convert to Visual FoxPro. What they wanted was
virtually undoable and I told them so. I've been through the trauma of
learning enough new versions of languages to want to maintain lots of
versions --- in general I pick up the new version and move on. My current
internal debate is with VB6.0 and VB .NET. I have them both installed and I
hate like hell to give up VB 6.0. But I guess it will happen if I stick
around much longer.

I have always hated the languages that maintain lots of old version
constructs for backward compatibility. They don't want to break all the old
code, and you can't force people to upgrade all that software in a short
timeframe, but when? Microsoft puts the screws on by naming a date and
dropping support after that. It's cruel I guess, but otherwise they will
sink in a quagmire of versions. It's one of the worst aspects of computing.
Hard won knowledge is hard to let go of, and the new stuff can be very hard
to learn. Will we ever get rid of C-style strings in C++? Probably not. One
book I'm using now mentions them in an Appendix, that's how little they are
needed -- except for backwash I/O and old programs. I have to teach it, but
if you teach string class first they look like a real waste of time and
effort.

Well, I can cut and run (being over the age of sticking around). But what of
my students? They are graduating in years like 2006, 2008, and still
programming like it was 1991. I'm getting to hate it. [This is in the
category of "Don't get me started."]
--
Gary
Jul 22 '05 #15
Will we ever get rid of C-style
strings in C++? Probably not.

I don't see what's wrong with C-style strings. I use them 90% of the time,
unless I *need* an std::string. Why? Efficency.

A string is and always will be just an array of characters after all! As for
the whole null character thing... two possible methods:

A) Use a null character which signifies the end of the string, as in:

char blah[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };
B) Have a field for the length of the string:

struct Blah
{
unsigned length;

char blah[5];
};

Blah poo = { 5, { 'H', 'e', 'l', 'l', 'o' } };
The former was chosen and it works well. It has advantages too: There's no
limit put on the length of the string, whereas with the latter, the length
is limited to the maximum value of an "unsigned".

As for std::string and std::ostringstr eam, I use them when want I want to do
would take me a few hours to code if I were to do it by myself, whereas it
would only take a few mins with these classes.

-JKop
Jul 22 '05 #16
Gary Labowitz wrote:
All of it apparently.


Starting with two deprecated headers and followed by pure C code. Here
and there introducing the opportunity for buffer overruns. I see. It
is code which compiles with a C++ compiler. I would not call it C++
code.


Oh, Atilla, you are SO fussy!


Neither. I am Attila and I believe that in a C++ group pure C code should
be flagged as such. BTW the sig-quotes are REALLY random. Does my random
number device try to tell something? To whom? ;-)

--
WW aka Attila
:::
Never argue with an idiot. They drag you down to their level then beat you
with experience.
Jul 22 '05 #17
Mike Wahler wrote:
All of it apparently.


Starting with two deprecated headers and followed by pure C code.


which also qualifies as 'pure C++ code'.


I see. Do you not want to see the point?
Here and
there introducing the opportunity for buffer overruns.


That's a quality issue.


Is it? Or is it that code with C++ design is less likely to have that
problem?
I see. It is code
which compiles with a C++ compiler. I would not call it C++ code.


Call it what you like. It *is* C++, according to 14882.


It is pure C. There is nothing C++ to it. You can call it whatever you
want, that won't change the facts.

--
WW aka Attila
:::
Man is a peculiar creature. He spends a fortune making his home
insect-proof and air-conditioned, and then eats in the yard.
Jul 22 '05 #18
I see. It is code
which compiles with a C++ compiler. I would not call it C++ code.


Call it what you like. It *is* C++, according to 14882.


It is pure C. There is nothing C++ to it. You can call it whatever you
want, that won't change the facts.


What is this please?

int main()
{
return 0;
}

john
Jul 22 '05 #19
John Harrison wrote:
"White Wolf" <wo***@freemail .hu> wrote in message [SNIP]
All of it apparently.


Starting with two deprecated headers and followed by pure C code. Here
and there introducing the opportunity for buffer overruns. I see. It
is code which compiles with a C++ compiler. I would not call it C++
code.


I see no buffer overflow before the year 10000. I don't care about that.


I see. I do, since I do not believe that this is the only way of failure
waiting there.
I was mainly trying to demonstrate to the OP that the same solution was
possible in C and C++ (he seemed concerned). But if you compare the
partial solution given by Gary Labowitz with mine I would still prefer
the C like solution above. Right tool for the right job I would say.


I have asked if it was a C++ solution. Later (having got the reply I did) I
hinted on it, that it had a buffer overflow opportunity. Prior to that all
I have been trying to point out is that it is C and not C++. Reason being
that we have tried for ages here to show C++ solutions or flag the pure C
solutions as such. Or tell (for dumb people like me) why is that C code the
best C++ to use.

--
WW aka Attila
:::
When the pupil is ready, the teacher will come.
Jul 22 '05 #20

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

Similar topics

3
4213
by: Martin Bless | last post by:
Below is what I'm currently using to construct datatime objects from strings. Date and time objects are made up similar. To and from string conversions are frequently needed in SQL neighborhood. (1) Is there reason enough to ask for a 'datetime.fromstr(...)' method? At least to convert ISO timestamps? (2) You've got a better/different idea? Glad to see. Don't let me die with this open question ... ;-)
4
2531
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it should be used? I can make a datetime easily enough
6
24812
by: Thomas Bartkus | last post by:
MySQL Version 4.0.20 on a Linux server. How does one get the elapsed time between (2) DateTime values? I need the answer to the nearest minute. Is upgrading to Ver 5 with its more robust date/time functions the only solution? You can directly subtract 2 DateTime values and a long integer results. What is that number?
4
2643
by: Mark | last post by:
Is there a way to convert a char to a DateTime without first converting to a string and using DateTime.Parse or ParseExact? I'm trying to reuse the char which can be reused instead of converting to a string since string is immutable and must be GC'ed. Please, I'm looking for a conversion between an char to DateTime without intermediary step. Thanks in advance Mark
6
8994
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference between date and datetime classes? Please, help
1
1247
by: Frank Esser | last post by:
Hello, how can I get the date/time value from a string that contains the date and time in this format: YYYYMMDDhhmmss Thanks!
9
4934
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:JadeWebServices/WebServiceProvider/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
3
9868
by: Eric Stott | last post by:
I need to take System.DateTime.Now and have the resulting text be in the following format: yyyyMMddhh24mmsss I am using System.Convert.ToString(System.DateTime.Now), but I need to format it correctly, what is the easiest way to accomplish this?
4
8623
by: simonZ | last post by:
I have string 20070502144551 and I would like to convert this to datetime: 2007-05-02 14:45:51 Is there some function? Regards,Simon
0
9605
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
10647
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
10386
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...
0
9204
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
7669
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
6889
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
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.