programing

Epoch 시간을 날짜 시간으로 변환하는 중

itsource 2022. 10. 5. 23:45
반응형

Epoch 시간을 날짜 시간으로 변환하는 중

나머지로부터 답변을 받고 있는 것은 다음과 같은 Epoch 시간 형식입니다.

start_time = 1234566
end_time = 1234578

차이를 MySQL 데이터베이스에 저장할 수 있도록 MySQL 포맷 시간으로 에폭초를 변환하고 싶습니다.

나는 시도했다.

>>> import time
>>> time.gmtime(123456)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=10, tm_min=17, tm_sec=36, tm_wday=4, tm_yday=2, tm_isdst=0)

위의 결과는 제가 예상한 것과 다릅니다.나는 그것이 이길 바란다

2012-09-12 21:00:00

어떻게 하면 좋을까요?

그리고 내가 왜...TypeError: a float is required위해서

>>> getbbb_class.end_time = 1347516459425
>>> mend = time.gmtime(getbbb_class.end_time).tm_hour
Traceback (most recent call last):
  ...
TypeError: a float is required

시간 값(float 또는 int)을 형식 문자열로 변환하려면 다음 명령을 사용합니다.

import time

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))

예를 들어 다음과 같습니다.

import time

my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))

print(my_time)

를 사용할 수도 있습니다.datetime:

>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%c')
  '2012-09-13 02:22:50'
>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
'2012-09-13 14:22:50' # Local time

UTC를 가져오려면:

>>> datetime.datetime.utcfromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
  '2012-09-13 06:22:50'

이게 네가 필요한 거야

In [1]: time.time()
Out[1]: 1347517739.44904

In [2]: time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time()))
Out[2]: '2012-09-13 06:31:43'

a를 입력해 주세요.float대신int그리고 다른 것TypeError사라져야 해요

mend = time.gmtime(float(getbbb_class.end_time)).tm_hour

이것을 시험해 보세요.

>>> import time
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1347517119))
'2012-09-12 23:18:39'

MySQL에서는 다음을 선택할 수 있습니다.

INSERT INTO tblname VALUES (FROM_UNIXTIME(1347517119))

두 번째 질문은 아마getbbb_class.end_time는 문자열입니다.다음과 같이 숫자로 변환할 수 있습니다.float(getbbb_class.end_time)

에폭(밀리초 단위)이 있는 경우 가능한 솔루션은 초 단위로 변환됩니다.

import time
time.ctime(milliseconds/1000)

자세한 것은 이쪽time기능: https://docs.python.org/3/library/time.html#functions

#This adds 10 seconds from now.
from datetime import datetime
import commands

date_string_command="date +%s"
utc = commands.getoutput(date_string_command)
a_date=datetime.fromtimestamp(float(int(utc))).strftime('%Y-%m-%d %H:%M:%S')
print('a_date:'+a_date)
utc = int(utc)+10
b_date=datetime.fromtimestamp(float(utc)).strftime('%Y-%m-%d %H:%M:%S')
print('b_date:'+b_date)

This is a little more wordy but it comes from date command in unix.

First a bit of info in epoch from man gmtime

The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar  time.   When  inter-
       preted  as  an absolute time value, it represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal
       Time (UTC).

to understand how epoch should be.

>>> time.time()
1347517171.6514659
>>> time.gmtime(time.time())
(2012, 9, 13, 6, 19, 34, 3, 257, 0)

전달처의 arg를 확인해 주세요.time.gmtime()정수입니다.

UTC와 현지 시간 변환을 명확하게 구별하기 위한 답변 공유사용하다import datetime아래 방법을 사용하기 전에 맨 위에 표시하십시오.

Convert to datetime of local machine's timezone

datetime.datetime.fromtimestamp(1347517370)

Convert to datetime of UTC timezone

datetime.datetime.utcfromtimestamp(1347517370)

For both the above methods, if you wish to return a formatted date string, use the following code block

datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
datetime.datetime.utcfromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')

ReferenceURL : https://stackoverflow.com/questions/12400256/converting-epoch-time-into-the-datetime

반응형