]> git.phdru.name Git - m_lib.git/blobdiff - m_lib/opdate.py
Use integer division for Py3 compatibility
[m_lib.git] / m_lib / opdate.py
index 13a9d253354d0f7dec2d7877f427b03b97dd79e3..e6940a950f80817741213a98c40604ad7ec74b59 100755 (executable)
@@ -7,6 +7,7 @@
 #
 
 
+from __future__ import print_function
 from string import *
 from time import *
 from calendar import *
@@ -48,7 +49,7 @@ class opdate_error(Exception):
 #
 
 def IsLeapYear(Year):
-   if ( (Year % 4 == 0) and (Year % 4000 <> 0) and ((Year % 100 <> 0) or (Year % 400 == 0)) ):
+   if ( (Year % 4 == 0) and (Year % 4000 != 0) and ((Year % 100 != 0) or (Year % 400 == 0)) ):
       return True
    return False
 
@@ -74,7 +75,7 @@ def DaysInMonth(Month, Year):
       return 28+IsLeapYear(_setYear(Year))
 
    else:
-      raise opdate_error, "bad month `%s'" % str(Month)
+      raise opdate_error("bad month `%s'" % str(Month))
 
 
 def ValidDate(Day, Month, Year):
@@ -109,7 +110,7 @@ def DMYtoDate(Day, Month, Year):
          Year = Year - 1
       Year = Year - MinYear
 
