0

Hi Team i have models as below. And i am trying to substract out time and in time .

class Attendance(models.Model):
   employee = models.ForeignKey(Employee, on_delete=models.CASCADE,
                                default=1, related_name='Attendance')
    attendance_date = models.DateField(null=True)
    in_time = models.TimeField(null=True)
    out_time = models.TimeField(null=True, blank=True)
    totaltime = models.TimeField(null=True, blank=True)

    def __str__(self):
        return str(self.employee) + '-' + str(self.attendance_date)

    @property
    def totaltime(self):
        FMT = '%H:%M:%S'
        currentDate = date.today().strftime('%Y-%m-%d')
        sub = datetime.strptime('out_time', FMT) -
            datetime.strptime('in_time', FMT)
        return sub

Still getting and error: ValueError: time data 'out_time' does not match format '%H:%M:%S'

Please advise what should i do ,I am expecting how i will substract two timefeild

2 Answers2

0
@property
def totaltime(self):
    total_time = datetime.timedelta(hours=self.out_time.hour, minutes=self.out_time.minute,
                                    seconds=self.out_time.second) - datetime.timedelta(hours=self.in_time.hour,
                                                                                      minutes=self.in_time.minute,
                                                                                      seconds=self.in_time.second)
    return total_time
Myth
  • 338
  • 7
0

So, first, you are using total_time as a @property, so you can just drop the field. Second, you need to use timedelta to calculate the difference between times, which is where another problem arises, because it seems only to operate with datetime objects, so you must work around that:

models.py:

from datetime import datetime, date, timedelta

class Attendance(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE,
                                default=1, related_name='Attendance')
    attendance_date = models.DateField(null=True)
    in_time = models.TimeField(null=True)
    out_time = models.TimeField(null=True, blank=True)

    def __str__(self):
        return str(self.employee) + '-' + str(self.attendance_date)

    @property
    def total_time(self):
        total = ( datetime.combine(date.today(), self.out_time) - 
                  timedelta(
                        hours=self.in_time.hour, 
                        minutes=self.in_time.minute, 
                        seconds=self.in_time.second
                    ) 
                )
        return total.time()

tests.py

class AttendanceTestCase(TestCase):
    def setUp(self):
        self.target_hour = 1
        self.target_minutes = 42
        self.target_seconds = 36

        now = datetime.now()
        in_time = time(now.hour, now.minute, now.second)
        dt = (datetime.combine(date.today(), in_time) + 
                timedelta(
                    hours=self.target_hour, 
                    minutes=self.target_minutes, 
                    seconds=self.target_seconds
                )
            )
        out_time = dt.time()
        
        self.employee = Employee.objects.create(
            username='employee',
            password='super_secret',
            email='employee@example.com'
        )

        self.attendance = Attendance.objects.create(
            employee=self.employee,
            attendance_date=date.today(),
            in_time=in_time,
            out_time=out_time
        )
        
    def test_attendance_total_time(self):
        total_time = self.attendance.total_time
        self.assertEqual(total_time.hour, self.target_hour)
        self.assertEqual(total_time.minute, self.target_minutes)
        self.assertEqual(total_time.second, self.target_seconds)
        print(total_time)
Niko
  • 3,012
  • 2
  • 8
  • 14