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
    #1
    1. Advertising

  2. 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.
    Jacek Dziedzic, Dec 17, 2006
    #2
    1. Advertising

  3. rossum Guest

    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
    #3
  4. David Harmon Guest

    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.
    David Harmon, Dec 17, 2006
    #4
  5. Greg Guest

    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
    Greg, Dec 17, 2006
    #5
  6. David Harmon Guest

    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
    #6
  7. 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
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    591
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UmV6YQ==?=
    Replies:
    3
    Views:
    5,517
    Craig Deelsnyder
    Mar 2, 2004
  3. Quentin Huo
    Replies:
    3
    Views:
    645
    Shiva
    Sep 15, 2004
  4. Karunakararao
    Replies:
    0
    Views:
    320
    Karunakararao
    Oct 6, 2004
  5. barry

    DataTime string format

    barry, Feb 14, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,458
    Kevin Spencer
    Feb 14, 2006
Loading...

Share This Page