-      return (((Year / 100)*146097) / 4) + (((Year % 100)*1461) / 4) + (((153*Month)+2) / 5)+Day+First2Months
+      return (((Year // 100)*146097) // 4) + (((Year % 100)*1461) // 4) + (((153*Month)+2) // 5)+Day+First2Months
 
 
 def DateToDMY(Julian):
@@ -127,11 +128,11 @@ def DateToDMY(Julian):
          Day = Julian-30
    else:
       I = (4*(Julian-First2Months))-1
-      J = (4*((I % 146097) / 4))+3
-      Year = (100*(I / 146097))+(J / 1461)
-      I = (5*(((J % 1461)+4) / 4))-3
-      Month = I / 153
-      Day = ((I % 153)+5) / 5
+      J = (4*((I % 146097) // 4))+3
+      Year = (100*(I // 146097))+(J // 1461)
+      I = (5*(((J % 1461)+4) // 4))-3
+      Month = I // 153
+      Day = ((I % 153)+5) // 5
       if Month < 10:
          Month = Month + 3
       else:
@@ -154,7 +155,7 @@ def IncDate(Julian, Days, Months, Years):
       Day = 28
 
    Year = Year + Years
-   Year = Year + Months / 12
+   Year = Year + Months // 12
    Month = Month + Months % 12
    if Month < 1:
       Month = Month + 12
@@ -164,7 +165,7 @@ def IncDate(Julian, Days, Months, Years):
       Year = Year + 1
 
    Julian = DMYtoDate(Day, Month, Year)
-   if Julian <> BadDate:
+   if Julian != BadDate:
       Julian = Julian + Days + Day28Delta
 
    return Julian
@@ -180,7 +181,7 @@ def IncDateTrunc(Julian, Months, Years):
       Day = 28
 
    Year = Year + Years
-   Year = Year + Months / 12
+   Year = Year + Months // 12
    Month = Month + Months % 12
    if Month < 1:
       Month = Month + 12
@@ -190,7 +191,7 @@ def IncDateTrunc(Julian, Months, Years):
       Year = Year + 1
 
    Julian = DMYtoDate(Day, Month, Year)
-   if Julian <> BadDate:
+   if Julian != BadDate:
       MaxDay = DaysInMonth(Month, Year)
       if Day+Day28Delta > MaxDay:
          Julian = Julian + MaxDay-Day
@@ -234,7 +235,7 @@ def DateDiff(Date1, Date2):
 def DayOfWeek(Julian):
    """ Return the day of the week for the date. Returns DayType(7) if Julian == BadDate. """
    if Julian == BadDate:
-      raise opdate_error, "bad date `%s'" % str(Julian)
+      raise opdate_error("bad date `%s'" % str(Julian))
    else:
       return (Julian+FirstDayOfWeek) % 7
 
@@ -272,9 +273,9 @@ def TimeToHMS(T):
       return 0, 0, 0
 
    else:
-      Hours = T / SecondsInHour
+      Hours = T // SecondsInHour
       T = T - Hours*SecondsInHour
-      Minutes = T / SecondsInMinute
+      Minutes = T // SecondsInMinute
       T = T - Minutes*SecondsInMinute
       Seconds = T
 
@@ -333,7 +334,7 @@ def RoundToNearestHour(T, Truncate = False):
    Seconds = 0
 
    if not Truncate:
-      if Minutes >= (MinutesInHour / 2):
+      if Minutes >= (MinutesInHour // 2):
          Hours = Hours + 1
 
    Minutes = 0
@@ -345,7 +346,7 @@ def RoundToNearestMinute(T, Truncate = False):
    Hours, Minutes, Seconds = TimeToHMS(T)
 
    if not Truncate:
-      if Seconds >= (SecondsInMinute / 2):
+      if Seconds >= (SecondsInMinute // 2):
          Minutes = Minutes + 1
 
    Seconds = 0
@@ -386,7 +387,7 @@ def IncDateTime(DT1, Days, Secs):
       Secs = -Secs
 
       # adjust the date
-      DT2[0] = DT2[0] - Secs / SecondsInDay
+      DT2[0] = DT2[0] - Secs // SecondsInDay
       Secs = Secs % SecondsInDay
 
       if Secs > DT2[1]:
@@ -402,7 +403,7 @@ def IncDateTime(DT1, Days, Secs):
       DT2[1] = DT2[1] + Secs
 
       # adjust date if necessary
-      DT2[0] = DT2[0] + DT2[1] / SecondsInDay
+      DT2[0] = DT2[0] + DT2[1] // SecondsInDay
 
       # force time to 0..SecondsInDay-1 range
       DT2[1] = DT2[1] % SecondsInDay
@@ -440,38 +441,39 @@ LongMonthNamesA = ['
 #
 
 def test():
-   print "Is 1984 leap year?", IsLeapYear(1984)
-   print "Is 1990 leap year?", IsLeapYear(1990)
+   print("Is 1984 leap year?", IsLeapYear(1984))
+   print("Is 1990 leap year?", IsLeapYear(1990))
 
-   print "Days in month 8 year 1996:", DaysInMonth(8, 1996)
+   print("Days in month 8 year 1996:", DaysInMonth(8, 1996))
 
-   print "Is date 8/12/1996 valid?", ValidDate(8, 12, 1996)
-   print "Is date 40/11/1996 valid?", ValidDate(40, 11, 1996)
-   print "Is date 8/14/1996 valid?", ValidDate(8, 14, 1996)
+   print("Is date 8/12/1996 valid?", ValidDate(8, 12, 1996))
+   print("Is date 40/11/1996 valid?", ValidDate(40, 11, 1996))
+   print("Is date 8/14/1996 valid?", ValidDate(8, 14, 1996))
 
-   print "Date->DMY for 138219:", DateToDMY(138219)
+   print("Date->DMY for 138219:", DateToDMY(138219))
 
    diff = DateDiff(DMYtoDate(12, 10, 1996), DMYtoDate(12, 10, 1997))
-   print "Date 12/10/1996 and date 12/10/1997 diff: %d years, %d months, %d days" % (diff[2], diff[1], diff[0])
+   print("Date 12/10/1996 and date 12/10/1997 diff: %d years, %d months, %d days" % (diff[2], diff[1], diff[0]))
 
    diff = DateDiff(DMYtoDate(12, 10, 1996), DMYtoDate(12, 11, 1997))
-   print "Date 12/10/1996 and date 12/11/1997 diff: %d years, %d months, %d days" % (diff[2], diff[1], diff[0])
+   print("Date 12/10/1996 and date 12/11/1997 diff: %d years, %d months, %d days" % (diff[2], diff[1], diff[0]))
 
    diff = DateDiff(DMYtoDate(31, 1, 1996), DMYtoDate(1, 3, 1996))
-   print "Date 31/01/1996 and date 01/03/1996 diff: %d years, %d months, %d days" % (diff[2], diff[1], diff[0])
+   print("Date 31/01/1996 and date 01/03/1996 diff: %d years, %d months, %d days" % (diff[2], diff[1], diff[0]))
 
 
-   #print "November is %dth month" % MonthStringToMonth("November")
+   #print("November is %dth month" % MonthStringToMonth("November"))
 
-   print "Today is", Today()
-   print "Now is", CurrentTime()
+   print("Today is", Today())
+   print("Now is", CurrentTime())
 
-   print "My birthday 21 Dec 1967 is (must be Thursday):", day_name[DayOfWeekDMY(21, 12, 67)]
+   print("My birthday 21 Dec 1967 is (must be Thursday):", day_name[DayOfWeekDMY(21, 12, 67)])
 
    gmt = DateTimeToGMT(DMYtoDate(21, 12, 1967), HMStoTime(23, 45, 0))
-   print "21 Dec 1967, 23:45:00 --", gmtime(gmt) # DOS version of gmtime has error processing dates before 1/1/1970 :(
+   # DOS version of gmtime has error processing dates before 1/1/1970 :(
+   print("21 Dec 1967, 23:45:00 --", gmtime(gmt))
    D, T = GMTtoDateTime(gmt)
-   print "(gmt) --", DateToDMY(D), TimeToHMS(T)
+   print("(gmt) --", DateToDMY(D), TimeToHMS(T))
 
 if __name__ == "__main__":
    test()