0

I'm trying to build what's essentially a specialized timeclock. Last piece of the puzzle is, once I've extracted a list of timeclock "events" by employee to sum the differences between events. It's this last part that's not working... I always wind up with a result of 0. Here's what I've tried so far:

var worked = 
    from workDays in dc.Events
    where
        workDays.Time >= PayPeriodStartPicker.SelectedDate && 
        workDays.Time <= PayPeriodEndPicker.SelectedDate
    orderby workDays.Time
    select workDays;

TimeSpan totalTime = new TimeSpan();

for (int i = 0; i < worked.ToList().Count - 1; i++)
{
    totalTime.Add(worked.ToList()[i + 1].Time - worked.ToList()[i].Time);
}

I've never worked with timespans so probably something very stupid but...

6
  • 1
    What is "not working" exactly? Can you please be more specific? Do you get an unexpected result or something? Commented Mar 15, 2019 at 20:17
  • 1
    Hi. Could you please provide an MCVE? As shown, we don't know anything about the models used in your code or what data is being passed around. I can make some assumptions, but that isn't necessarily useful for others. Commented Mar 15, 2019 at 20:19
  • @SonerGönül … Sorry if I'm not being clear, the result (sum) of totalTime is always zero. Commented Mar 15, 2019 at 20:21
  • @MattJohnson … sorry, I'm not the most experience SO user :( I'm retrieving a list of DateTime objects, ordered by time and trying to sum the differences between each pair in the list (TimeIn, TImeOut, TimeIn, TimeOut basically). It looks like I'm retrieving them correctly but my math isn't working. The sum is always zero. Commented Mar 15, 2019 at 20:23
  • 2
    Everytime you call ToList(), you're creating a new copy of the list. If you want worked as a List, then you can call ToList on the original result: var worked = (<insertQueryHere>).ToList(); Commented Mar 15, 2019 at 20:44

1 Answer 1

3

The main problem I can see is that you are not saving the result of totalTime.Add. TimeSpan objects are immutable, so you need to do this instead:

totalTime = totalTime.Add(...);

However, you should probably also not be calling worked.ToList() so much. Call that once, before your for loop.

Also, you may want to consider the effects of daylight saving time on your code. Assuming the values are recorded in local time, subtracting two DateTime instances does not take DST into account. If one value is before the transition, and the other is after, you will not have the correct result. To account for this, record your values using DateTimeOffset types instead.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you nailed it! Looks like I need to make a few tweaks but the immutability was the issue. Thank you VERY VERY much!
Instead of DateTimeOffset, consider using all your times in UTC: DateTime.ToUniversalTime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.