getting current DateTime

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 2 03:21:36 PST 2015


On Wednesday, December 31, 2014 19:07:13 bitwise via Digitalmars-d-learn wrote:
> > Why do you need DateTime and not SysTime?
> >
> > I'm actually surprised it doesn't have that too...
> >
> > -Steve
>
> Initially I was looking for something that would be the
> equivalent to DateTime in C#. This now appears to be SysTime, not
> DateTime in D.
>
> It seems the only real reason to use DateTime is as an
> optimization.
>
>  From DateTime docs:
> "It is optimized for calendar-based operations and has no concept
> of time zone."

That and it gets used internally by SysTime for any calendar-based
operations that it has to do, so it encapsulates that functionality, but if
what you're looking for is something that represents the time from the
system rather than a date and time on a calendar, you want SysTime.

The main places that code is likely to use DateTime is if it needs to
operate on dates and times separately from any connection to the system's
time or time zones and when you want to get the separate pieces of the
date and time (year, month, etc.) efficiently, because converting to
DateTime calculates that information once, whereas calling each individual
property on SysTime results in having to do some of the calculations
multiple times if you want to access multiple of the properties (which
sometimes makes me think that I never should have put those properties on
SysTime in the first place, but it's convenient if you don't care about the
small loss of efficiency when duplicating some of the calculations to get
multiple of the properties or you only need one or two of the properties).

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list