dabla commented on PR #40746:
URL: https://github.com/apache/airflow/pull/40746#issuecomment-2227938829

   > Apparently there’s not, Python’s API is just a very thin wrapper around 
the C implementation, and the C API can only use the current locale. Very bad 
but what can you expect from something designed 40 years ago. I guess a context 
manager is the best we can do for now—if bad things happen down the road we’ll 
add a lock to work around it.
   
   This also works, and that's converting the python datetime format to babel:
   
   ```
   def ds_format(ds: str, input_format: str, output_format: str, locale: str | 
None = None) -> str:
       def strftime_to_babel_format(format_str):
           # Mapping of strftime format codes to Babel format codes
           format_mapping = {
               '%a': 'EEE', '%A': 'EEEE', '%w': 'e', '%d': 'dd', '%b': 'MMM', 
'%B': 'MMMM', '%m': 'MM',
               '%y': 'yy', '%Y': 'yyyy', '%H': 'HH', '%I': 'hh', '%p': 'a', 
'%M': 'mm', '%S': 'ss',
               '%f': 'SSSSSS', '%z': 'Z', '%Z': 'z', '%j': 'D', '%U': 'w', 
'%W': 'w',
               '%c': 'EEE MMM dd HH:mm:ss yyyy',
               '%x': 'MM/dd/yy', '%X': 'HH:mm:ss', '%%': '%'
           }
   
           for k, v in format_mapping.items():
               format_str = format_str.replace(k, v)
   
           return format_str
   
       """
       Output datetime string in a given format.
   
       :param ds: Input string which contains a date.
       :param input_format: Input string format (e.g., '%Y-%m-%d').
       :param output_format: Output string format (e.g., '%Y-%m-%d').
       :param locale: Locale used to format the output string (e.g., 'en_US').
                      If None (default), the locale will not be changed.
   
       >>> ds_format("2015-01-01", "%Y-%m-%d", "%m-%d-%y")
       '01-01-15'
       >>> ds_format("1/5/2015", "%m/%d/%Y", "%Y-%m-%d")
       '2015-01-05'
       >>> ds_format("12/07/2024", "%d/%m/%Y", "%A %d %B %Y", "en_US")
       'Friday 12 July 2024'
   
       .. versionadded:: 2.10.0
          The `locale` parameter.
       """
       date_obj = datetime.strptime(str(ds), input_format)
       if locale:
           return format_datetime(date_obj, 
format=strftime_to_babel_format(output_format), locale=locale)
       return date_obj.strftime(output_format)
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to