473,811 Members | 3,298 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
Mike Wahler wrote:
[SNIP]
You can do it the same way in C, C++, VC++, gcc, g++, in fact in any C
or C++ compiler in the world.

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

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);

Untested code

Which part of the above is C++?


All of it. Remember that the C++ standard library contains
the (C90) C standard library. Also, tHe headers with .h, while
deprecated, are indeed part of standard C++.


And it is still a pure C solution. IIRC there was an agreement here that
when showing code, we show C++ code and the best C++ there is to demonstrate
the issue. I see no problem with pure C code as long as it is proven that
that piece of code is the best C++ solution as well.

--
WW aka Attila
:::
Zen meditation isn't what you think...
Jul 22 '05 #21
> >
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.


OK, educate me, where are the other oppotunites for buffer overflow?
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.


I did flag it as a C solution. Whether it is the best solution is obviously
a matter of opinion. I just coded what I would have done given the OP's
problem.

john
Jul 22 '05 #22

"JKop" <NU**@NULL.NULL > wrote in message
news:l6******** ***********@new s.indigo.ie...
Starting with two deprecated headers and followed by pure C code.
which also qualifies as 'pure C++ code'.

int Blah()
{
return 5;
}

int main()
{
Blah(8);
}
Case and point.


What is your point? What you wrote above is *not* valid C++,
but is valid C (99).

C is irrelevant here.
It is relevant to "White Wolf"s remarks, since that was what
he talked about.

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

Just like a banger is still a car.


I don't know what a 'banger' is. (I'll guess it's some
sort of 'slang', but 'slang' and informal terms imo should
be eschewed in a technical forum. It only leads to confusion.

-Mike
Jul 22 '05 #23

"White Wolf" <wo***@freemail .hu> wrote in message
news:cj******** **@phys-news1.kolumbus. fi...
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?


Sure. So tell me what your point is.
Here and
there introducing the opportunity for buffer overruns.
That's a quality issue.


Is it?


Yes. The code is valid C++. Whether it's of 'good quality'
is of course a matter of opinion.
Or is it that code with C++ design is less likely to have that
problem?
What is "C++ design?".
I see. It is code
which compiles with a C++ compiler.

It is code which conforms to the internation standard which
defines the C++ language.
I would not call it C++ code.
The C++ standard does.

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


It is pure C.


It qualifies as C and C++.
There is nothing C++ to it.
Everything in it is valid C++ according to the C++ standard.
You can call it whatever you
want, that won't change the facts.


That's right. And the facts are that is is valid C++.

-Mike
Jul 22 '05 #24

"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?
That depends upon your teaching goals. However, for completeness,
perhaps you should at least mention that those functions exist in
C++, even if you don't actually teach about or use them in class.

I don't teach backward compatibility.
Backward compatibility is important in the 'real world'.
I don't even like having to explain
why <ctime> is used instead of just <time>.
I think you meant <time.h>. There is no <time> header in
either language.
Since the languages have diverged, there is no sense to me in trying to keep them together.


Many C++ projects interface with C code. One should be at least
aware of the facilities for dealing with this, even if it's not
part of a "C++ only" course.

$.02,
-Mike
Jul 22 '05 #25

"White Wolf" <wo***@freemail .hu> wrote in message
news:cj******** **@phys-news1.kolumbus. fi...
Mike Wahler wrote:
[SNIP]
You can do it the same way in C, C++, VC++, gcc, g++, in fact in any C
or C++ compiler in the world.

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

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);

Untested code
Which part of the above is C++?
All of it. Remember that the C++ standard library contains
the (C90) C standard library. Also, tHe headers with .h, while
deprecated, are indeed part of standard C++.


And it is still a pure C solution.

It is a solution which qualifies as legal C *or* C++. Both
language standards support this assertion.
IIRC there was an agreement here that
when showing code, we show C++ code

Well, yes, this is a C++ group.
and the best C++ there is
"Best" will never be agreed upon, since it's a subjective issue.
to demonstrate
the issue.
John gave what he (apparently) felt was the 'best' solution.
What did he feel was 'best' about it? I'm not sure. Perhaps
as simple as that he thought it would be easier for OP to follow
that e.g. something involving containers etc.

I see no problem with pure C code
John's code qualifies according to both standards as C *or* C++.
If it were not valid C, it would not be topical here. So far
I haven't seen anyone challenge the topicality of his code (and
anyone who did would be wrong to do so(.
as long as it is proven that
that piece of code is the best C++ solution as well.


Again, "best" can never be proven. It's a matter of opinion,
and dependent upon context as well.
-Mike

Jul 22 '05 #26

"White Wolf" <wo***@freemail .hu> wrote in message
news:cj******** **@phys-news1.kolumbus. fi...
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.


I agree with that, and also add that it should be flagged as
off topic. However John's code does not qualify as such.

-Mike
Jul 22 '05 #27
Mike Wahler wrote in news:u7f8d.2569 $M05.241
@newsread3.news .pas.earthlink. net in comp.lang.c++:
I don't even like having to explain
why <ctime> is used instead of just <time>.


I think you meant <time.h>. There is no <time> header in
either language.


That *is* the point, i.e. why is <ctime> not <time>.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #28

"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
news:Xn******** *************** ***********@130 .133.1.4...
Mike Wahler wrote in news:u7f8d.2569 $M05.241
@newsread3.news .pas.earthlink. net in comp.lang.c++:
I don't even like having to explain
why <ctime> is used instead of just <time>.


I think you meant <time.h>. There is no <time> header in
either language.


That *is* the point, i.e. why is <ctime> not <time>.


Because as far as I can tell, the 'c' in the name
is to denote that the header comes from the portion
of the C library inherited by the C++ library.

-Mike
Jul 22 '05 #29
White Wolf wrote:

I see. I do, since I do not believe that this is the only way of
failure waiting there.

If you see a problem, point it out. Don't just hint.

Brian Rodenborn
Jul 22 '05 #30

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
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...
1
10398
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
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
5554
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...
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.