Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I'm working on a time tracking system which I want to use in a MVC 5 app.
Here is what I have so far:

My (simplified) class ClockInOut.cs
C#
public Guid Id { get; set; }
public DateTime ClockIn { get; set; }
public DateTime ClockOut { get; set; }
public TimeSpan Duration { get; set; }

Now in my view the user has two buttons. Clock in and clock out.

These are the controller actions:
C#
public ActionResult ClockIn()
{
    var model = new ZeitBuchungDto()
                {
                    Id = Guid.NewGuid(),
                    Kommen = DateTime.Now,
                    Gehen = new DateTime(9998,12,31,0,0,0),
                };
    
    var convertToEntity = Mapper.Map<ZeitBuchungDto, ZeitBuchung>(model);
    
    zeitContext.ZeitBuchung.Add(convertToEntity);
    zeitContext.SaveChanges();

    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}

C#
public ActionResult ClockOut()
{
    using (var db = new ZeitContext())
          {
              db.ZeitBuchung.Attach(convertToEntity);
              convertToEntity.Gehen = DateTime.Now;
              convertToEntity.AktuellGebucht = false;
              convertToEntity.Gesamtdauer = convertToEntity.Gehen - zeitKommen;
              db.Entry(convertToEntity).Property(x => x.Gesamtdauer).IsModified = true;
              db.Entry(convertToEntity).Property(x => x.Gehen).IsModified = true;
              db.SaveChanges();
           }
    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}

Now what's coming next is the point with which I'm not satisfied:
C#
private IEnumerable<TimeSpan> GetTotalTime()
{
    var totalTime = myContext.MyEntity.Select(m => m.Duration).AsEnumerable();

    return totalTime;
}

C#
public ActionResult Index()
{
    IEnumerable<TimeSpan> time = GetTotalTime();
    var totalTime = time.Sum(s => s.Ticks);
    var convertedTime = TimeSpan.FromTicks(totalTime).ToString(@"hh\:mm");

    ViewBag.Hours = convertedTime;

    return View(model);
}


What I have tried:

Actually all this stuff works but I have a feeling this conversion is pretty inaccurate.

Throughout my tests I noticed that sometimes the numbers I see don't add up. Might be just a rounding issue but I'm still pretty new to programming so I can't really judge on that.

What I would like to have is an accurate view. Take a look at this pic:
http://fs5.directupload.net/images/170905/zhvrlb4j.png
Where I put the circle, I assume this just got rounded up to 1 minute - which isn't too bad right now. But imagine you have a whole month of data and it just rounds up to a good amount of minutes - the person checking the times at the end of the month might get confused.

So my reason for asking - have mercy with my code, it's probably not really good lol - but anyway, how would I get an accurate illustration of the various times.

Hope you have some suggestions....

Thanks in advance :)

Paul
Posted
Updated 5-Sep-17 9:46am

1 solution

A TimeSpan in EF maps to a Time field in SQL Server. That time field can only hold values up to 24 hours where as a TimeSpan can go beyond that.

The rounding you're seeing is only in the UI, not in the TimeSpan property or Time field in the database.

The rounding you're seeing will NOT accumulate because it's not really there. The math to add up all of the TimeSpans will work on the non-rounded values, not the rounded displayed value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900