Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

01%20python/02.15%20%ED%8C%8C%EC%9D%B4%EC%8D%AC%EC%97%90%EC%84%9C%20%EB%82%A0%EC%A7%9C%EC%99%80%20%EC%8B%9C%EA%B0%84%20%EB%8B%A4%EB%A3%A8%EA%B8%B0 #74

Open
utterances-bot opened this issue Dec 6, 2022 · 5 comments

Comments

@utterances-bot
Copy link

2.15 파이썬에서 날짜와 시간 다루기 — 데이터 사이언스 스쿨

https://datascienceschool.net/01%20python/02.15%20%ED%8C%8C%EC%9D%B4%EC%8D%AC%EC%97%90%EC%84%9C%20%EB%82%A0%EC%A7%9C%EC%99%80%20%EC%8B%9C%EA%B0%84%20%EB%8B%A4%EB%A3%A8%EA%B8%B0.html

Copy link

class input_birthday(object):

def __init__(self, birthday):
    self.birthday = birthday
    
    

def how_old(self):
    
    today = dt.datetime.now()
    birthday_date = dt.datetime.strptime(str(self.birthday),"%Y%m%d") ## int -> str 변환 후 str -> 날짜로 변환
    age = math.ceil((today - birthday_date).days/365)
    
    if today.month > birthday_date.month:
        age = age
        
        if today.month == birthday_date.month:
            
            if today.day > birthday_date.day:
                age = age
            
            else:
                age -= 1
        
    else:
        age -= 1
        
    print(age)

Copy link

today = datetime.datetime.now()
birthday = datetime.datetime(2000, 7, 18)

next_birthday = birthday + relativedelta(year = today.year+1)
remain_day = next_birthday - today

print("다음 생일은 " + str(next_birthday.date()) + " 입니다")
print("생일까지 남은 일수 : " + str(remain_day.days) + "일")
print("생일까지 남은 분 : " + str(round(remain_day.total_seconds()/60,)) + "분")

Copy link

연습 문제 15.1

bday = dt.datetime.strptime('2004.6.4','%Y.%m.%d')
bday.strftime("%Y년 %m월 %d일 {0}요일").format('월.화.수.목.금.토.일'.split('.')[x.weekday()])

 


연습 문제 15.2

# param {datetime} bday
# returns {int} 실행 시각을 기준으로 한 만 나이
def calculateCurrentAge(bday):
    today = dt.datetime.now()
    y = today.year - bday.year
    if today.month < bday.month + (1 if today.day < bday.day else 0):
        return y - 1
    return y

 


연습 문제 15.3

# go away, clean code!
from datetime import datetime

# 다음 생일까지 남은 일수
def getRemainingDaysTillNextBDay(month, day):
    today = datetime.now()
    return (datetime.strptime(f'{today.year + (1 if today.month > month else 0)}.{month}.{day}','%Y.%m.%d') - today).days

# '내년' 생일까지 남은 일수
def getRemainingDaysTillNextBDayOnNextYear(month, day):
    today = datetime.now()
    return (datetime.strptime(f'{today.year + 1}.{month}.{day}','%Y.%m.%d') - today).days

# '내년' 생일까지 남은 분(minutes)
def getRemainingMinutesTillNextBDayOnNextYear(month, day):
    today = datetime.now()
    return round((datetime.strptime(f'{today.year + 1}.{month}.{day}','%Y.%m.%d') - today).total_seconds() / 60, 2)

 


연습 문제 15.4

# 한 번에 하기
# relativedelta의 months 값을 사용하지 않아서 이걸 썼다고 해도 되는 건지는 모르겠지만, 일단 사용했습니다
import datetime as dt
from dateutil.relativedelta import relativedelta

def isLeapYear(year):
    return (dt.datetime.strptime(f'{year}.2.1', '%Y.%m.%d') + relativedelta(days=28)).month == 2

Copy link

ZZoongss commented Aug 3, 2023

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
# 위 코드의 min, max 아이디어를 사용하였음
# 부모 클래스의 매소드 코드를 재사용 가능하다는 클래스 상속의 장점을 이용하였음
 
# 부모 클래스 Car 생성
class Car(object):  
    def __init__(self):
        self.speed = 0
        self.max_speed = 160
        self.accel = 20
        
    def speed_up(self):
        self.speed = min(self.max_speed, self.speed + self.accel)
        print('speed:',self.speed)
        
    def speed_down(self):
        self.speed = max(0self.speed - self.accel)
        print('speed:',self.speed)
 
#자식 클래스 SportCar 생성
class SportCar(Car):
    def __init__(self):
        super(SportCar,self).__init__()
        self.max_speed = 200
        self.accel = 45
        
# 자식 클래스 Truck 생성
class Truck(Car):
    def __init__(self):
        super(Truck,self).__init__()
        self.max_speed = 100
        self.accel = 15
cs

Copy link

woominKIM commented Sep 1, 2023

연습문제2.15.1



from datetime import datetime as dt
myBirth = dt(1998, 9, 1)
wkday_D = {
    0: "월요일",
    1: "화요일",
    2: "수요일",
    3: "목요일",
    4: "금요일",
    5: "토요일",
    6: "일요일",
}
print(myBirth.strftime("%Y년 %B월 %d일"), wkday_D[myBirth.weekday()])

연습문제2.15.2



from datetime import datetime as dt
myBirth=dt(1998,9,1)
curTime=dt.now()
age=curTime.year-myBirth.year if curTime.month>=myBirth.month&curTime.day>=myBirth.day else curTime.year-myBirth.year-1
print(age)

연습문제2.15.3



from datetime import datetime as dt
from datetime import timedelta as td
cur = dt.now()
nextBirth = dt(cur.year + 1, 6, 25)
diff = nextBirth - cur
print("내년 생일까지", diff.days, "일 남음")
print("내년 생일까지", diff.days * 1440 + int(diff.seconds / 60), "분 남음")

연습문제2.15.4



from datetime import datetime as dt
from dateutil.relativedelta import relativedelta as rd
t = dt(2000,2,1)
rt = rd(days=28)
print("윤년입니다") if (t+rt).day==29 else print("윤년이 아닙니다")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants