# -*- coding: utf-8 -*- ############################################################################### # # Cybrosys Technologies Pvt. Ltd. # # Copyright (C) 2025-TODAY Cybrosys Technologies() # Author: Cybrosys Techno Solutions() # # You can modify it under the terms of the GNU AFFERO # GENERAL PUBLIC LICENSE (AGPL v3), Version 3. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU AFFERO GENERAL PUBLIC LICENSE (AGPL v3) for more details. # # You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE # (AGPL v3) along with this program. # If not, see . # ############################################################################### import logging import pytz from odoo import api, fields, models from datetime import datetime from geopy.geocoders import Nominatim from timezonefinder import TimezoneFinder _logger = logging.getLogger(__name__) class ResPartner(models.Model): _inherit = 'res.partner' contact_local_time_tz = fields.Char( string="Local Time", readonly=True, help="Local Time based on address") contact_local_tz = fields.Selection( lambda self: [(tz, tz) for tz in sorted(pytz.all_timezones, key=lambda tz: tz if not tz.startswith('Etc/') else '_')], string='Timezone', readonly=True, help="Timezone based on address" ) def action_compute_contact_local_time_tz(self): """Compute the timezone and local time based on address.""" geolocator = Nominatim(user_agent="odoo_timezone_app") tz_finder = TimezoneFinder() for record in self: record.contact_local_tz = False record.contact_local_time_tz = False try: # Build address for geocoding address = ", ".join(filter(None, [ record.state_id.name, record.country_id.name ])) location = geolocator.geocode(address) if location: tz_str = tz_finder.timezone_at(lat=location.latitude, lng=location.longitude) if tz_str: record.contact_local_tz = tz_str tz = pytz.timezone(tz_str) local_time = datetime.now(tz) record.contact_local_time_tz = local_time.strftime("%Y-%m-%d %H:%M:%S") except Exception as e: _logger.exception(f"Error retrieving timezone or local time for {record.name}: {e}")