近期一个项目要写一个类,跟同学争执不下,特来请教各位大佬
import time
class SchoolDate(object):
def __init__(self, school_time):
self.weeks = 1
self.what_day = 1
self.school_time = time.strptime(school_time, "%Y-%m-%d")
def get_term(self):
timestamp = time.mktime(self.school_time)
#计算开学前周日的时间戳
first_week_timestamp=timestamp- 86400* int(time.strftime("%w",self.school_time))
now_timestamp = time.time()
self.what_day = time.strftime("%w",time.localtime(now_timestamp))
#周数 = (当前时间戳 - 开学周日的时间戳) / 7 天 +1
self.weeks = int((time.time() -first_week_timestamp)/ (86400*7)) + 1
if self.weeks < 1:
self.what_day = 1
self.weeks = 1
return self
def weeks(self):
return self.weeks
def day(self):
return self.what_day
版本 2
import time
from datetime import datetime
class SchoolDate(object):
__get = False
def __init__(self, starting_date):
"""
:param starting_date: 开学日期
"""
self.now_weeks = 1
self.what_day = 1
self.starting_date = time.strptime(starting_date, "%Y-%m-%d")
def _get_term(self):
self.__get = True
timestamp = time.mktime(self.starting_date)
# 计算开学前周日的时间戳
first_week_timestamp = timestamp - 86400 * int(time.strftime("%w", self.starting_date))
now_timestamp = time.time()
self.what_day = time.strftime("%w", time.localtime(now_timestamp))
# 周数 = (当前时间戳 - 开学周日的时间戳) / 7 天 +1
self.now_weeks = int((time.time() - first_week_timestamp) / (86400*7)) + 1
if self.weeks < 1:
self.what_day = 1
self.now_weeks = 1
@property
def weeks(self):
"""开学第几周"""
if not self.__get:
self._get_term()
return self.now_weeks
@property
def week_day(self):
"""周几"""
if not self.__get:
self._get_term()
return self.what_day
@property
def school_year(self):
"""获取当前学年"""
now = datetime.now()
if 2 <= now.month <= 7:
return "%d-%d" % ((now.year - 1), now.year)
else:
return "%d-%d" % (now.year, (now.year + 1))
初级写法,敬请吐槽!!
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.