a question about c++ datatime

C

could.net

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

Jacek Dziedzic

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

rossum

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
 
D

David Harmon

On 17 Dec 2006 03:34:01 -0800 in comp.lang.c++, (e-mail address removed)
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.
 
G

Greg

David said:
On 17 Dec 2006 03:34:01 -0800 in comp.lang.c++, (e-mail address removed)
wrote,

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
 
D

David Harmon

On 17 Dec 2006 05:30:37 -0800 in comp.lang.c++, "Greg"
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.
 
M

Michal Nazarewicz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top