473,395 Members | 1,613 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.

TimeSpan / 2

IS there a reason Timespan doesn't have an operator for divide? Being that
it's an amount of time I would have thought it should. Really it's just a
wrapper around a double variable?

Thanks,
Michael

Jan 24 '08 #1
11 6058
Michael,

It's not really a wrapper around a double variable. More like a 64 bit
integer.

You can always get the value of the Ticks property, divide it by two,
and create a new TimeSpan instance from that.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael C" <no****@nospam.comwrote in message
news:eg**************@TK2MSFTNGP04.phx.gbl...
IS there a reason Timespan doesn't have an operator for divide? Being that
it's an amount of time I would have thought it should. Really it's just a
wrapper around a double variable?

Thanks,
Michael
Jan 24 '08 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:B9**********************************@microsof t.com...
Michael,

It's not really a wrapper around a double variable. More like a 64 bit
integer.
Good point.
You can always get the value of the Ticks property, divide it by two,
and create a new TimeSpan instance from that.
Yes, in my case I just got total seconds and divided by 2 but I'm still
curious as to why there is no operator for divide. I guess seeing no one
came up with a reason that means the reason is MS simply forgot.

Michael
Jan 25 '08 #3
On Thu, 24 Jan 2008 16:55:16 -0800, Michael C <mi**@nospam.comwrote:
[...] I guess seeing no one
came up with a reason that means the reason is MS simply forgot.
At best, it means that no one reading your post knows the answer.

You can't infer anything about the actual reason from a lack of responses.

Pete
Jan 25 '08 #4
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
At best, it means that no one reading your post knows the answer.
Got a reason then?
Jan 25 '08 #5
On Thu, 24 Jan 2008 18:25:46 -0800, Michael C <mi**@nospam.comwrote:
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>At best, it means that no one reading your post knows the answer.

Got a reason then?
Why do I need an answer to the question just to point out that there's no
way (at present) to know the answer to the question? What would the point
be in pointing out that we don't know the reason if I actually knew the
reason?
Jan 25 '08 #6
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>Got a reason then?

Why do I need an answer to the question just to point out that there's no
way (at present) to know the answer to the question? What would the point
be in pointing out that we don't know the reason if I actually knew the
reason?
Well, I'm sure if there was a reason a smart guy like you would know it.
Jan 25 '08 #7
Michael C wrote:
Yes, in my case I just got total seconds and divided by 2 but I'm still
curious as to why there is no operator for divide. I guess seeing no one
came up with a reason that means the reason is MS simply forgot.
I am with Peter on this, just because it wasn't done doesn't mean they forgot.

The possibility which makes the most sense to me relates to code clarity. Making
you use a property of the TimeSpan makes it perfectly clear what property you
are doing the math on, and what your expected result would be.

I'm not saying it's a perfect reason to not do it, I'm just guessing and don't
know any more than the people who have already spoken up.
Chris.
Jan 25 '08 #8
"Chris Shepherd" <ch**@nospam.chsh.cawrote in message
news:ug**************@TK2MSFTNGP04.phx.gbl...
I am with Peter on this, just because it wasn't done doesn't mean they
forgot.

The possibility which makes the most sense to me relates to code clarity.
Making you use a property of the TimeSpan makes it perfectly clear what
property you are doing the math on, and what your expected result would
be.

I'm not saying it's a perfect reason to not do it, I'm just guessing and
don't know any more than the people who have already spoken up.
To me it made such perfect sense that I went and coded it fully expecting it
to work. I was quite suprised some time later to get a compile error. The
alternative:

TimeSpan ts2 = new TimeSpan(ts1.Ticks / 2)

appears much more complicated and although I presume that is the correct way
to do it I'm still not certain. Should I convert it to Milliseconds and back
or maybe seconds? Should I be using a double variable or an integer or long?

The only thing I can think of is that doing "ts1 / 2" it's not clear how it
will round but I don't think that's much of an issue. Obviously programmers
will expect it to have a certain level of resolution and that dividing by 2
could cause some minor loss of information.

