C
C#11mo ago
Whiteboy

✅ EF with MySQL getting double/triple time values ?!

So i assign task to user and set the assignTime to DateTime.Now so i should be able to get remaining time right?
var userTaskAssignment = dbContext.UserTaskAssignments.FirstOrDefault(assignment => assignment.TaskId == taskId && assignment.UserId == UserId);
if (userTaskAssignment != null)
{
RemainingTime = userTaskAssignment.TimeToCompleteTask - DateTime.Now;
RemainingHours = RemainingTime.TotalHours;
RemainingMinutes = RemainingTime.TotalMinutes;
RemainingSeconds = RemainingTime.TotalSeconds;
}
var userTaskAssignment = dbContext.UserTaskAssignments.FirstOrDefault(assignment => assignment.TaskId == taskId && assignment.UserId == UserId);
if (userTaskAssignment != null)
{
RemainingTime = userTaskAssignment.TimeToCompleteTask - DateTime.Now;
RemainingHours = RemainingTime.TotalHours;
RemainingMinutes = RemainingTime.TotalMinutes;
RemainingSeconds = RemainingTime.TotalSeconds;
}
private async Task<Task?> AssignTaskToUserIds(List<int> userIds, int taskId, TimeSpan taskLifespan)
{
DateTime assignmentTime = DateTime.Now;

foreach (int userId in userIds)
{
UserTaskAssignment userTaskAssignment = new UserTaskAssignment
{
UserId = userId,
TaskId = taskId,
AssignmentTime = assignmentTime,
};

dbContext.UserTaskAssignments.Add(userTaskAssignment);
}

await dbContext.SaveChangesAsync();

var assignedTasks = dbContext.UserTaskAssignments
.Where(uta => userIds.Contains(uta.UserId) && uta.TaskId == taskId)
.ToList();

foreach (var assignment in assignedTasks)
{
assignment.TimeToCompleteTask = assignment.AssignmentTime.Add(taskLifespan);
}

await dbContext.SaveChangesAsync();

PageHelper.SetTempDataSuccessMessage("Task assigned successfully", TempData);
return null;
}
private async Task<Task?> AssignTaskToUserIds(List<int> userIds, int taskId, TimeSpan taskLifespan)
{
DateTime assignmentTime = DateTime.Now;

foreach (int userId in userIds)
{
UserTaskAssignment userTaskAssignment = new UserTaskAssignment
{
UserId = userId,
TaskId = taskId,
AssignmentTime = assignmentTime,
};

dbContext.UserTaskAssignments.Add(userTaskAssignment);
}

await dbContext.SaveChangesAsync();

var assignedTasks = dbContext.UserTaskAssignments
.Where(uta => userIds.Contains(uta.UserId) && uta.TaskId == taskId)
.ToList();

foreach (var assignment in assignedTasks)
{
assignment.TimeToCompleteTask = assignment.AssignmentTime.Add(taskLifespan);
}

await dbContext.SaveChangesAsync();

PageHelper.SetTempDataSuccessMessage("Task assigned successfully", TempData);
return null;
}
For some reason when i set TimeToCompleteTask = 1h and i go to user i get 3h to complete task orr 1h 59min?
27 Replies
Jimmacle
Jimmacle11mo ago
first point - don't use DateTime, use DateTimeOffset
Angius
Angius11mo ago
Task<Task?>
scared
Jimmacle
Jimmacle11mo ago
DateTime does not know anything about time zone offsets and could easily lead to errors calculating durations if you aren't solely using UTC times
Whiteboy
WhiteboyOP11mo ago
well yea i created "Task" as a entity then realized it was bad naming tho everywhere?
Jimmacle
Jimmacle11mo ago
everywhere possible, yes
Whiteboy
WhiteboyOP11mo ago
i set task to 1h xd
Whiteboy
WhiteboyOP11mo ago
No description
Jimmacle
Jimmacle11mo ago
besides that, have you checked what times are actually being used in calculations? like what's getting stored in the db, what are the actual time values when you set a breakpoint after the calculations
Whiteboy
WhiteboyOP11mo ago
nope
Jimmacle
Jimmacle11mo ago
that wasn't a yes or no question well not the one you replied to do that and see if you can find the problem
Whiteboy
WhiteboyOP11mo ago
does this look good or cursed tho
int days = ParseAndValidateInput(Request.Form["Days"]);
int hours = ParseAndValidateInput(Request.Form["Hours"]);
int minutes = ParseAndValidateInput(Request.Form["Minutes"]);

TimeSpan timeToCompleteTask = new TimeSpan(days, hours, minutes, 0);
int days = ParseAndValidateInput(Request.Form["Days"]);
int hours = ParseAndValidateInput(Request.Form["Hours"]);
int minutes = ParseAndValidateInput(Request.Form["Minutes"]);

TimeSpan timeToCompleteTask = new TimeSpan(days, hours, minutes, 0);
Jimmacle
Jimmacle11mo ago
could be worse, but i think your naming is confusing that looks like a different value than what TimeToCompleteTask in your entity should be why is it a timespan?
Whiteboy
WhiteboyOP11mo ago
idk that's what i tought it should be
Jimmacle
Jimmacle11mo ago
timespans represent durations
Whiteboy
WhiteboyOP11mo ago
im 1st time ever dealing with time
Jimmacle
Jimmacle11mo ago
you want datetime/datetimeoffset for a point in time preferably datetimeoffset
Whiteboy
WhiteboyOP11mo ago
but that's not point in time no? im confused rn it's like i set timeToCompleteTask = 2h so the task is visible fro 2h
Jimmacle
Jimmacle11mo ago
how does that make sense in the context of userTaskAssignment.TimeToCompleteTask - DateTime.Now;? you should be comparing DateTime.Now with another point in time, not a duration i think your naming is just confusing, it should be consistent throughout your code
Whiteboy
WhiteboyOP11mo ago
foreach (var assignment in assignedTasks)
{
assignment.DeadLineDateTime = assignment.AssignmentTime.Add(timeToCompleteTask);
}
foreach (var assignment in assignedTasks)
{
assignment.DeadLineDateTime = assignment.AssignmentTime.Add(timeToCompleteTask);
}
yeah sure i should rename it but then i add to it the timeToCompleteTask so it's point in time when i compare it
Jimmacle
Jimmacle11mo ago
okay, so use your debugger and find out what values you're actually comparing
Whiteboy
WhiteboyOP11mo ago
No description
Whiteboy
WhiteboyOP11mo ago
so i set timeToCompleteTask to 1h and on site im getting 1h 59mins
Jimmacle
Jimmacle11mo ago
what time zone are you in?
Whiteboy
WhiteboyOP11mo ago
i found the issue
Whiteboy
WhiteboyOP11mo ago
No description
Whiteboy
WhiteboyOP11mo ago
There should be JUST "Hours/Minutes/Seconds" Not total because with total the minutes were setting to 60 at 1h timeToCompleteTask and the hours timer is also 1h so 2h at ttoal xd
Jimmacle
Jimmacle11mo ago
that would also do it
Want results from more Discord servers?
Add your server