Work around for TimeSpan parsing 24:00
Asked Answered
B

4

7

I have a small issue with the TimeSpan class where it can parse 23:59 but not 24:00.

Of course the client wants to enter 24:00 to indicate the end of the day rather than 23:59 or 00:00 as 00:00 indicates the start of the day.

Currently my code parses the end time like so:

if ( !TimeSpan.TryParse( ( gvr.FindControl( "txtTimeOff" ) as TextBox ).Text, out tsTimeOff ) )
{
   this.ShowValidationError( String.Format( "Please enter a valid 'Time Off' on row '{0}'.", gvr.RowIndex + 1 ) );
   return false;
}

Whats the best work around for this situation?

EDIT: (Solution 1)

if ( ( gvr.FindControl( "txtTimeOff" ) as TextBox ).Text == "24:00" )
{
    tsTimeOff = new TimeSpan( 24, 0, 0 );
}
else
{
    if ( !TimeSpan.TryParse( ( gvr.FindControl( "txtTimeOff" ) as TextBox ).Text, out tsTimeOff ) )
    {
         this.ShowValidationError(
            String.Format( "Please enter a valid 'Time Off' on row '{0}'.", gvr.RowIndex + 1 ) );
         return false;
    }
}

EDIT: (solution2)

string timeOff = ( gvr.FindControl( "txtTimeOff" ) as TextBox ).Text;

if ( !TimeSpan.TryParse(
        timeOff == "24:00" ? "1.00:00:00" : timeOff
        , out tsTimeOff ) )
{
    this.ShowValidationError(
        String.Format( "Please enter a valid 'Time Off' on row '{0}'.", gvr.RowIndex + 1 ) );
    return false;
}
Bard answered 15/9, 2011 at 10:9 Comment(2)
You could simply handle the string "24:00" as special case..Davy
@Davy - thanks, Im going to try both options suggestedBard
N
8

try something like this

textBox = (TextBox) gvr.FindControl ("txtTimeOff");

TimeSpan.TryParse (textBox.Text == "24:00"
                      ? "1.00:00"
                      : textBox.Text,
                   out tsTimeOff)
Nauseous answered 15/9, 2011 at 10:18 Comment(1)
Thanks, I have added both solutions to the question.Bard
C
5

this code may help you:

string span = "35:15";
TimeSpan ts = new TimeSpan(int.Parse(span.Split(':')[0]),    // hours
                           int.Parse(span.Split(':')[1]),    // minutes
                           0);                               // seconds

from: How to parse string with hours greater than 24 to TimeSpan?

Carr answered 14/5, 2013 at 6:57 Comment(0)
H
4

Generally the System.TimeSpan class is not well suited to represent a "point in time" but rather, as the name suggests, a "span" of time or duration. If you can refactor your code to use System.DateTime, or better yet System.DateTimeOffset, that would be the best solution. If that's not possible the other answer from Yahia is your best bet :)

Hypercriticism answered 15/9, 2011 at 10:24 Comment(4)
Thanks, I'm using the timespan as the start time is also a timespan, then im getting the number of hours between the two.Bard
TimeSpan is the normal .NET way to represent a time of a day - that is a point in time that either repeats every day or else is only fully defined when combined with a date. E.g. DateTime.TimeOfDay.Indue
@JonHanna yep that's slightly different semantically, in that case you're expressing the time of day as an offset from midnight - which is perfectly representable with a timespan (eg midnight plus 8 hours). To represent an actual point in time it would have to be combined with a DateTime as you say.Hypercriticism
It seems to be doing "span" thing poorly too. Say I want span of 24 hours, but if I specify it as TimeSpan.Parse("24:00:00"), I'll get 24 days. "23:59:59" though will give me 1 day without 1 second. As a coder I understand why it behaves like that, but I'll have a hard time explaining this to users.Mirisola
T
4

Honestly the best solution is to inform your client that 24:00 is not a valid hour and that he should use 0:00. I've never seen a clock jump from 23:59 to 24:00.

But if you insist ... check out the following SO question which contains the same question and contains a bunch of possible solutions:

Parsing times above 24 hours in C#

Theorem answered 15/9, 2011 at 10:25 Comment(5)
Thanks, I know what you mean. Just wanted to see if there was an easy work around. I have to at lease try and give the client what they want! :)Bard
tell the client what they want! ;) heheHypercriticism
Nothing wrong with telling a client his suggestion might not be optimal and to suggest a different approach. Depends on how you phrase it though ;)Theorem
24:00 is a valid time in a great many systems, including most national standards (but not most national conventions) and ISO 8601 dates. In ISO 8601 it is clearly synonymous as an alternative representation of 00:00 the next day, and normalised by converting it to that.Indue
I ran into this issue dealing with time intervals. Is "now" between "start" and "end". The proper way to express an interval is as a partially bounded pair, >= "start" and < "end". This works fine for "02:00-16:00". Exactly 4:00 PM is not included. It doesn't work for "20:00-24:00", without creating a special case to handle "24:00".Bogie

© 2022 - 2024 — McMap. All rights reserved.