Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -128,43 +128,116 @@ def parse_flexible_date(date_str):
|
|
128 |
"""유연한 날짜 파싱"""
|
129 |
import re
|
130 |
|
|
|
|
|
|
|
131 |
# 숫자만 추출
|
132 |
numbers = re.findall(r'\d+', date_str)
|
133 |
|
134 |
-
if len(numbers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
year = int(numbers[0])
|
136 |
month = int(numbers[1])
|
137 |
day = int(numbers[2])
|
138 |
|
139 |
# 연도가 2자리인 경우 처리
|
140 |
if year < 100:
|
141 |
-
if year > 30:
|
142 |
year += 1900
|
143 |
-
else:
|
144 |
year += 2000
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
147 |
else:
|
148 |
raise ValueError("날짜 정보가 부족합니다.")
|
|
|
|
|
149 |
|
150 |
def parse_flexible_time(time_str):
|
151 |
"""유연한 시간 파싱"""
|
152 |
import re
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
# 숫자만 추출
|
155 |
numbers = re.findall(r'\d+', time_str)
|
156 |
|
157 |
-
if len(numbers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
hour = int(numbers[0])
|
159 |
-
minute = int(numbers[1])
|
160 |
-
|
161 |
-
# 24시간 형식으로 변환
|
162 |
-
if hour > 24:
|
163 |
-
hour = hour % 24
|
164 |
-
|
165 |
-
return hour, minute
|
166 |
else:
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
def calculate_saju(birth_date, birth_time, gender, birth_place):
|
170 |
"""사주 계산 메인 함수"""
|
@@ -179,15 +252,18 @@ def calculate_saju(birth_date, birth_time, gender, birth_place):
|
|
179 |
# 유연한 날짜 파싱
|
180 |
try:
|
181 |
year, month, day = parse_flexible_date(birth_date)
|
|
|
182 |
|
183 |
# 날짜 유효성 검사
|
|
|
|
|
184 |
if month < 1 or month > 12:
|
185 |
return f"❌ 월은 1~12 사이여야 합니다. 입력된 월: {month}"
|
186 |
if day < 1 or day > 31:
|
187 |
return f"❌ 일은 1~31 사이여야 합니다. 입력된 일: {day}"
|
188 |
|
189 |
except Exception as e:
|
190 |
-
return f"❌ 날짜
|
191 |
|
192 |
# 유연한 시간 파싱
|
193 |
if birth_time:
|
|
|
128 |
"""유연한 날짜 파싱"""
|
129 |
import re
|
130 |
|
131 |
+
# 모든 공백 제거
|
132 |
+
date_str = date_str.replace(' ', '')
|
133 |
+
|
134 |
# 숫자만 추출
|
135 |
numbers = re.findall(r'\d+', date_str)
|
136 |
|
137 |
+
if len(numbers) == 0:
|
138 |
+
raise ValueError("숫자를 찾을 수 없습니다.")
|
139 |
+
|
140 |
+
# 하나의 긴 숫자 문자열인 경우 (예: 19900515, 900515)
|
141 |
+
if len(numbers) == 1:
|
142 |
+
num_str = numbers[0]
|
143 |
+
|
144 |
+
if len(num_str) == 8: # YYYYMMDD
|
145 |
+
year = int(num_str[:4])
|
146 |
+
month = int(num_str[4:6])
|
147 |
+
day = int(num_str[6:8])
|
148 |
+
elif len(num_str) == 6: # YYMMDD 또는 YYYYMM
|
149 |
+
if int(num_str[:2]) > 30: # 년도가 30보다 크면 19XX
|
150 |
+
year = 1900 + int(num_str[:2])
|
151 |
+
month = int(num_str[2:4])
|
152 |
+
day = int(num_str[4:6])
|
153 |
+
else: # YYYYMM 형태로 해석
|
154 |
+
year = int(num_str[:4])
|
155 |
+
month = int(num_str[4:6])
|
156 |
+
day = 1 # 기본값
|
157 |
+
elif len(num_str) == 5: # YMMDD
|
158 |
+
year = 1900 + int(num_str[0])
|
159 |
+
month = int(num_str[1:3])
|
160 |
+
day = int(num_str[3:5])
|
161 |
+
else:
|
162 |
+
raise ValueError(f"날짜 형식을 인식할 수 없습니다: {num_str}")
|
163 |
+
|
164 |
+
# 여러 숫자로 분리된 경우 (예: 1990-5-15)
|
165 |
+
elif len(numbers) >= 3:
|
166 |
year = int(numbers[0])
|
167 |
month = int(numbers[1])
|
168 |
day = int(numbers[2])
|
169 |
|
170 |
# 연도가 2자리인 경우 처리
|
171 |
if year < 100:
|
172 |
+
if year > 30:
|
173 |
year += 1900
|
174 |
+
else:
|
175 |
year += 2000
|
176 |
+
|
177 |
+
elif len(numbers) == 2:
|
178 |
+
# 년월만 있는 경우
|
179 |
+
year = int(numbers[0])
|
180 |
+
month = int(numbers[1])
|
181 |
+
day = 1
|
182 |
|
183 |
+
if year < 100:
|
184 |
+
if year > 30:
|
185 |
+
year += 1900
|
186 |
+
else:
|
187 |
+
year += 2000
|
188 |
+
|
189 |
else:
|
190 |
raise ValueError("날짜 정보가 부족합니다.")
|
191 |
+
|
192 |
+
return year, month, day
|
193 |
|
194 |
def parse_flexible_time(time_str):
|
195 |
"""유연한 시간 파싱"""
|
196 |
import re
|
197 |
|
198 |
+
if not time_str:
|
199 |
+
return 12, 0
|
200 |
+
|
201 |
+
# 모든 공백 제거
|
202 |
+
time_str = time_str.replace(' ', '')
|
203 |
+
|
204 |
# 숫자만 추출
|
205 |
numbers = re.findall(r'\d+', time_str)
|
206 |
|
207 |
+
if len(numbers) == 0:
|
208 |
+
return 12, 0
|
209 |
+
|
210 |
+
# 하나의 숫자인 경우
|
211 |
+
if len(numbers) == 1:
|
212 |
+
time_num = numbers[0]
|
213 |
+
|
214 |
+
if len(time_num) == 4: # HHMM
|
215 |
+
hour = int(time_num[:2])
|
216 |
+
minute = int(time_num[2:4])
|
217 |
+
elif len(time_num) == 3: # HMM
|
218 |
+
hour = int(time_num[0])
|
219 |
+
minute = int(time_num[1:3])
|
220 |
+
elif len(time_num) <= 2: # HH
|
221 |
+
hour = int(time_num)
|
222 |
+
minute = 0
|
223 |
+
else:
|
224 |
+
hour = 12
|
225 |
+
minute = 0
|
226 |
+
|
227 |
+
# 여러 숫자인 경우
|
228 |
+
elif len(numbers) >= 2:
|
229 |
hour = int(numbers[0])
|
230 |
+
minute = int(numbers[1])
|
231 |
+
|
|
|
|
|
|
|
|
|
|
|
232 |
else:
|
233 |
+
hour = int(numbers[0])
|
234 |
+
minute = 0
|
235 |
+
|
236 |
+
# 24시간 형식으로 조정
|
237 |
+
if hour > 24:
|
238 |
+
hour = hour % 24
|
239 |
+
|
240 |
+
return hour, minute
|
241 |
|
242 |
def calculate_saju(birth_date, birth_time, gender, birth_place):
|
243 |
"""사주 계산 메인 함수"""
|
|
|
252 |
# 유연한 날짜 파싱
|
253 |
try:
|
254 |
year, month, day = parse_flexible_date(birth_date)
|
255 |
+
print(f"파싱된 날짜: {year}년 {month}월 {day}일") # 디버깅용
|
256 |
|
257 |
# 날짜 유효성 검사
|
258 |
+
if year < 1900 or year > 2100:
|
259 |
+
return f"❌ 연도는 1900~2100 사이여야 합니다. 입력된 연도: {year}"
|
260 |
if month < 1 or month > 12:
|
261 |
return f"❌ 월은 1~12 사이여야 합니다. 입력된 월: {month}"
|
262 |
if day < 1 or day > 31:
|
263 |
return f"❌ 일은 1~31 사이여야 합니다. 입력된 일: {day}"
|
264 |
|
265 |
except Exception as e:
|
266 |
+
return f"❌ 날짜 파싱 오류: {str(e)}\n입력값: '{birth_date}'\n💡 다음과 같이 입력해보세요:\n- 19900515 (8자리 숫자)\n- 1990-5-15 (하이픈 구분)\n- 1990/5/15 (슬래시 구분)"
|
267 |
|
268 |
# 유연한 시간 파싱
|
269 |
if birth_time:
|