Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / May 2004

Tip: Looking for answers? Try searching our database.

When 10 minutes before midnight is out of range.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dennis D. - 30 May 2004 17:09 GMT
VB.net does not seem to have adequate structure for handling time
within it's own code.
Subtract 15 minutes from 00:00 AM, and an out of range condition results.
Subtract 15 minutes from 12:00 PM, and the result is 11:45 AM,
as it should be.
Seems one half of the process is missing, and it is the half that requires
the most thought. Without it, we are left to hand code the whole process.
Time has a standard, and it should be properly implemented in the language.
Time is also one of the most commonly used routines in programming.
While I could work around the VB time construct programmatically, I feel
that the issue should be addressed. At least it should work with local time,
with only UTC conversion requiring additional code.

Why is it the way it is?

Here's the code:

Private Sub btnAction_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAction.Click
       'begin StartText
       Dim StartText As String = txbStart.Text
       Dim HourParse As String = Mid(StartText, 1, 2)
       Dim HourDbl As Double = Val(HourParse)
       Dim HourInt As Integer = CInt(HourDbl)
       Dim MinParse As String = Mid(StartText, 4, 2)
       Dim MinDbl As Double = Val(MinParse)
       Dim MinInt As Integer = CInt(MinDbl)
       Dim SecInt As Integer = 0
       Dim FunctionTime As Date = TimeSerial(HourInt, MinInt, SecInt)
       Dim StartLabelText As String = Format(FunctionTime, "hh:mm tt")
       'Set Start Time Label
       lblStartUpR.Text = StartLabelText
       'end StartText
       'begin PreStart
       Dim PreStart As String = txbPreStart.Text
       HourParse = Mid(PreStart, 1, 2)
       HourDbl = Val(HourParse)
       HourInt = CInt(HourDbl)
       MinParse = Mid(PreStart, 4, 2)
       MinDbl = Val(MinParse)
       MinInt = CInt(MinDbl)
       'Begin Calc
       Dim MinusHour As Double = HourDbl * -1
       Dim MinusMin As Double = MinDbl * -1
       Dim PreStartHour As Date = DateAdd(DateInterval.Hour, MinusHour,
CDate(txbStart.Text))
       Dim PreStartHourDbl As Double = Val(PreStartHour)
       Dim PreStartHourInt As Integer = CInt(PreStartHourDbl)
       Dim PreStartMin As Date = DateAdd(DateInterval.Minute, MinusMin,
CDate(txbStart.Text))
       Dim PreStartMinDbl As Double = Val(PreStartMin)
       Dim PreStartInt As Integer = CInt(PreStartMinDbl)
       FunctionTime = TimeSerial(HourInt, MinInt, SecInt)
       StartLabelText = Format(FunctionTime, "hh:mm tt")
       lblArrivalR.Text = PreStartMin.ToShortTimeString
   End Sub

txbStart is a textbox. The time is entered in 24hour format as hh:mm.
lblStartUpR is a label that displays the time in short time format

txbPreStart is a textbox taking the same format.
lblArrivalR is a label that displays the time in short time format that is a
result of the following calculation:

txbPreStart is subtracted (by addition of a minus value) from txbStart

'Should I write simpler and tighter code to throw the exception?
Armin Zingler - 30 May 2004 18:10 GMT
> VB.net does not seem to have adequate structure for handling time
>  within it's own code.
[quoted text clipped - 12 lines]
>
> Why is it the way it is?

You must distinguish between a certain point in time and a Timespan. The
first is expressed by a Date (DateTime) value and the second by a Timespan
(System.TimeSpan) value. The time within a day is also expressed by a
Timespan (the timespan since midnight) as you see by having a look at the
data type of DateTime.TimeOfDay.

A certain point in time is composed of a date and a time. The smallest value
is DateTime.MinValue (01/01/0001 00:00) and the largest ist
DateTime.MaxValue.

So, your fault is to use a DateTime variable to store the time of the day.
When you use

PreStartHour As Date = DateAdd(DateInterval.Hour, MinusHour,
CDate(txbStart.Text))

then you are trying to subtract 1 hour from 01/01/0001 00:00. This is not
possible because this date is already the smallest value possible for a
DateTime structure.

I don't understand the purpose of your procedure, but I suggest to use the
members of the DateTime and TimeSpan structures to convert from/to
String/DateTime/Timespan, like
- DateTime.Parse, DateTime.ParseExact
- DateTime.Add, DateTime.Subtract
- Constructors for DateTime and TimeSpan
- TimeSpan.Add
and all the other members.

Maybe you could describe the purpose of the procedure and I can show you how
I would implement it.

Signature

Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Dennis D. - 31 May 2004 01:05 GMT
> > VB.net does not seem to have adequate structure for handling time
> >  within it's own code.
[quoted text clipped - 44 lines]
> Maybe you could describe the purpose of the procedure and I can show you how
> I would implement it.