Michael
Jan 29 '08 #9
Michael C wrote:
To me it made such perfect sense that I went and coded it fully expecting it
to work. I was quite suprised some time later to get a compile error. The
alternative:

TimeSpan ts2 = new TimeSpan(ts1.Ticks / 2)

appears much more complicated and although I presume that is the correct way
to do it I'm still not certain. Should I convert it to Milliseconds and back
or maybe seconds? Should I be using a double variable or an integer or long?

The only thing I can think of is that doing "ts1 / 2" it's not clear how it
will round but I don't think that's much of an issue. Obviously programmers
will expect it to have a certain level of resolution and that dividing by 2
could cause some minor loss of information.
If you divide one int by another, the result is not rounded, it is truncated. If
you divide 99 by 100 the integer result is 0. This is likely why a TimeSpan,
which is purported by others to be just a wrapper for Int64, wouldn't have a
divisor or multiplier implementation.
If you need to ensure rounding occurs, you'll need to store it in a more precise
type and then round it back to an integer.

Chris.
Jan 29 '08 #10
"Chris Shepherd" <ch**@nospam.chsh.cawrote in message
news:Ob**************@TK2MSFTNGP05.phx.gbl...
If you divide one int by another, the result is not rounded, it is
truncated.
I was aware of this, I just used the wrong word.
If you divide 99 by 100 the integer result is 0. This is likely why a
TimeSpan, which is purported by others to be just a wrapper for Int64,
wouldn't have a divisor or multiplier implementation.
If you need to ensure rounding occurs, you'll need to store it in a more
precise type and then round it back to an integer.
This was something I suggested a while ago. If you're timing something that
does 1,000,000 iterations then you might find that when you divide the
timespan by 1,000,000 that you get zero. I don't really think this is a
problem as many other timing methods can result in zero also, eg using
GetTickCount.
>
Chris.

Jan 29 '08 #11
"Israel" <is**********@hotmail.comwrote in message
news:1c**********************************@f47g2000 hsd.googlegroups.com...
I think if the "TimeSpan" is going to be the universal representation
of a vector in the time then it makes perfect sense to have a multiply
and divide operator on it.
At least I'm not going mad. I guess the nature of newsgroups is that
generally those who agree don't reply. :-)
If I want to use it for a sampling
interval which may dynamically change based on a multiplier then I'm
forced to write extra code to do what seems like an natural
extension. Why even use a TimeSpan in that case? It's almost cleaner
to just use a tick count since I can add ticks to a DateTime to get a
new DateTime.
Yes. If you want to divide TimeSpan by 2 to create another TimeSpan then you
need to know how it implements itself internally anyway.

Michael
Feb 1 '08 #12

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

Similar topics

2
by: Jeff Shantz | last post by:
Hello, I'm developing a large statistics application for a call center. It often needs to calculate time spanning over months. For example, an agent's total talk time within 30 days. Since an...
3
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
2
by: DWalker | last post by:
In Visual Studio (Visual Basic) .NET 2002, I noticed that this: Dim Elapsed as DateTime = Now - Now gives a "compile time" error (error in the IDE), saying that the '-' operator is not...
10
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
2
by: ucasesoftware | last post by:
i translate a C# funtion to VB.NET and i have this : Shared Function isAllDay(ByVal ap As Appointment) As Boolean Return ap.DateBegin.TimeOfDay = TimeSpan.Zero AndAlso ap.DateEnd.TimeOfDay =...
2
by: Dennis D. | last post by:
Hello: I want to subtract a timespan from the selectionrange.start of a month calendar. The selectionrange.start of a month calendar is a date, and it is possible to subtract a timespan from a...
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
5
by: style | last post by:
Hello In my custom configuration section, I'd like to specify an interval attribute which specifies an interval in milliseconds. I think it would be very comfortable to get this value directly...
4
by: Massimo | last post by:
Hi to All, i'm using C# in .NET 2.0 and i have a DataTable A with a column of type TimeSpan used to store HOUR info. I'm trying to filter my DataTable A selecting only rows that have the column...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.