Python의 시간 함수 | 1개 설정(time(), ctime(), sleep()...)

파이썬은 다음을 정의했습니다. 기준 치수 '시간'은 삶의 다양한 응용 분야에서 사용되는 변환 및 표현과 관련된 다양한 작업을 처리할 수 있게 해줍니다. 시간의 시작부터 측정이 시작되었습니다. 1970년 1월 1일 오전 12시 그리고 바로 이 시간을 ' 시대 '파이썬에서.

Python의 시간에 따른 작업 

Python time.time() 함수

파이썬 시간() 함수는 숫자를 세는 데 사용됩니다. 에포크 이후 경과된 시간(초)

Python3
   # Python code to demonstrate the working of   # time()   # importing 'time' module for time operations   import   time   # using time() to display time since epoch   print   (  'Seconds elapsed since the epoch are : '    end  =  ''  )   print   (  time  .  time  ())   

Python time.gmtime() 함수

파이썬 gmtime() 함수는 9개의 값을 갖는 구조 각각은 시간 속성을 순서대로 나타냅니다. 그것은 변환한다 초를 시간 속성(일 년 월 등)으로 에포크로부터 지정된 초까지. 초가 언급되지 않으면 현재까지의 시간이 계산됩니다. 구조 속성 테이블은 다음과 같습니다. 

Index Attributes Values 0 tm_year 2008 1 tm_mon 1 to 12 2 tm_mday 1 to 31 3 tm_hour 0 to 23 4 tm_min 0 to 59 5 tm_sec 0 to 61 (60 or 61 are leap-seconds) 6 tm_wday 0 to 6 7 tm_yday 1 to 366 8 tm_isdst -1 0 1 where -1 means Library determines DST 
Python3
   # Python code to demonstrate the working of gmtime()   import   time   # using gmtime() to return the time attribute structure   print   (  'Time calculated acc. to given seconds is : '  )   print   (  time  .  gmtime  ())   

산출: 

Time calculated acc. to given seconds is : time.struct_time(tm_year=2016 tm_mon=8 tm_mday=2 tm_hour=7 tm_min=12 tm_sec=31 tm_wday=1 tm_yday=215 tm_isdst=0) 

Python time.asctime() 및 time.ctime() 함수

파이썬 시간.asctime() 함수는 다음에 의해 생성된 시간 속성 문자열을 사용합니다. 지엠타임() 그리고는 시간을 나타내는 24자 문자열 . 파이썬 시간.ctime() 함수는 24자 시간 문자열 하지만 인수로 몇 초가 걸리고 언급된 초까지의 시간을 계산합니다. . 인수가 전달되지 않으면 현재까지의 시간이 계산됩니다.

Python3
   # Python code to demonstrate the working of   # asctime() and ctime()   # importing 'time' module for time operations   import   time   # initializing time using gmtime()   ti   =   time  .  gmtime  ()   # using asctime() to display time acc. to time mentioned   print   (  'Time calculated using asctime() is : '    end  =  ''  )   print   (  time  .  asctime  (  ti  ))   # using ctime() to display time string using seconds    print   (  'Time calculated using ctime() is : '     end  =  ''  )   print   (  time  .  ctime  ())   

산출: 

Time calculated using asctime() is : Tue Aug 2 07:47:02 2016 Time calculated using ctime() is : Tue Aug 2 07:47:02 2016 

Python time.sleep() 함수

이 방법은 다음과 같은 데 사용됩니다. 프로그램 실행을 중단하다 인수에 지정된 시간 동안.

Python3
   # Python code to demonstrate the working of   # sleep()   # importing 'time' module for time operations   import   time   # using ctime() to show present time   print   (  'Start Execution : '    end  =  ''  )   print   (  time  .  ctime  ())   # using sleep() to hault execution   time  .  sleep  (  4  )   # using ctime() to show present time   print   (  'Stop Execution : '    end  =  ''  )   print   (  time  .  ctime  ())   

산출: 

Start Execution : Tue Aug 2 07:59:03 2016 Stop Execution : Tue Aug 2 07:59:07 2016 

Python time.mktime() 함수

이 예에서는 구조체_시간 각 필드에 대한 값의 튜플을 가진 객체를 객체에 전달했습니다. 시간.mktime() 이를 Unix 시대 이후의 초 수를 나타내는 부동 소수점 숫자로 변환합니다.

Python3
   import   time   # Create a struct_time object representing a date and time   my_time   =   time  .  strptime  (  '2023-05-10 14:30:00'     '%Y-%m-  %d   %H:%M:%S'  )   # Convert the struct_time object to a floating-point number   seconds_since_epoch   =   time  .  mktime  (  my_time  )   print  (  'Seconds since epoch:'     seconds_since_epoch  )   

산출:

Seconds since epoch: 1683709200.0 

Python time.localtime() 함수

이 예에서는 다음을 호출합니다. 시간.현지시간() 현재 현지 시간을 struct_time으로 가져오는 인수가 없습니다.

Python3
   import   time   current_time   =   time  .  localtime  ()   print  (  current_time  )   

산출:

time.struct_time(tm_year=2023 tm_mon=5 tm_mday=10 tm_hour=12 tm_min=42 tm_sec=51 tm_wday=2 tm_yday=130 tm_isdst=0) 

Python time.strftime() 함수

출력 문자열의 원하는 형식을 지정하는 첫 번째 인수로 형식 문자열을 사용합니다.

Python3
   import   time   now   =   time  .  localtime  ()   formatted_time   =   time  .  strftime  (  '%Y-%m-  %d   %H:%M:%S'     now  )   print  (  formatted_time  )   

산출:

2023-05-10 13:42:04