Hướng dẫn compare unix timestamps python - so sánh dấu thời gian unix python

Ứng dụng của tôi cần so sánh sự khác biệt giữa hai dấu thời gian Unix và trả về đúng hoặc sai. Tóm lại, nếu người dùng đã đánh giá trong vòng một ngày hoặc tuần sau khi xếp hạng cố gắng, chức năng sẽ trả về sai. Tôi sợ rằng điều này có thể không phải là rất pythonic và tôi có thể đã phát minh lại bánh xe. Có thư viện đóng gói sẵn trong Python 2.5 sẽ giúp tôi đạt được kết quả tương tự theo cách tốt hơn không?

import time

def is_allowed(time_rated, duration):
    durations={'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = int(time.time()) - time_rated
        if  time_difference > durations[duration]:
            return True
        else:
            return False
    return False

Đã hỏi ngày 14 tháng 3 năm 2014 lúc 11:25Mar 14, 2014 at 11:25

Hướng dẫn compare unix timestamps python - so sánh dấu thời gian unix python

user1645914user1645914user1645914

3714 Huy hiệu bạc21 Huy hiệu đồng4 silver badges21 bronze badges

2

Không, bạn đang làm tốt ở đây, nhưng bạn có thể trả về kết quả của so sánh

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
04, nó đã mang lại cho bạn một giá trị boolean. Cũng không cần phải chuyển phao
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
05 thành một số nguyên ở đây:

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False

Bạn có thể đơn giản hóa điều này hơn nữa bằng cách mặc định vào

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
06 của
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
07, tiết kiệm cho bạn một bài kiểm tra
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
08:

def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration

Điều này trả về

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
09 nếu:

  • def is_allowed(time_rated, duration):
        durations = {'day': 86400, 'week': 604800}
        if duration in durations and isinstance(time_rated, int):
            time_difference = time.time() - time_rated
            return time_rated > durations[duration]
        return False
    
    10 không phải là một số nguyên
  • def is_allowed(time_rated, duration):
        durations = {'day': 86400, 'week': 604800}
        if duration in durations and isinstance(time_rated, int):
            time_difference = time.time() - time_rated
            return time_rated > durations[duration]
        return False
    
    06 không bằng
    def is_allowed(time_rated, duration):
        durations = {'day': 86400, 'week': 604800}
        if duration in durations and isinstance(time_rated, int):
            time_difference = time.time() - time_rated
            return time_rated > durations[duration]
        return False
    
    12 hoặc
    def is_allowed(time_rated, duration):
        durations = {'day': 86400, 'week': 604800}
        if duration in durations and isinstance(time_rated, int):
            time_difference = time.time() - time_rated
            return time_rated > durations[duration]
        return False
    
    13 (vì không có số nào lớn hơn
    def is_allowed(time_rated, duration):
        durations = {'day': 86400, 'week': 604800}
        if duration in durations and isinstance(time_rated, int):
            time_difference = time.time() - time_rated
            return time_rated > durations[duration]
        return False
    
    07).
  • def is_allowed(time_rated, duration):
        durations = {'day': 86400, 'week': 604800}
        if duration in durations and isinstance(time_rated, int):
            time_difference = time.time() - time_rated
            return time_rated > durations[duration]
        return False
    
    10 nhỏ hơn 86400 hoặc 604800 giây trước, tùy thuộc vào
    def is_allowed(time_rated, duration):
        durations = {'day': 86400, 'week': 604800}
        if duration in durations and isinstance(time_rated, int):
            time_difference = time.time() - time_rated
            return time_rated > durations[duration]
        return False
    
    06.

Đã trả lời ngày 14 tháng 3 năm 2014 lúc 11:27Mar 14, 2014 at 11:27

Martijn Pieters ♦ Martijn PietersMartijn Pieters

998K280 Huy hiệu vàng3925 Huy hiệu bạc3264 Huy hiệu đồng280 gold badges3925 silver badges3264 bronze badges

5

Bạn có thể sử dụng phương thức

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
17 để chuyển đổi gấu trúc
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
18 thành đối tượng
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
19 và sau đó những thứ khác rất đơn giản:

import pandas as pd
import numpy as np
from datetime import time

#Create fake dataset
df = pd.DataFrame()
df["CRASH TIME"] = pd.to_datetime(['2017-06-26 22:00:00', '2017-06-27 21:50:00'])
df["SUNRISE"] = [time(22, 26, 50, 135154), time(21, 26, 49, 135154)]
print(df.head())

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))

