bonjour,
Je cherche à utiliser le module businesshours, dont voici la classe:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
 
"""
Class:  BusinessHours(datetime1,datetime2,worktiming=[9 ,18],weekends=[6,7],holidayfile=None)
 
Synopsis: Provides three date functions.
getdays()       - Returns the number of working days betweeen the two datetime object.
gethours()      - Returns the number of working hours between the two datetime objects.
dateinbetween() - Checks whether a given datetime lies between two datetime objects , used by the internal functions.
 
Class Parameters:
datetime1   - The datetime object with the starting date.
datetime2   - The datetime object with the ending date.
worktiming  - The working office hours . A list containing two values start time and end time
weekends    - The days in a week which have to be considered as weekends sent as a list, 1 for monday ...  7 for sunday.
holidayfile - A file consisting of the predetermined office holidays.Each date starts in a new line and currently must only be in the format dd-mm-yyyy
 
Example:
from BusinessHours import BusinessHours
from datetime import   datetime
 
testing = BusinessHours(datetime(2007,10,15),datetime.now())
print testing.getdays()
print testing.gethours()
 
 
"""
class BusinessHours:
 
    def __init__(self,datetime1,datetime2,worktiming=[9 ,18],weekends=[6,7],holidayfile=None):
        self.holidayfile = holidayfile
        self.weekends = weekends
        self.worktiming = worktiming
        self.datetime1 = datetime1
        self.datetime2 = datetime2
 
    def getdays(self):
        days = (self.datetime2-self.datetime1).days
        #exclude any day in the week marked as holiday (ex: saturday , sunday)
        noofweeks = days / 7
        extradays = days % 7
        startday = self.datetime1.isoweekday();
        days = days - (noofweeks * self.weekends.__len__())
        # this is for the extra days that dont make up 7 days , to remove holidays from them
        for weekend in self.weekends:
            if(startday==weekend):
                days = days - 1;
            else:
                if(weekend >= startday):
                    if(startday+extradays >= weekend):
                        days = days - 1 ;
                else:
                    if(7-startday+extraday>=weekend):
                        days = days - 1 ;
        # Read company holiday's from the file
        if(self.holidayfile is not None):
            f=open(self.holidayfile);
            fdata = f.read();
            definedholidays=fdata.split();
            f.close();
        #exclude any holidays that have been marked in the companies academic year
            for definedholiday in definedholidays:
                flag=0;
                day , month , year = definedholiday.split('-')
                holidate = date(int(year) , int(month) , int(day))
                for weekend in self.weekends:
                     #check if mentioned holiday lies in defined weekend , shouldnt deduct twice
                    if(holidate.isoweekday==weekend):
                        flag=1;
                        break;
                if(flag==1):
                    continue;
                if(self.dateinbetween(holidate)):
                    days = days - 1
        return days;
 
    def gethours(self):
        days = self.getdays()
        #calculate hours
        days = days - 2 # (-2)To remove the start day and the last day
        hoursinaday = self.worktiming[1]-self.worktiming[0]
        hours = days * hoursinaday
        # To calculate working hours in the first day.
        if(self.datetime1.hour < self.worktiming[0] ):
            hoursinfirstday = hoursinaday;
        elif(self.datetime1.hour > self.worktiming[1]):
            hoursinfirstday = 0;
        else:
            hoursinfirstday = worktiming[1]-self.datetime1.hour
        # To calculate working hours in the last day
        if(self.datetime2.hour > self.worktiming[1] ):
            hoursinlastday = hoursinaday;
        elif(self.datetime2.hour < self.worktiming[0]):
            hoursinlastday = 0;
        else:
            hoursinlastday = self.datetime2.hour-self.worktiming[0]
        hours = hours + hoursinfirstday + hoursinlastday
        return hours
 
    def dateinbetween(self,holidate):
        if(holidate.year > self.datetime1.year and holidate.year <= self.datetime2.year):
            return True
        if(holidate.year >= self.datetime1.year and holidate.year < self.datetime2.year):
            return True
        if(holidate.year == self.datetime1.year and holidate.year == self.datetime2.year):
            if(holidate.month > self.datetime1.month and holidate.month <= self.datetime2.month):
                return True
            if(holidate.month >= self.datetime1.month and holidate.month < self.datetime2.month):
                return True
            if(holidate.month == self.datetime1.month and holidate.month == self.datetime2.month):
                if(holidate.day >= self.datetime1.day and holidate.day <= self.datetime2.day):
                    return True
        return False;
Jai donc voulu tester:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from BusinessHours1 import BusinessHours
from datetime import   datetime
 
testing = BusinessHours(datetime(2013,07,1),datetime.now(), holidayfile = 'E:\exos python\Hollydays')
print testing.getdays()
print testing.gethours()
Mon ficher "Hollydays" comporte:
11-07-2013
01-07-2013
13-07-2013

mais à l'exécution, j'ai:

Traceback (most recent call last):
File "E:\exos python\BusinessHours-1.2.tar\module1.py", line 18, in <module>
print testing.getdays()
File "E:\exos python\BusinessHours-1.2.tar\BusinessHours-1.1\BusinessHours1.py", line 64, in getdays
holidate = date(int(year) , int(month) , int(day))
NameError: global name 'date' is not defined

Comment pui-je régler cette erreur, sachent que si j'essaye de définir "date" avec une valeur vide, j'obtiens:

Traceback (most recent call last):
File "E:\exos python\BusinessHours-1.2.tar\module1.py", line 106, in <module>
File "E:\exos python\BusinessHours-1.2.tar\BusinessHours-1.1\BusinessHours1.py", line 54, in getdays
TypeError: 'tuple' object is not callable


Merci d'avance,