# a question about c++ datatime

Discussion in 'C++' started by could.net@gmail.com, Dec 17, 2006.

1. ### Guest

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!

, Dec 17, 2006

2. ### Jacek DziedzicGuest

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,
second on New Years Day Eve, from time to time.

HTH,
- J.

Jacek Dziedzic, Dec 17, 2006

3. ### rossumGuest

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

rossum, Dec 17, 2006
4. ### David HarmonGuest

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.
long Time;
long Date;
and maybe unsigned.

David Harmon, Dec 17, 2006
5. ### GregGuest

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.
> 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

Greg, Dec 17, 2006
6. ### David HarmonGuest

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.

David Harmon, Dec 17, 2006
7. ### Michal NazarewiczGuest

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

Michal Nazarewicz, Dec 19, 2006