问题描述
嗨!
我正在研究一个我想在MVC 5应用程序中使用的时间跟踪系统。
这是我到目前为止所拥有的: br $>
我的(简化)类ClockInOut.cs
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);
}
我的尝试:
实际上所有这些东西都有效,但我觉得这种转换是非常不准确的。
在我的测试中,我注意到有时我看到的数字没有加起来。可能只是一个四舍五入的问题,但我仍然是编程的新手,所以我无法真正判断。
我想要的是准确的视图。看看这张照片:
我把圆圈放在哪里,我认为这只是将其调整为1分钟 - 现在还不错。但是想象一下你有一整个月的数据,而且只需要很长的时间 - 在月底检查时间的人可能会感到困惑。
所以我问的理由 - 怜悯我的代码,它可能不是很好lol - 但无论如何,我怎么能得到不同时间的准确说明。
希望你有一些建议....
提前致谢:)
Paul
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
推荐答案
这篇关于在这种情况下,计算时间的最准确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!