-2

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'),
ilham
  • 47
  • 2
  • 8

1 Answers1

0

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.

Community
  • 1
  • 1
user1097772
  • 3,499
  • 15
  • 59
  • 95
  • thank you so much for your replay, yes i'm using python the function that i used work corretly but it counts all the days for exemple if i choose to calcul the number of days between today (20/01/2017) and monday (23/01/2017) the function return as a result 3 days and that's wrong i need from the function to remove the weekend but i don't know how to add that to my function – ilham Jan 20 '17 at 16:20
  • @ilham Well I already give you hint how to start, but you can always use some library look on this question http://stackoverflow.com/questions/3615375/python-count-days-ignoring-weekends here are two libraries: https://pypi.python.org/pypi/workdays/ https://bitbucket.org/shelldweller/python-bizdatetime – user1097772 Feb 01 '17 at 15:37