473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

.NET 2.0: Fraction support broken for DateTime?

I just made a pretty strange observation with .NET 2.0 -
DateTime.AddMilliseconds(double) and AddSeconds(double) seem to be
broken:

DateTime now = new DateTime();
DateTime t2 = now.AddMilliseconds(1.5);
Console.Out.WriteLine(t2.TimeOfDay);

The code above rounds the fraction to full milliseconds and results in
this output:
00:00:00.0020000
However, if I use AddTicks, the result is as expected:

DateTime now = new DateTime();
DateTime t2 = now.AddTicks(15000);
Console.Out.WriteLine(t2.TimeOfDay);

Output: 00:00:00.0015000
IMHO, AddMillisecond should work completely different. From the
documentation of AddMilliseconds:

<quote>
The fractional part of value is the fractional part of a millisecond.
For example, 4.5 is equivalent to 4 milliseconds, and 5000 ticks, where
one millisecond = 10000 ticks.
</quote>

However, I just can't imagine that there is such a severe bug in the
framework. Any ideas?

Cheers,
Philipp

Jul 3 '06 #1
4 3650

su***@freesurf.ch wrote:
I just made a pretty strange observation with .NET 2.0 -
DateTime.AddMilliseconds(double) and AddSeconds(double) seem to be
broken:

DateTime now = new DateTime();
DateTime t2 = now.AddMilliseconds(1.5);
Console.Out.WriteLine(t2.TimeOfDay);

The code above rounds the fraction to full milliseconds and results in
this output:
00:00:00.0020000
However, if I use AddTicks, the result is as expected:

DateTime now = new DateTime();
DateTime t2 = now.AddTicks(15000);
Console.Out.WriteLine(t2.TimeOfDay);

Output: 00:00:00.0015000
IMHO, AddMillisecond should work completely different. From the
documentation of AddMilliseconds:

<quote>
The fractional part of value is the fractional part of a millisecond.
For example, 4.5 is equivalent to 4 milliseconds, and 5000 ticks, where
one millisecond = 10000 ticks.
</quote>

However, I just can't imagine that there is such a severe bug in the
framework. Any ideas?
>From looking at Reflector I would be inclined to agree this is a bug.
AddMilliseconds, AddSeconds, AddMinutes and AddHours all privately call
Add, which takes a number and a 'scale' parameter. For AddMilliseconds,
scale is 1:

public DateTime AddMilliseconds(double value)
{
return this.Add(value, 1);
}
This is what Add looks like:

private DateTime Add(double value, int scale)
{
long num1 = (long) ((value * scale) + ((value >= 0) ? 0.5 :
-0.5));
// bounds checking snipped

return this.AddTicks(num1 * 0x2710);
}
>From which it seems clear that the fractional part of the argument to
AddMilliseconds is simply thrown away.
--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 3 '06 #2
Larry,

Thanks for the fast reply! I just checked it with .NET 1.1 and the
behaviour is the same. I'm really surprised that this hasn't been
reported and fixed for 2.0 yet. I guess this indicates that .NET still
plays a minor role when it comes to real-time applications...

Cheers,
Philipp

Jul 4 '06 #3
su***@freesurf.ch wrote:
Thanks for the fast reply! I just checked it with .NET 1.1 and the
behaviour is the same. I'm really surprised that this hasn't been
reported and fixed for 2.0 yet. I guess this indicates that .NET still
plays a minor role when it comes to real-time applications...
That's certainly true, as .NET isn't an appropriate platform for
real-time applications for various reasons. (Heck, most if not all
versions of Windows aren't appropriate for running real-time
applications.) Of course, it's possible that we're thinking of
different definitions of "real-time applications".

Jon

Jul 4 '06 #4
It's indeed not "real real-time" what I'm talking about. While we do
some stuff in "real time", we don't work with intervals below 100ms -
which is horribly slow for the hardware guys ;-)

However, we're controlling external hardware that provides realtime
functionality via a .NET application, and just retrieve the data for
analysis. This makes a nice couple.

Cheers,
Philipp

Jul 4 '06 #5

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

Similar topics

42
by: cody | last post by:
public DateTime Value { get { try { return new DateTime(int.Parse(tbYear.Text), int.Parse(tbMonth.Text), int.Parse(tbDay.Text)); } catch (FormatException)
13
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
4
by: M. Uppal | last post by:
How do i convert fraction to time in c#. I need to convert 0.25 to time. Excel does this by using Selection.NumberFormat = "h:mm:ss;@" of the Cell Format function. thanks, M. Uppal
0
by: Marius Tennes Krogh | last post by:
Hi! I've written some code in my globala.asax application_error event to send me some email whenever an error occur. I send the errormessage, stacktrace, etc. and I also list all the form elements...
0
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
1
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.