Advertisement
WadeRollins2710

Date Time Python

Nov 21st, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. import calendar
  2. import math
  3.  
  4. class Date(object):
  5.     def __init__(self, day, month, year, hour, minute, second, zone):
  6.         self.day = day
  7.         self.month = month
  8.         self.year = year
  9.         self.hour = hour
  10.         self.minute = minute
  11.         self.second = second
  12.         self.zone = zone
  13.  
  14.     def print(self):
  15.         print(self.hour, self.minute, self.second)
  16.  
  17.     def from_start(self):
  18.         monthDate = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  19.         if calendar.isleap(self.year):
  20.             monthDate[1] += 1
  21.         days = 0;
  22.         for i in range(1, self.year):
  23.             days += 365
  24.             if calendar.isleap(i):
  25.                 days += 1
  26.         for i in range(1, self.month):
  27.             days += monthDate[i - 1];
  28.         days += self.day - 1;
  29.         return days
  30.  
  31.     def __sub__(self, date2):
  32.         result = abs(self.from_start() - date2.from_start()) * 86400
  33.         print(result)
  34.         result -= date2.hour * 3600 + date2.minute * 60 + date2.second;
  35.         result += self.hour * 3600 + self.minute * 60 + self.second;
  36.         return result
  37.  
  38.     def get_days_of_month(self, month, year):
  39.         monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  40.         if calendar.isleap(year):
  41.             monthDays[1] += 1
  42.         return monthDays[month - 1]
  43.  
  44.     def get_tomorrow(self):
  45.         self.hour -= 24
  46.         days = self.get_days_of_month(self.month, self.year)
  47.         self.day += 1
  48.         if self.day > days:
  49.             self.day = 1
  50.             self.month += 1
  51.         if self.month > 12:
  52.             self.year += 1
  53.             self.month = 1
  54.  
  55.     def get_yesterday(self):
  56.         self.hour += 24
  57.         self.day -= 1
  58.         if self.day < 1:
  59.             self.month -= 1
  60.             if self.month < 1:
  61.                 self.month = 12
  62.                 self.year -= 1
  63.             self.day = self.get_days_of_month(self.month, self.year)
  64.  
  65.     def to_GMT(self):
  66.         if self.zone.startswith("-"):
  67.             self.hour += int(self.zone[1:3])
  68.             self.minute += int(self.zone[3:5])
  69.             if self.minute >= 60:
  70.                 self.hour += 1
  71.                 self.minute -= 60
  72.             if self.hour >= 24:
  73.                 self.get_tomorrow()
  74.         else:
  75.             self.hour -= int(self.zone[1:3])
  76.             self.minute -= int(self.zone[3:5])
  77.             if self.minute < 0:
  78.                 self.hour -= 1
  79.                 self.minute += 60
  80.             if self.hour < 0:
  81.                 self.get_yesterday()
  82.  
  83. def change(arr):
  84.     # Sun 10 May 2015 13:54:36 -0700
  85.     #  0   1  2   3       4       5
  86.     month = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  87.     arr[1] = int(arr[1])
  88.     arr[2] = month.index(arr[2])
  89.     arr[3] = int(arr[3])
  90.     zone = arr.pop()
  91.     time = map(int, arr.pop().split(":"))
  92.     for item in time:
  93.         arr.append(item)
  94.     arr.append(zone)
  95.     arr = arr[1:]
  96.     return arr
  97.  
  98. def is_later(date1, date2):
  99.     if (date1.year > date2.year):
  100.         return 2
  101.     elif (date1.year < date2.year):
  102.         return 0
  103.     else:
  104.         if (date1.month > date2.month):
  105.             return 2
  106.         elif (date1.month < date2.month):
  107.             return 0
  108.         else:
  109.             if (date1.day > date2.day):
  110.                 return 2
  111.             elif (date1.day < date2.day):
  112.                 return 0
  113.             else:
  114.                 if (date1.hour > date2.hour):
  115.                     return 2
  116.                 elif (date1.hour < date2.hour):
  117.                     return 0
  118.                 else:
  119.                     if (date1.minute > date2.minute):
  120.                         return 2
  121.                     elif (date1.minute < date2.minute):
  122.                         return 0
  123.                     if (date1.second > date2.second):
  124.                         return 2
  125.                     elif (date1.second < date2.second):
  126.                         return 0
  127.                     else:
  128.                         return 1
  129.  
  130. s1 = change(input().split())
  131. s2 = change(input().split())
  132.  
  133. date1 = Date(*s1)
  134. date2 = Date(*s2)
  135.  
  136. date1.to_GMT()
  137. date2.to_GMT()
  138.  
  139. if is_later(date2, date1):
  140.     sec = (date2 - date1)
  141. else:
  142.     sec = (date1 - date2)
  143.  
  144. print(sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement