简体   繁体   中英

function to calculate days of work

hello everyone i want to calculate the days of work between two dates without calculing sunday and saturday but i don't know how to do that ! this is the code that i used but it doesn't work

file.py
import datetime
import math
from datetime import date

from openerp.osv import osv, fields, orm
class obj_ghb(osv.osv):
    _name = 'obj.ghb'
    _description = 'objet ghb'

    def get_total_days( self, cr, uid, ids,days_tota,arg, context = {}):
        diff_day={}


        for record in self.browse(cr, uid, ids, context=context):


            s_date = datetime.datetime.strptime(record.datedebut, "%Y-%m-%d").date()

           e_date =datetime.datetime.strptime(record.datefin, "%Y-%m-%d").date()       

            diff_day[record.id] =(e_date-s_date).days

        return diff_day


    _columns = {
        'nomprojet': fields.char('Nom du projet'),
        'responsable': fields.char('Responsable GHB'),
        'client': fields.char('Client'),
        'contactclient': fields.char('Contact du client'),
        'datedebut': fields.date('Date de debut'),
        'datefin': fields.date('Date de fin'),
        'nombredejour': fields.function(get_total_days, type = "integer", method=True, store = True),
        'obj_ghb_parent': fields.one2many('loyer', 'loyer_obj_ghb'),
        'obj_ghb_id': fields.one2many('assurance', 'assurance_obj_ghb'),
        'obj_ghb_parenttt': fields.one2many('salaire', 'salaire_obj_ghb'),
        'obj_ghb_parentttt': fields.one2many('autres', 'autres_obj_ghb'),

I suppose your code is in python. I don't work with python, so I don't know if you have some code errors in your code, but at first sight what you do is wrong.

  s_date = datetime.datetime.strptime(record.datedebut, "%Y-%m-%d").date()
  e_date =datetime.datetime.strptime(record.datefin, "%Y-%m-%d").date()       
  diff_day[record.id] =(e_date-s_date).days

You wrote you don't want to count the weekend days also your diff of days is probably wrong.
Lets say you want to count days from monday to friday in January 2017. Monday was 2.1. and Friday was 6.1. If you do your diff_day calculation e_date - s_date = 6. - 2. = 4 which is obviously wrong, because its 5 days.
So your number of days count will be like e_date - s_date + 1 .

Next you need to subtract number of saturdays and sundays in your time range + you will probably also want to exclude some public holidays etc or other non working days.

Look at answer for this question: Calculate the number of business days between two dates?

It should lead you to right way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM