DateTime equivalent struct with start/end properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anders Borum

    #1

    DateTime equivalent struct with start/end properties

    Hello!

    As part of a refactoring iteration, I was looking at consolidating two
    properties (Start and EndDate) to a single structure that would allow a
    single access point for the "duration" (so to speak).

    I considered using the TimeSpan, but that structure doesn't provide the
    boundaries for the "duration", only the length of the "duration" itself.

    The obvious step next would be creating my own structure that provided these
    boundaries - except that I have a feeling this is already present in the
    framework (or sounds like it should be). Am I looking for something that
    doesn't exist? :)

    Thanks in advance!

    --
    With regards
    Anders Borum / SphereWorks
    Microsoft Certified Professional (.NET MCP)


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: DateTime equivalent struct with start/end properties

    Anders,

    Why not something like this:

    public struct Duration
    {
    public DateTime Start;
    public DateTime End;
    }

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Anders Borum" <anders@spherew orks.dk> wrote in message
    news:eiH9XC1BGH A.3584@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hello!
    >
    > As part of a refactoring iteration, I was looking at consolidating two
    > properties (Start and EndDate) to a single structure that would allow a
    > single access point for the "duration" (so to speak).
    >
    > I considered using the TimeSpan, but that structure doesn't provide the
    > boundaries for the "duration", only the length of the "duration" itself.
    >
    > The obvious step next would be creating my own structure that provided
    > these boundaries - except that I have a feeling this is already present in
    > the framework (or sounds like it should be). Am I looking for something
    > that doesn't exist? :)
    >
    > Thanks in advance!
    >
    > --
    > With regards
    > Anders Borum / SphereWorks
    > Microsoft Certified Professional (.NET MCP)
    >[/color]


    Comment

    • William Stacey [MVP]

      #3
      Re: DateTime equivalent struct with start/end properties

      I put up a DateRange and SqlDateRange classes and DateFunction static class
      helper lib at http://channel9.msdn.com/ShowPost.aspx?PostID=147390 that does
      this. You can ignore all the sql stuff and just look at DateRange class for
      example of doing this.
      --
      William Stacey [MVP]

      "Anders Borum" <anders@spherew orks.dk> wrote in message
      news:eiH9XC1BGH A.3584@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Hello!
      >
      > As part of a refactoring iteration, I was looking at consolidating two
      > properties (Start and EndDate) to a single structure that would allow a
      > single access point for the "duration" (so to speak).
      >
      > I considered using the TimeSpan, but that structure doesn't provide the
      > boundaries for the "duration", only the length of the "duration" itself.
      >
      > The obvious step next would be creating my own structure that provided
      > these boundaries - except that I have a feeling this is already present in
      > the framework (or sounds like it should be). Am I looking for something
      > that doesn't exist? :)
      >
      > Thanks in advance!
      >
      > --
      > With regards
      > Anders Borum / SphereWorks
      > Microsoft Certified Professional (.NET MCP)
      >[/color]


      Comment

      • Lavinci@gmail.com

        #4
        Re: DateTime equivalent struct with start/end properties

        In .Net 2.0 framework, try the System.Diagnost ics.StopWatch Class

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: DateTime equivalent struct with start/end properties

          <Lavinci@gmail. com> wrote:[color=blue]
          > In .Net 2.0 framework, try the System.Diagnost ics.StopWatch Class[/color]

          That just gives an elapsed time as far as I can see - and not one that
          you can change manually.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Anders Borum

            #6
            Re: DateTime equivalent struct with start/end properties

            Hello!

            I already use the StopWatch class in another part of the application and I
            don't think using this type as a public property on types that will be
            instantiated really often (i.e. thousands).

            The implementation used a start / end date, but businesslogic implied that
            the starting date should be less than the ending date (obviously). This
            caused a validation exception to be thrown if the programmer was assigning a
            startdate greater than enddate and so on .. as part of setting i.e. a future
            duration.

            So I wrote some sample lines of C# to see, how the API would make the most
            sense in client code (the Brad Abrams way), and decided to change the
            interface.

            A dedicated structure to handle this is probably the way to go. I was
            looking for a native BCL class, but will probably have to wait until 3.0 to
            see this :)

            Thanks for the input!

            --
            Venlig hilsen
            Anders Borum / SphereWorks
            Microsoft Certified Professional (.NET MCP)


            Comment

            • Anders Borum

              #7
              Re: DateTime equivalent struct with start/end properties

              Great!

              I'll get the code and see what you've been implemented. However, we've
              already created a custom structure here that works quite well but it's
              always interesting to see other solutions.

              Thanks for all the input.

              --
              Venlig hilsen
              Anders Borum / SphereWorks
              Microsoft Certified Professional (.NET MCP)


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: DateTime equivalent struct with start/end properties

                Anders,
                In addition to the other comments.

                I have a Range(Of T), DateRange & TimeRange available at:





                One should be able to easily convert the above to C# if needed.

                My DateRange will ignore the respective Time parts of the DateTime values,
                while the TimeRange will ignore the respective Date parts, plus it handles
                ranges spanning midnight.

                --
                Hope this helps
                Jay [MVP - Outlook]
                ..NET Application Architect, Enthusiast, & Evangelist
                T.S. Bradley - http://www.tsbradley.net


                "Anders Borum" <anders@spherew orks.dk> wrote in message
                news:eiH9XC1BGH A.3584@TK2MSFTN GP11.phx.gbl...
                | Hello!
                |
                | As part of a refactoring iteration, I was looking at consolidating two
                | properties (Start and EndDate) to a single structure that would allow a
                | single access point for the "duration" (so to speak).
                |
                | I considered using the TimeSpan, but that structure doesn't provide the
                | boundaries for the "duration", only the length of the "duration" itself.
                |
                | The obvious step next would be creating my own structure that provided
                these
                | boundaries - except that I have a feeling this is already present in the
                | framework (or sounds like it should be). Am I looking for something that
                | doesn't exist? :)
                |
                | Thanks in advance!
                |
                | --
                | With regards
                | Anders Borum / SphereWorks
                | Microsoft Certified Professional (.NET MCP)
                |
                |


                Comment

                Working...