title: Python: Finding Local Mean Time (Adjusted for the distance in longitude from the locations time zone meridian)
body: <p>I am currently constructing a function with the inputs (Location, Time) "City, State, Country" and "Year-Month-Day Hour:Min:Sec" with the output of sidereal time that can be used in astronomical calculations. Currently I am passing the location through geopy to find the longitude/latitude and then using timezonefinder to find the exact time zone.</p>
  
  <p>From there I am using pytz to convert my local time to UTC while taking into account DST. I have a database built in which I can find the sidereal time using that converted Time and Date.</p>
  
  <p>My issue is to be accurate I can not use the local time I need to use the local mean time, which is the local time adjusted depending on how far away it is from the time zone meridian. For example if your city has a longitude of -80 and is in America/New_York (Eastern Time) the meridian which actually lines up with the sun is -75 Longitude meaning you are actually 20 minutes earlier then Eastern Time with respect to the sun. But because of DST and the fact that the timezones are not really treated as uniform segments on a map, I can not find a solid way of calculating this difference.</p>
  
  <p>The best way I can think of doing it is to convert my location into a long/lat. Find the time zone for that location and then covert that timezone somehow into the meridian.</p>
  
  <p>I will post my code for others who work better with visuals.</p>
  
  <pre><code>from timezonefinder import TimezoneFinder
  import geopy.geocoders, pytz, certifi, ssl, datetime, ephem, math
  
      def position(city, state, country):
  
      """
  
      :param city: String of city Ex. Chattannoga
      :param state: String of state Ex. TN
      :param country: String USA
      :return: latitude and longitude
  
      """
  
      ctx = ssl.create_default_context(cafile=certifi.where())
      geopy.geocoders.options.default_ssl_context = ctx
      geo_locator = geopy.geocoders.Nominatim(user_agent="my-application", scheme='http')
      location = geo_locator.geocode(city + ' ' + state + ' ' + country)
  
      return location.longitude, location.latitude
  
  
  def timezone(longitude, latitude):
  
      """
  
      :param longitude:
      :param latitude:
      :return: timezone
  
      """
  
      tf = TimezoneFinder()
      zone = (tf.certain_timezone_at(lng=longitude, lat=latitude))
  
      return zone
  
  # def localmeantime(longitude):
  #     zones = [-0, -15, -30, -45, -60, -75, -90, -105, -120, -135, -150, -165, -180]
  #     x = min(zones, key=lambda x: abs(x - longitude))
  #     print(x)
  
  
  
  def localtoutc(time, timezo):
  
      """
  
      :param time: Ex. "2001-2-3 10:11:12" String
      :param timezo: Ex. America/New_York String
      :return:
  
      """
  
      local = pytz.timezone(timezo)
      naive = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
      local_dt = local.localize(naive, is_dst=None)
      utc_dt = local_dt.astimezone(pytz.utc)
      return utc_dt
  </code></pre>
  
  <p>The other way I can think of is to find out if the location is in DST and then finding out the meridian by counting how many hours have passed. 15 degrees for every hour.</p>
  
  <p>I am wondering if there is a better way to get local mean time or if there is a way to get sidereal time with all these adjustments much easier.</p>