Python Forum
Calendar calcualtions
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calendar calcualtions
#1
Hi!

I am trying to calculate difference in days between two dates. If the dates are close enough the answer is correct but as the difference increases I start getting an error.
I know there is a different way how to do it, but I would appreciate help in spotting whats wrong in this one.
print "***Calendar simulator 2.0***"
print
d1,m1,y1 = map(int,raw_input("Enter start date in DD.MM.YYYY format: ").split("."))
print
d2,m2,y2 = map(int,raw_input("Enter end date in DD.MM.YYYY format: ").split("."))
print

m111=m1-1
m222=m2-1
y11 = int((y1-1)/4.)+(y1-1)*365
if (y1)/4. == float(int((y1)/4.)):
    a = [31,29,31,30,31,30,31,31,30,31,30,31]
else:
    a = [31,28,31,30,31,30,31,31,30,31,30,31]    
m11 = sum(a[0:m111])

x = y11+m11+d1

y22 = int((y2-1)/4.)+(y2-1)*365
if (y2)/4. == float(int((y2)/4.)):
    b = [31,29,31,30,31,30,31,31,30,31,30,31]
else:
    b = [31,28,31,30,31,30,31,31,30,31,30,31]
m22 = sum(b[0:m222])

y = y22+m22+d2

diff = abs(y-x)

s = (diff % 10)
if s == 1:
    print "Difference is: ", diff, " day."
    print
    print "Ready."
else:
    print"Difference is: ", diff, " days."
    print
    print "Ready."
print x
print y
Reply
#2
Hello and welcome!
Turn each date in epoch time, subtract them and turn the result into days.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Apr-28-2017, 10:06 PM)wavic Wrote: Hello and welcome!
Turn each date in epoch time, subtract them and turn the result into days.

Thanks for suggestion, but that's not the aim of the task. I am trying to make it work using math and cannot spot the mistake :(
Reply
#4
It's hard to follow the code - the variable names are meaningless. I don't know what I am looking at all the time.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
You account for the possibility that a year in question may be a leap year - it's a little bit more complicated than you assume, but you don't account for the fact that there may be leap years in between.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
(Apr-28-2017, 10:11 PM)wavic Wrote: It's hard to follow the code - the variable names are meaningless. I don't know what I am looking at all the time.

I was rushing it and it is almost my first code. If you look into it its really no that confusing, but I guess ill look for help from someone else then. Thanks for the effort anyways
Reply
#7
From quick look it seems that you count every year divisible by four as a leap year; thats not right - for example years 1900 or 2100 are not leap years.

As you do exactly same calculations for both dates, it would make sense to rewrite it as a function.
Reply
#8
(Apr-28-2017, 10:39 PM)deusvult Wrote:
(Apr-28-2017, 10:11 PM)wavic Wrote: It's hard to follow the code - the variable names are meaningless. I don't know what I am looking at all the time.

I was rushing it and it is almost my first code. If you look into it its really no that confusing, but I guess ill look for help from someone else then. Thanks for the effort anyways

I could but I am sick. My brain is liquified.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(Apr-28-2017, 10:39 PM)deusvult Wrote:
(Apr-28-2017, 10:11 PM)wavic Wrote: It's hard to follow the code - the variable names are meaningless. I don't know what I am looking at all the time.

I was rushing it and it is almost my first code. If you look into it its really no that confusing, but I guess ill look for help from someone else then. Thanks for the effort anyways

If it were not confusing you would understand it and not be here for help. Your code is hard to read and even though you don't notice, it impacts *your* ability to understand it. Put descriptive names on variables and you'll have a very different view. Also add comment explaing what the intent of the code is.

PS: To test that a year is a multiple of 4: year % 4 == 0 is a bit less convoluted than (y1)/4. == float(int((y1)/4.)) (and as some have remarked even done properly this isn't sufficient to determine leap-ness).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#10
Here is an excellent resource for all sorts of date and time calculations: http://pleac.sourceforge.net/pleac_pytho...times.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Task calendar problem humanical 8 1,726 Sep-04-2023, 02:55 PM
Last Post: Pedroski55
  Calendar program louienyy 2 4,992 Mar-30-2020, 01:21 PM
Last Post: louienyy
Sad [Learning] Calendar without modules or list KoFu 5 4,853 Sep-09-2019, 03:25 PM
Last Post: DeaD_EyE
  Calendar calculations frequency 10 5,607 Nov-13-2018, 07:34 PM
Last Post: frequency

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020