print("SUNRISE element type:", type(df["SUNRISE"][0]))

df["CRASH TIME"] = df["CRASH TIME"].dt.time

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))

Output->

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 

Pandas Timestamp tương đương với DateTime trong Python. Dấu thời gian được sử dụng cho các cấu trúc dữ liệu định hướng chuỗi thời gian trong gấu trúc. Đôi khi ngày và thời gian được cung cấp dưới dạng dấu thời gian trong gấu trúc hoặc có lợi để được chuyển đổi theo dấu thời gian. Và, cần phải so sánh dấu thời gian để biết mục nhập mới nhất, các mục giữa hai dấu thời gian, mục cũ nhất, v.v. cho các nhiệm vụ khác nhau. So sánh giữa các đối tượng dấu thời gian của gấu trúc được thực hiện bằng cách sử dụng các toán tử so sánh đơn giản:>,

Thời gian cho có thể được chuyển đổi thành Pandas Timestamp bằng phương thức pandas.timestamp (). Phương pháp này có thể lấy đầu vào ở các dạng khác nhau, chẳng hạn như chuỗi giống như DateTime (ví dụ: '2017-01-01T12'), Epoch Unix tính theo đơn vị vài giây (1513393355.5), v.v. giờ, phút, thứ hai, vv được phân tách bằng dấu phẩy hoặc sử dụng tên biến. Ví dụ: nếu chúng ta muốn viết 2018/2/21 11:40:00, chúng ta có thể cung cấp (2018,2,21,11,40) làm tham số cho phương thức Timestamp hoặc có thể viết (năm = 2018, tháng = 2, ngày = 21, giờ = 11, phút = 40). Các giá trị không được cung cấp sẽ được coi là không. Cách tiếp cận này được sử dụng trong mã sau để tạo cột Timestamp ‘New_time, bằng cách sử dụng thông tin ngày và thời gian được cung cấp.

Approach:

  • Tạo một khung dữ liệu với các giá trị ngày và thời gian
  • Chuyển đổi giá trị ngày và thời gian thành các giá trị dấu thời gian bằng phương thức pandas.timestamp ()
  • So sánh dấu thời gian cần thiết bằng cách sử dụng các toán tử so sánh thường xuyên.

Tạo một bản dữ liệu gấu trúc với ngày và giờ:

Python3

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
20

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
0
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
1
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
2
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
3
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
5
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
6
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
7
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
7
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
7
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
83
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
83
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
86
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
88
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
6
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
50
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
50
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
54
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
56
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
58
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
86

Các

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
import pandas as pd
import numpy as np
from datetime import time

#Create fake dataset
df = pd.DataFrame()
df["CRASH TIME"] = pd.to_datetime(['2017-06-26 22:00:00', '2017-06-27 21:50:00'])
df["SUNRISE"] = [time(22, 26, 50, 135154), time(21, 26, 49, 135154)]
print(df.head())

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))

print("SUNRISE element type:", type(df["SUNRISE"][0]))

df["CRASH TIME"] = df["CRASH TIME"].dt.time

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))
64
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
6
import pandas as pd
import numpy as np
from datetime import time

#Create fake dataset
df = pd.DataFrame()
df["CRASH TIME"] = pd.to_datetime(['2017-06-26 22:00:00', '2017-06-27 21:50:00'])
df["SUNRISE"] = [time(22, 26, 50, 135154), time(21, 26, 49, 135154)]
print(df.head())

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))

print("SUNRISE element type:", type(df["SUNRISE"][0]))

