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
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:
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);
}
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:
private IEnumerable<TimeSpan> GetTotalTime()
{
var totalTime = myContext.MyEntity.Select(m => m.Duration).AsEnumerable();
return totalTime;
}
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