That's my point exactly.
A date is assumed, or you must give the program a date
(as January 01, 2001).
But when is not 00:00 minus 00:15 not 23:45 on a
24 hour clock? It is always 23:45!
So why go through all this code to arrive at a simple fact?
It should require 2 lines of code at worst to say:
00:00 - 00:15 == 23:45
Because at this point no one has assumed anything.
No one has said Daylight savings, or past the Prime Meridian.
If I wanted to specifiy something extra, such as DST or UTC,
then I could understand the difficulty of being required to
provide more information.
But a time type should have some simple unassuming
assumptions, that if you move past the negative boundry it
subtracts one unit from the parent, whatever that is.
0 - 1 = -1, how difficult is that?
In time: 00:00 - 00:15 = 23:45, or
00:00 - 01:01 = 23:59, seems simple enough to me.
That's not how it works?
I know.
That is why I am recommending action.
A high level language shouldn't have a problem with this.
Dennis D. - 31 May 2004 02:39 GMT
oops: 00:00 - 01:01 = 22:59
I think. Brain Fart!

> > > VB.net does not seem to have adequate structure for handling time
> > >  within it's own code.
[quoted text clipped - 70 lines]
> That is why I am recommending action.
> A high level language shouldn't have a problem with this.
Armin Zingler - 31 May 2004 11:01 GMT
IMO your logical approach is not completely correct. You're saying that
00:00 - 01:01 = 22:59. That's only 1/2 truth. The whole truth is that it is
"22:59 the day before". This can be expressed by a Timespan:

dim ts as new timespan
ts = ts.subtract(new timespan(1, 1, 0))    '00:00
msgbox ts.tostring    '=> -01:01:00

What would happen if you subtract 25 hours and 1 minute? Your result would
*also* be 22:59. So, subtracting 1 hour or subtracting 25 hours would lead
to the *same* result? I don't think it is correct because you are ignoring
the day. Do you really want to ignore the day? I'm almost sure you can't
because there will probably any calculation using the result of the
subtraction. Won't it? What are you going to do with the result of the
subtraction?

Signature

Armin

> oops: 00:00 - 01:01 = 22:59
> I think. Brain Fart!
[quoted text clipped - 86 lines]
> > That is why I am recommending action.
> > A high level language shouldn't have a problem with this.
Cor Ligthert - 31 May 2004 11:31 GMT
Hi Armin,

First time I see you in this newsgroup.

Did you see that Dennis told he saw it wrong?

Cor
Armin Zingler - 31 May 2004 12:03 GMT
> Hi Armin,
>
> First time I see you in this newsgroup.
>
> Did you see that Dennis told he saw it wrong?

No, Cor. I read his post now again, but I dont' see it. What do you mean?

Signature

Armin

Cor Ligthert - 31 May 2004 14:02 GMT
Hi Armin,

By Dennis as answer on his own message which was an answer to you.

>>oops: 00:00 - 01:01 = 22:59
>>I think. Brain Fart!

Although your message is of course a good addition.

Cor
Dennis D. - 31 May 2004 16:17 GMT
What are you going to do with the result of the
> subtraction?

Ever notice that most clocks do not have day or date features?
So why should an object oriented programming language that types a time
function ignore that fact, and require a date?
Cor Ligthert - 31 May 2004 16:36 GMT
Hi Dennis,

Exacly here you show the superiority of human on a computer.

> Ever notice that most clocks do not have day or date features?
> So why should an object oriented programming language that types a time
> function ignore that fact, and require a date?

We do not have to know that, millions years of evolution have learned us the
changing of the day.

However the clock is not such a natural thing and now we need a clock for
that to try to get those times.

Here a link for that

http://www.ernie.cummings.net/clock.htm

Just my thought,

Cor
Armin Zingler - 31 May 2004 16:47 GMT
>  What are you going to do with the result of the
> > subtraction?
>
> Ever notice that most clocks do not have day or date features?
> So why should an object oriented programming language that types a
> time function ignore that fact, and require a date?

On which clock do you subtract hours? Even if you can, there are two cases:
Either you can only subtract a maximum of 1 day, or you can subtract more
than 24 hours.
The former case: Simply add 24 hours if the result is <0, that's what the
clock also would have to do, not to get negative values.
The latter case: There must be a date somewhere, otherwise subtracting 1
hour or 25 hours would be the same.

Dim ts As New TimeSpan
ts = ts.Subtract(New TimeSpan(1, 1, 0))
If ts.Ticks < 0 Then
   ts = ts.Add(New TimeSpan(-ts.Days + 1, 0, 0, 0))
End If
MsgBox(ts.Hours.ToString("00") & ":" & ts.Minutes.ToString("00"))

I would agree that the Timespan's ToString method should be overloaded and
accept a format string.

Signature

Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Dennis D. - 31 May 2004 17:10 GMT
Thanks Armin, Cor:

Have a great day!

Dennis,
http://www.dennisys.com/

>  What are you going to do with the result of the
> > subtraction?
>
> Ever notice that most clocks do not have day or date features?
> So why should an object oriented programming language that types a time
> function ignore that fact, and require a date?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.