summaryrefslogtreecommitdiffstats
path: root/libs/tiny_datetime/datetime/__init__.py
blob: 694d4d8c4af86a0d9dc0a58b7920045128b9d763 (plain)
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
def _calc_daycount(y, m, d):
	daysBeforeMonthTab = []
	b = 0
	for _d in (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31):
		daysBeforeMonthTab.append(b)
		b += _d
	daysBeforeYear = (y - 1) * 365 + (y - 1) // 4 - (y - 1) // 100 + (y - 1) // 400
	leap = y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
	daysBeforeMonth = daysBeforeMonthTab[m - 1] + (1 if (m > 2 and leap) else 0)
	return daysBeforeYear + daysBeforeMonth + d

def _calc_weekday(y, m, d):
	return (_calc_daycount(y, m, d) + 6) % 7

class date(object):
	def __init__(self, year, month, day):
		self.year = year
		self.month = month
		self.day = day

	def __add__(self, other):
		#TODO
		return timedelta()

	def __sub__(self, other):
		if isinstance(other, date):
			return timedelta(_calc_daycount(self.year, self.month, self.day) -
					 _calc_daycount(other.year, other.month, other.day))
		#TODO
		return timedelta()

	def weekday(self):
		return _calc_weekday(self.year, self.month, self.day)

class datetime(object):
	def __init__(self, year=0, month=0, day=0,
		     hour=0, minute=0, second=0, microsecond=0, tzinfo=None):
		self.year = year
		self.month = month
		self.day = day
		self.hour = hour
		self.minute = minute
		self.second = second
		self.microsecond = microsecond
		self.tzinfo = tzinfo

	def __add__(self, other):
		#TODO
		return timedelta()

	def __sub__(self, other):
		#TODO
		return timedelta()

	@classmethod
	def now(cls, tz=None):
		#TODO
		return cls.utcnow()

	@classmethod
	def utcnow(cls):
		#TODO
		return cls()

	@classmethod
	def strptime(cls, date_string, format):
		#TODO
		pass

	def weekday(self):
		return _calc_weekday(self.year, self.month, self.day)

class timedelta(object):
	def __init__(self, days=0, seconds=0, microseconds=0):
		self.days = days
		self.seconds = seconds
		self.microseconds = microseconds
bues.ch cgit interface