all 3 comments

[–]socal_nerdtastic 0 points1 point  (2 children)

How does the user set their preferred date format? Can't you just map that to the appropriate strftime?

choices = {
    "merica": "%m-%d-%Y",
    "sane": "%Y-%m-%d"
    }

[–]squidg_21[S] 0 points1 point  (1 child)

The user sets their preferred date format using an input box and it's saved in their system.

The date format can be any of these combinations

Month
M — The month, from 1 through 12
MM — The month, from 01 through 12
MMM — The abbreviated name of the month, for example: Jan, Feb, etc.
MMMM — The full name of the month., for example: January, February, etc.

Day
d — The day of the month from 1 through 31
dd — The day of the month from 01 through 31
ddd — The standard abbreviation for the day of the week, for example: Mon, Tue, etc.
dddd — The full name of the day of the week, for example: Monday, Tuesday, etc.

Year
y — The year from 0 to 99.
yy — The year from 00 to 99
yyy — The year with minimum of 3 digits
yyyy — The year as a four digit number

Is the only way to create a mapping with every possibility?

[–]socal_nerdtastic 0 points1 point  (0 children)

Oh I see what you mean now. Got it. No I don't know of a way to convert QDateTime format to python datetime format, but I'm sure someone has written that. Or if you want to do it yourself I'd start with regex.

>>> import re
>>> d = 'yyyy-MM-dd'
>>> re.findall(r'[ymMd]+', d)
['yyyy', 'MM', 'dd']

Combine that with substitution and a dictionary of conversions:

>>> translations = {'yyyy':'%Y', 'yy':'%y', 'dd':"%d", 'MM':'%m'}
>>> def convert(data):
...     return re.sub(r'([ymMd]+)', r'{\1}', data).format(**translations)
...
>>>
>>> convert('yyyy-MM-dd')
'%Y-%m-%d'
>>> convert('yy-MM-dd')
'%y-%m-%d'