how to capture time interval?

  • Thread starter Thread starter SJ
  • Start date Start date
S

SJ

howdy,

In vb6 I could say

If Time >#4:00:00 PM# And Time < #4:01:00 PM# Then
'Do Something
End If

Well, I don't see the Time function in vb.net. I have
experimented with the TimeSpan object for capturing
durations of things. And I tried the following with no
luck

If Now.TimeOfDay > #16:00:00# And Now.TimeOfDay <
#16:01:00# Then...

But got the error message that "<" and ">" are not defined
for a timespan. How can I perform the vb6 operation above
in vb.net?

Thanks
 
SJ,
I created a TimeRange class just for this purpose.

http://www.martinfowler.com/ap2/range.html
If Now.TimeOfDay > #16:00:00# And Now.TimeOfDay <
#16:01:00# Then...

Dim range As New TimeRange(#16:00:00#, #16:01:00#)

If range.Contains(Now) Then
...
End If

Something like:

Public Structure TimeRange

' Store the range as TimeSpans as the comparisons are "easier"
Private ReadOnly m_start As TimeSpan
Private ReadOnly m_finish As TimeSpan

' used to handle special case of the range spanning midnight
Private ReadOnly m_midnight As Boolean

Public Sub New(ByVal start As DateTime, ByVal finish As DateTime)
m_start = start.TimeOfDay
m_finish = finish.TimeOfDay
m_midnight = (TimeSpan.Compare(m_start, m_finish) > 0)
End Sub

Public ReadOnly Property Start() As DateTime
Get
Return DateTime.MinValue.Add(m_start)
End Get
End Property

Public ReadOnly Property Finish() As DateTime
Get
Return DateTime.MinValue.Add(m_finish)
End Get
End Property

Public Function Contains(ByVal value As DateTime) As Boolean
Dim timeOfDay As TimeSpan = value.TimeOfDay
If m_midnight Then
Return TimeSpan.Compare(m_start, timeOfDay) <= 0 OrElse
TimeSpan.Compare(timeOfDay, m_finish) <= 0
Else
Return TimeSpan.Compare(m_start, timeOfDay) <= 0 AndAlso
TimeSpan.Compare(timeOfDay, m_finish) <= 0
End If
End Function

End Structure

NOTE: I've defined the time range as being inclusive of the end points.

Hope this helps
Jay
 
Doh!

If you want the end points to be exclusive, then you can change test on the
TimeSpan.Compare lines in TimeRange.Contains:

Something like (untested):
Public Function Contains(ByVal value As DateTime) As Boolean
Dim timeOfDay As TimeSpan = value.TimeOfDay
If m_midnight Then
Return TimeSpan.Compare(m_start, timeOfDay) < 0 OrElse
TimeSpan.Compare(timeOfDay, m_finish) < 0
Else
Return TimeSpan.Compare(m_start, timeOfDay) < 0 AndAlso
TimeSpan.Compare(timeOfDay, m_finish) < 0
End If
End Function

Basically the value returned from TimeSpan.Compare is:
- less then 0 if the first timespan is less then the second
- equal 0 if the first timespan equals the second
- greater then 0 if the first timespan is greater then the second

Hope this helps
Jay
 
Thanks. This is wonderful! It does exactly what I need,
although I will tinker with it to understand what is going
on. Note: I love DotNet. I just have to assimilate why
this is superior to the way you do it in VB6 :-). I mean,
I realize that var typing is way stronger in DotNet and
this results in less ambiguity. Is this one of the
benefits of DotNet for this situation? In any event, many
thanks!
 
SJ,
I just have to assimilate why
this is superior to the way you do it in VB6 :-).
One can create a TimeRange class in VB6 almost as easily. The three things
that I would find missing, that does not allow the VB6 implementation to be
as robust as the VB.NET implementation below are:
1. No constructors (I cannot define the start & finish times when I create a
new value, I would need to set them afterwords).
2. No readonly fields (the fields can be changed within the VB6 class,
however Properties can be used to protect the values outside the class).
3. Type (aka Structure) do not allow methods, only Classes do.

I made TimeRange a Structure (VB6 Type) as I wanted it to:
- act like primitive types
- be immutable
- value semantics are desired

Its size should be under 16 bytes.

http://msdn.microsoft.com/library/d...genref/html/cpconValueTypeUsageGuidelines.asp

One feature that VB.NET has that VB6 also is missing are Shared methods. I
would consider adding a Shared Parsed method to TimeRange that is able to
take a string & convert it into a new TimeRange value. Then I would also
override the Object.ToString method in TimeRange to allow converting the
TimeRange into a readable format.
I realize that var typing is way stronger in DotNet and
this results in less ambiguity. Is this one of the
benefits of DotNet for this situation?

I consider being able to create value types such as TimeRange a benefit.

http://martinfowler.com/ieeeSoftware/whenType.pdf

I also consider the full OO nature of .NET when creating classes a major
benefit, such as constructors, inheritance, polymorphism (both inheritance &
implements), encapsulation...

Hope this helps
Jay
 
Wow! That was definitely a clear explanation. I guess my
issue is that I just don't have a broad enough of
experience yet, even with vb6, to appreciate all of this
functionality. Thus, I have to ask these questions
(humbly) and just assimilate.

Many thanks for your help and explanations.
 
SJ,
A few books you may want to consider (hopefully I don't scare you :-)):

Robin A. Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and
Microsoft Visual C# .NET - Step by Step" from Microsoft Press covers the how
of OOP, unfortunately not the why of OOP. I consider it a good entry level
book.

David West's book "Object Thinking" from Microsoft Press covers the why of
OOP and some of the intellectual how (at an abstract design level, not
implementation level as the first book). I consider this a more advanced
book.

Gang of Four's (GOF) book "Design Patterns - Elements of Reusable
Object-Oriented Software" from Addison Wesley, is IMHO a "must have" book
for the serious OO developer. Design Patterns provide programmers with a
convenient way to reuse object-origned code & concepts amount programmers
and across projects, offering easy, time-saving solutions to commonly
recurring problems in software design. The GOF are Erich Gamma, Richard
Helm, Ralph Johnson, and John Vlissides.

James W. Cooper's book "Visual Basic Design Patterns - VB 6.0 and VB.NET" is
also a "must have" book that is an excellent companion to the above GOF
book. Cooper's book gives the VB6 & VB.NET view of each pattern in the GOF
book.


Once you have a good solid handle on OO and Patterns I consider Martin
Fowler's two books "Refactoring" and "Patterns of Enterprise Application
Architecture" both from Addison Wesley to be informative reads.

I recently started reading Joshua Kerievsky's book "Refactoring to Patterns"
also from Addison Wesley which helps bridge the gap between the GOF Patterns
book & Martin's Refactoring book.

Another book I also recently starting reading is James W. Newkirk & Alexei
A. Vorontsov's book "Test-Driven Development in Microsoft .NET" which
explains a useful methodology (Test Driven Development or TDD) in the
context of .NET. I find that TDD simplifies the development process.

I'm sure I have some other books that I could recommend...

Yes, I am currently reading two books...

Hope this helps
Jay


SJ said:
Wow! That was definitely a clear explanation. I guess my
issue is that I just don't have a broad enough of
experience yet, even with vb6, to appreciate all of this
functionality. Thus, I have to ask these questions
(humbly) and just assimilate.

Many thanks for your help and explanations.
<<snip>>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top