473,473 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

a question about c++ datatime

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 '06 #1
6 1626
co*******@gmail.com 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.
Dec 17 '06 #2
On 17 Dec 2006 03:34:01 -0800, co*******@gmail.com 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

Dec 17 '06 #3
On 17 Dec 2006 03:34:01 -0800 in comp.lang.c++, co*******@gmail.com
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.

Dec 17 '06 #4

David Harmon wrote:
On 17 Dec 2006 03:34:01 -0800 in comp.lang.c++, co*******@gmail.com
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

Dec 17 '06 #5
On 17 Dec 2006 05:30:37 -0800 in comp.lang.c++, "Greg"
<gr****@pacbell.netwrote,
>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.

Dec 17 '06 #6
co*******@gmail.com 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--
Dec 19 '06 #7

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

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
3
by: Sun | last post by:
Hi all, I am inserting datatime value into mysql table, while select value its giving 0000-00-00 00:00:00 value. mysql> insert into dtest values('02-17-05 17:08:02'); Query OK, 1 row...
8
by: Zibi | last post by:
I have some problem with datatime. SELECT * FROM stat WHERE data > '2005-05-24 14:07:28' ORDER BY id Asc I got the error: Microsoft OLE DB Provider for SQL Server error '80040e07' The...
14
by: expertware | last post by:
Ok! to avoid confusion I will start a new argument. Thanks!! FIREFOX 1.0.7 AND IE6 viewed through DATATIME: a summary REPORT ===============================================================...
1
by: DongningLi | last post by:
I have raw data from one query that has everything in. Now I'm about to put a summary in my report. Which way is a better way to present my summary? 1. do a pivot table in my query and link...
0
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...
0
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,...
0
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.