-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
29 lines (22 loc) · 1 KB
/
test.py
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
import json
from datetime import datetime
def get_current_value_inc_vat(json_file_path):
# Load JSON data from the file
with open(json_file_path, 'r') as file:
data = json.load(file)
# Get the current date and time
current_datetime = datetime.now()
# Iterate through the "results" list to find the corresponding "value_inc_vat"
for result in data['results']:
valid_from = datetime.strptime(result['valid_from'], "%Y-%m-%dT%H:%M:%SZ")
valid_to = datetime.strptime(result['valid_to'], "%Y-%m-%dT%H:%M:%SZ")
# Check if the current time is within the valid range
if valid_from <= current_datetime <= valid_to:
return result['value_inc_vat']
# If no matching time range is found, return None or an appropriate value
return None
# Example usage with the file path
json_file_path = 'data.json'
current_value_inc_vat = get_current_value_inc_vat(json_file_path)
# Print or use the result
print("Current value_inc_vat:", current_value_inc_vat)