Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a question about c++ datatime

Reply
Thread Tools

a question about c++ datatime

 
 
could.net@gmail.com
Guest
Posts: n/a
 
      12-17-2006
I met across a datetime problem when I tried to translate some delphi
code to cpp.
There's a struct called TTimeStamp in delphi,
and this is the cpp equivalent:

struct TTimeStamp
{
int Time; // the number of milliseconds that have elapsed since
midnight
int Date; // the number of days since 1/1/0001 plus one
};

My question is, I can only get the current date and time in cpp,
how can I figure out the corresponding TTimeStamp of the current time?

Thanks!

 
Reply With Quote
 
 
 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      12-17-2006
wrote:
> I met across a datetime problem when I tried to translate some delphi
> code to cpp.
> There's a struct called TTimeStamp in delphi,
> and this is the cpp equivalent:
>
> struct TTimeStamp
> {
> int Time; // the number of milliseconds that have elapsed since
> midnight
> int Date; // the number of days since 1/1/0001 plus one
> };
>
> My question is, I can only get the current date and time in cpp,
> how can I figure out the corresponding TTimeStamp of the current time?


Well, you can always do a little maths. Each year is 365 days,
each day is 86400000 ms.

Things to remember:
- leap years have a day extra (every 4 years, except when the year
is divisible by 100 and not by 400),
- change of calendar from Julian to Gregorian, if you need dates
from long ago,
- lack of year 0, if you need BCE dates,
- if you are really pedantic about this, additions of 61-st
second on New Years Day Eve, from time to time.

HTH,
- J.
 
Reply With Quote
 
 
 
 
rossum
Guest
Posts: n/a
 
      12-17-2006
On 17 Dec 2006 03:34:01 -0800, wrote:

>I met across a datetime problem when I tried to translate some delphi
>code to cpp.
>There's a struct called TTimeStamp in delphi,
>and this is the cpp equivalent:
>
>struct TTimeStamp
>{
> int Time; // the number of milliseconds that have elapsed since
>midnight
> int Date; // the number of days since 1/1/0001 plus one
>};
>
>My question is, I can only get the current date and time in cpp,
>how can I figure out the corresponding TTimeStamp of the current time?
>
>Thanks!

Run a quick Delphi program to determine what the TTimeStamp is for
some fixed date like 1/1/2000. Use that value as a constant to which
you add the number of days between then and the date you want.

Milliseconds after midnight should be pretty easy.

rossum

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      12-17-2006
On 17 Dec 2006 03:34:01 -0800 in comp.lang.c++,
wrote,
> int Time; // the number of milliseconds that have elapsed since
>midnight
> int Date; // the number of days since 1/1/0001 plus one


Note that neither of those have enough range to be very useful.
Should probably be instead.
long Time;
long Date;
and maybe unsigned.

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      12-17-2006

David Harmon wrote:
> On 17 Dec 2006 03:34:01 -0800 in comp.lang.c++,
> wrote,
> > int Time; // the number of milliseconds that have elapsed since
> >midnight
> > int Date; // the number of days since 1/1/0001 plus one

>
> Note that neither of those have enough range to be very useful.
> Should probably be instead.
> long Time;
> long Date;
> and maybe unsigned.


A "long" and an "int" are the likely the same (32-bit) size on the
target architecture. And with a 32-bit signed int, TTimeStamp has an
range in excess of 5,600,000 years and a precision of 0.001 seconds.

Greg

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      12-17-2006
On 17 Dec 2006 05:30:37 -0800 in comp.lang.c++, "Greg"
<> wrote,
>A "long" and an "int" are the likely the same (32-bit) size on the
>target architecture.


No guarantee of that. An int is 16 bits or possibly more;
if you actually need more then say so with long.

 
Reply With Quote
 
Michal Nazarewicz
Guest
Posts: n/a
 
      12-19-2006
writes:

> I met across a datetime problem when I tried to translate some delphi
> code to cpp.
> There's a struct called TTimeStamp in delphi,
> and this is the cpp equivalent:
>
> struct TTimeStamp
> {
> int Time; // the number of milliseconds that have elapsed since
> midnight
> int Date; // the number of days since 1/1/0001 plus one
> };
>
> My question is, I can only get the current date and time in cpp,
> how can I figure out the corresponding TTimeStamp of the current time?


Depending on what you really need you can try something like:

#v+
#include <ctime>

int main() {
long time = std::time(0);
long data = SOME_MAGIC_NUMBER + t / (3600 * 24);
time %= 2600 * 24;
time *= 1000;
return 0;
}
#v+

Where SOME_MAGIC_NUMBER is number of days between 01/01/0001 and start
of the epoch used in time() function. If you need more accuracy you
can try using some platform-specific functions like gettimeofday() on
POSIX.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTime string format barry ASP .Net 4 02-14-2006 04:12 PM
DATATIME PATTERN based on regional optional settings Karunakararao ASP .Net 0 10-06-2004 03:52 PM
How to make a DataTime object from a string? Quentin Huo ASP .Net 3 09-15-2004 04:15 PM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Datagrid datetime: empty datatime column shows "1/1/0001", how to avoid this? =?Utf-8?B?UmV6YQ==?= ASP .Net 3 03-02-2004 06:27 PM



Advertisments