Сравнение восточного времени (ET) и времени Южной Африки (SAT): методы и примеры кода

Чтобы сравнить восточное время (ET) и южноафриканское время (SAT) и предоставить примеры кода, мы можем использовать различные языки программирования. Вот несколько методов с использованием популярных языков:

Метод 1. Использование Python и библиотеки pytz

from datetime import datetime
import pytz
def compare_timezones():
    et_timezone = pytz.timezone('US/Eastern')
    sat_timezone = pytz.timezone('Africa/Johannesburg')
    current_time_et = datetime.now(et_timezone)
    current_time_sat = datetime.now(sat_timezone)
    print("Current time in Eastern Time:", current_time_et.strftime("%Y-%m-%d %H:%M:%S"))
    print("Current time in South Africa Time:", current_time_sat.strftime("%Y-%m-%d %H:%M:%S"))
compare_timezones()

Метод 2. Использование JavaScript

function compareTimezones() {
  const etTime = new Date().toLocaleString("en-US", { timeZone: "America/New_York" });
  const satTime = new Date().toLocaleString("en-US", { timeZone: "Africa/Johannesburg" });
  console.log("Current time in Eastern Time:", etTime);
  console.log("Current time in South Africa Time:", satTime);
}
compareTimezones();

Метод 3: использование PHP

function compareTimezones() {
  $etTimezone = new DateTimeZone('America/New_York');
  $satTimezone = new DateTimeZone('Africa/Johannesburg');
  $etTime = new DateTime('now', $etTimezone);
  $satTime = new DateTime('now', $satTimezone);
  echo "Current time in Eastern Time: " . $etTime->format('Y-m-d H:i:s') . "\n";
  echo "Current time in South Africa Time: " . $satTime->format('Y-m-d H:i:s') . "\n";
}
compareTimezones();