df["CRASH TIME"] = df["CRASH TIME"].dt.time

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))
66
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
import pandas as pd
import numpy as np
from datetime import time

#Create fake dataset
df = pd.DataFrame()
df["CRASH TIME"] = pd.to_datetime(['2017-06-26 22:00:00', '2017-06-27 21:50:00'])
df["SUNRISE"] = [time(22, 26, 50, 135154), time(21, 26, 49, 135154)]
print(df.head())

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))

print("SUNRISE element type:", type(df["SUNRISE"][0]))

df["CRASH TIME"] = df["CRASH TIME"].dt.time

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))
66
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
import pandas as pd
import numpy as np
from datetime import time

#Create fake dataset
df = pd.DataFrame()
df["CRASH TIME"] = pd.to_datetime(['2017-06-26 22:00:00', '2017-06-27 21:50:00'])
df["SUNRISE"] = [time(22, 26, 50, 135154), time(21, 26, 49, 135154)]
print(df.head())

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))

print("SUNRISE element type:", type(df["SUNRISE"][0]))

df["CRASH TIME"] = df["CRASH TIME"].dt.time

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))
66
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
50
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
50
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
86
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
177
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
6
import pandas as pd
import numpy as np
from datetime import time

#Create fake dataset
df = pd.DataFrame()
df["CRASH TIME"] = pd.to_datetime(['2017-06-26 22:00:00', '2017-06-27 21:50:00'])
df["SUNRISE"] = [time(22, 26, 50, 135154), time(21, 26, 49, 135154)]
print(df.head())

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))

print("SUNRISE element type:", type(df["SUNRISE"][0]))

df["CRASH TIME"] = df["CRASH TIME"].dt.time

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))
66
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
181
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
183
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
45
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
187
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
188

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
189
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
190

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
192
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
193

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
194
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
195

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
196
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
197
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
198

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
2
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
200
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
201
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
202__12
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
204
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
205

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
206

Output:

DF trên được sử dụng trong các ví dụ sau.

Ví dụ 1: Ở đây, dấu thời gian thứ nhất và thứ hai trong ‘new_time, được so sánh để biết những người lâu đời nhất trong số đó. Here, the first and second timestamp in ‘new_time’ are compared to know the oldest among those. Here, the first and second timestamp in ‘new_time’ are compared to know the oldest among those.

Python3

Is

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
10
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
11
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
12
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
205
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
14
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
15
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
10
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
11
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
19
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
205

Output:

def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
8

Ví dụ 2: Ở đây, tất cả các dấu thời gian trong ‘new_time, được so sánh với dấu thời gian (2018-01-05 12:00:00) và các mục trước khi dấu thời gian này được trả về Here, all timestamps in ‘new_time’ are compared with Timestamp(2018-01-05 12:00:00) and the entries before this timestamp are returned Here, all timestamps in ‘new_time’ are compared with Timestamp(2018-01-05 12:00:00) and the entries before this timestamp are returned

Python3

Is

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
207
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
196
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
197
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
34
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
83
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
56
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8__
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
43
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
10
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
27
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
197
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
47

Output:

def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
5

Ví dụ 3: Ở đây một lần nữa chúng tôi so sánh tất cả các dấu thời gian với dấu thời gian (2018-01-05 12:00:00), nhưng đã trả về kết quả so sánh dưới dạng giá trị boolean (true/false) cho tất cả các dấu thời gian. Here again we compared all timestamps with Timestamp(2018-01-05 12:00:00), but returned comparison result as boolean values (True/False) for all timestamps. Here again we compared all timestamps with Timestamp(2018-01-05 12:00:00), but returned comparison result as boolean values (True/False) for all timestamps.

Python3

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
10
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
27
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
197
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
51
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
83
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
56
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
39
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
54
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
59

Output:

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4

Ví dụ 4: Ở đây, hàm tối đa được sử dụng để có được tối đa của tất cả các dấu thời gian, đó là mục gần đây trong cột ‘New_time. Here, the max function is used to get the maximum of all timestamps, that is the recent entry in the ‘new_time’ column. Here, the max function is used to get the maximum of all timestamps, that is the recent entry in the ‘new_time’ column.

