forked from curioe/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dateutil.py
53 lines (39 loc) · 1.64 KB
/
dateutil.py
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
# -*- coding: utf-8 -*-
import datetime
class Seoul_tzinfo(datetime.tzinfo):
"""Implementation of the Seoul timezone.
Adapted from http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html
"""
def utcoffset(self, dt):
return datetime.timedelta(hours=+9) + self.dst(dt)
def _FirstSunday(self, dt):
"""First Sunday on or after dt."""
return dt + datetime.timedelta(days=(6-dt.weekday()))
def dst(self, dt):
""" 한국은 섬머타임 없으므로 계산 생략 (dst=daylight saving time)
# 2 am on the second Sunday in March
dst_start = self._FirstSunday(datetime.datetime(dt.year, 3, 8, 2))
# 1 am on the first Sunday in November
dst_end = self._FirstSunday(datetime.datetime(dt.year, 11, 1, 1))
if dst_start <= dt.replace(tzinfo=None) < dst_end:
return datetime.timedelta(hours=1)
else:
return datetime.timedelta(hours=0)
"""
return datetime.timedelta(hours=0)
def tzname(self, dt):
"""
if self.dst(dt) == datetime.timedelta(hours=0):
return "KST"
else:
return "KDT"
"""
return "KST"
def date_for_new_snippet():
today = datetime.datetime.now(Seoul_tzinfo()).date()
# 다음목요일로 조정, 금,토, 일경우 이전 목요일로 조정됨
aligned = today - datetime.timedelta(days=(today.weekday()-2)) # 다음 목요일로 맞춤
return aligned
def date_for_retrieval():
today = datetime.datetime.now(Seoul_tzinfo()).date()
return today - datetime.timedelta(days=(today.weekday()-2))