Skip to content

Add __format__ support for date and datetime classes#174

Open
meshya wants to merge 3 commits into
slashmili:mainfrom
meshya:main
Open

Add __format__ support for date and datetime classes#174
meshya wants to merge 3 commits into
slashmili:mainfrom
meshya:main

Conversation

@meshya
Copy link
Copy Markdown

@meshya meshya commented Dec 2, 2025

  • Edit __format__ method to date class with default format %Y-%m-%d
  • Edit __format__ method to datetime class with special format specifiers:
    • 's' - Unix timestamp as integer
    • 'f' - Unix timestamp as float
    • Other formats passed to strftime()
  • Add unit tests for both classes

Comment thread jdatetime/__init__.py
return self.strftime('%Y-%m-%d %H:%M:%S') + f'{mil}{tz}'

def __format__(self, _format):
if _format == 's':
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, the built-in datetime does not support s or f as format specifiers for timestamps. Since jdatetime aims to maintain maximum compatibility with the built-in datetime, I’m not sure that adding these specifiers is a good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants