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

feat: add timezone support #12189

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions debug_scripts/estimate_epoch_start_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import time
import math
import argparse
from datetime import datetime, timedelta
import pytz


# Function to get block data
Expand Down Expand Up @@ -80,20 +82,35 @@ def get_exponential_weighted_epoch_lengths(url,
return epoch_lengths, exponential_weighted_average_epoch_length


# Function to check if timezone is valid
def is_valid_timezone(timezone_str):
try:
pytz.timezone(timezone_str)
return True
except pytz.UnknownTimeZoneError:
return False


# Function to approximate future epoch start dates
def predict_future_epochs(starting_epoch_timestamp, avg_epoch_length,
num_future_epochs):
num_future_epochs, timezone_str):
future_epochs = []
current_timestamp = ns_to_seconds(
starting_epoch_timestamp) # Convert from nanoseconds to seconds

# Set up the timezone
target_timezone = pytz.timezone(timezone_str)

for i in range(1, num_future_epochs + 1):
# Add the average epoch length for each future epoch
future_timestamp = current_timestamp + (i * avg_epoch_length)

# Convert to human-readable format
future_date = time.strftime('%Y-%m-%d %H:%M:%S %A',
time.gmtime(future_timestamp))
# Convert timestamp to datetime in target timezone
future_datetime = datetime.fromtimestamp(future_timestamp,
target_timezone)

# Format date
future_date = future_datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z %A')
future_epochs.append(future_date)

print(f"Predicted start of epoch {i}: {future_date}")
Expand All @@ -103,6 +120,10 @@ def predict_future_epochs(starting_epoch_timestamp, avg_epoch_length,

# Main function to run the process
def main(args):
if not is_valid_timezone(args.timezone):
print(f"Error: Invalid timezone '{args.timezone}'")
return

latest_block = get_block(args.url, None)
next_epoch_id = latest_block['next_epoch_id']
current_epoch_first_block = get_block(args.url, next_epoch_id)
Expand All @@ -116,7 +137,7 @@ def main(args):
# Predict future epoch start dates
predict_future_epochs(current_timestamp,
exponential_weighted_average_epoch_length,
args.num_future_epochs)
args.num_future_epochs, args.timezone)


# Custom action to set the URL based on chain_id
Expand Down Expand Up @@ -156,6 +177,10 @@ def __call__(self, parser, namespace, values, option_string=None):
type=int,
default=3,
help="Number of future epochs to predict.")
parser.add_argument(
"--timezone",
default="UTC",
help="Time zone to display times in (e.g., 'America/New_York').")

args = parser.parse_args()
main(args)
Loading