In the third case, I didn't understand why you didn't divide (his_to - his_from) / (my_to - my_from). Bearing that in mind, this made sense in my head, but analyze it carefully:
def match(mf,mt,hf,ht): mydiff = diff_in_seconds(mf,mt) if (mf >= hf and mt <= ht) or (mf < hf and mt > ht): mydiff = diff_in_seconds(mf,mt) hisdiff = diff_in_seconds(hf,ht) return min(1, hisdiff / mydiff) else: case1 = max(0, diff_in_seconds(hf, mt)) case2 = max(0, diff_in_seconds(mf, ht)) diff = min(case1, case2) return (diff / mydiff) def diff_in_seconds(d1,d2): timedelta = d2 - d1 return float(timedelta) print match(0,100,40,90) 2010/8/5 Alexandre González <agonzale...@gmail.com> > I've develope a "simple" function to calculate the difference between 2 > dates, and the time that they are interpolated. > > I need it to: I arrive to a site at a hour (my_from), I go out from the > site at a hour (my_to). Some friend arrive at the site at a hour (his_from) > and let it at a hour (his_to) > > If I stay in the site after he arrives and before he goes out, the > "probability" of match is 1, if I let the site after he arrives, the > probability of match is 0... You can see the samples in the documentation. > > I need to know if this is a good way to do, and if compare all the kinds of > matching (I've found 6). This is the code: > > def time_match(self, my_from, my_to, his_from, his_to): > """ > I've found 6 general kinds of time matching. It's better to > explain it with graphics: > > (my_from)--A--(my_to) > ------------------------- > (his_from)--B--(his_to) > """ > > # |-A-| |-A-| > # -------------------- or -------------------- > # |--B--| |--B--| > if my_to < his_from or his_to < my_from: > return 0 > > # |--A--| > # -------------------- > # |------B-------| > elif my_from >= his_from and my_to <= his_to: > return 1 > > # |-----A-------| > # -------------------- > # |-B-| > elif my_from < his_from and my_to > his_to: > my_diff_to_reuse = diff_in_seconds(my_from, my_to) > diff = my_diff_to_reuse - self.diff_in_seconds(his_from, > his_to) > > return (diff / my_diff_to_reuse) > > # |---A---| > # -------------------- > # |---B---| > elif my_from <= his_from and my_to <= his_to: > diff = self.diff_in_seconds(his_from, my_to) > > return (diff / self.diff_in_seconds(my_from, my_to)) > > # |---A---| > # -------------------- > # |---B---| > elif my_from >= his_from and my_to >= his_to: > diff = self.diff_in_seconds(my_from, his_to) > > return (diff / self.diff_in_seconds(my_from, my_to)) > > # If I'm here I have a problem > return 0 > > > def diff_in_seconds(date1, date2): > # Initial from: http://www.bytemycode.com/snippets/snippet/304/ > timedelta = date2 - date1 > diff = timedelta.days*24*3600 + timedelta.seconds > > return abs(float(diff)) > > Thanks! > -- > Please, don't send me files with extensions: .doc, .docx, .xls, .xlsx, .ppt > and/or .pptx > http://mirblu.com > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.