Ngoài ra, với điều đó, chúng tôi đã tính toán chênh lệch thời gian giữa dấu thời gian thứ nhất và lần thứ hai trong cột ‘New_time.

Python3

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
10
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
11
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
62
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
64
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
27
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
197
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
67

Is

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
10
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
11
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
84
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
85

Output:

           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
4
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
207
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
196
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
197
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
34
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
83
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8
def is_allowed(time_rated, duration):
    if not isinstance(time_rated, int):
        return False
    duration = {'day': 86400, 'week': 604800}.get(duration, float('inf'))
    return time.time() - time_rated > duration
56
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
8__
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
43
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
10
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
27
def is_allowed(time_rated, duration):
    durations = {'day': 86400, 'week': 604800}
    if duration in durations and isinstance(time_rated, int):
        time_difference = time.time() - time_rated
        return time_rated > durations[duration]
    return False
197
           CRASH TIME          SUNRISE
0 2017-06-26 22:00:00  22:26:50.135154
1 2017-06-27 21:50:00  21:26:49.135154
CRASH TIME element type: 
SUNRISE element type: 
CRASH TIME element type: 
47

Làm thế nào để python so sánh dấu thời gian và datetime?

Ví dụ 3: Ở đây một lần nữa chúng tôi so sánh tất cả các dấu thời gian với dấu thời gian (2018-01-05 12:00:00), nhưng đã trả về kết quả so sánh dưới dạng giá trị boolean (true/false) cho tất cả các dấu thời gian. Here again we compared all timestamps with Timestamp(2018-01-05 12:00:00), but returned comparison result as boolean values (True/False) for all timestamps..

Ví dụ 4: Ở đây, hàm tối đa được sử dụng để có được tối đa của tất cả các dấu thời gian, đó là mục gần đây trong cột ‘New_time. Here, the max function is used to get the maximum of all timestamps, that is the recent entry in the ‘new_time’ column.

Ngoài ra, với điều đó, chúng tôi đã tính toán chênh lệch thời gian giữa dấu thời gian thứ nhất và lần thứ hai trong cột ‘New_time.

import pandas as pd
import numpy as np
from datetime import time

#Create fake dataset
df = pd.DataFrame()
df["CRASH TIME"] = pd.to_datetime(['2017-06-26 22:00:00', '2017-06-27 21:50:00'])
df["SUNRISE"] = [time(22, 26, 50, 135154), time(21, 26, 49, 135154)]
print(df.head())

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))

print("SUNRISE element type:", type(df["SUNRISE"][0]))

df["CRASH TIME"] = df["CRASH TIME"].dt.time

print("CRASH TIME element type:", type(df["CRASH TIME"][0]))
6Làm thế nào để python so sánh dấu thời gian và datetime?

Pandas Timestamp tương đương với DateTime trong Python ...

Tạo một khung dữ liệu với các giá trị ngày và thời gian ... In the following program, we initialize two datetime objects, and then check if both datetime objects have same date and time.

Chuyển đổi giá trị ngày và thời gian sang các giá trị dấu thời gian bằng cách sử dụng gấu trúc. Phương thức dấu thời gian () ..

So sánh các dấu thời gian cần thiết bằng cách sử dụng các toán tử so sánh thường xuyên ...

Chúng ta có thể so sánh hai dấu thời gian trong Python không?

Bạn có thể sử dụng bằng toán tử so sánh = để kiểm tra xem một đối tượng DateTime có tương tự như các đối tượng khác không. Trong chương trình sau, chúng tôi khởi tạo hai đối tượng DateTime, sau đó kiểm tra xem cả hai đối tượng DateTime có cùng ngày và thời gian không.. In the following program, we initialize two datetime objects, and then check if both datetime objects have same date and time.the date and time of occurrence of an event. In Python we can get the timestamp of an event to an accuracy of milliseconds. The timestamp format in Python returns the time elapsed from the epoch time which is set to 00:00:00 UTC for 1 January 1970.