Disable `strftime` in the code base

I want to make sure that no developer on our code base can ever verify the code that uses strftime. This is a super useful method, but since it cannot be used on dates until 1900 , it is a constant source of errors that we only discover after the code lives on.

I thought of several ways to do this so that it cannot be used:

  • Create a check in Intellij that causes an error when encountered strftime.

    (This seems OK, but creating Intellij inspections is a big deal.)

  • Create a unittest that greped any changes that are currently in the code and make sure that strftimeit doesn’t come back again.

    (Seems hacky and adds a dependency for some git tool.)

  • Create a git hook that explodes when strftime appears in commit.

    (Having looked at this, but git, the hooks should be manually marked, which type the point view wins.)

  • A subclass of a module datetimewith an implementation that causes an error NotImplemented, if strftimeever used.

    (But people could still import the regular datetime module.)

  • Just train yourself and everyone else that this is the devil.

    (But so far it has not worked.)

I feel that this is probably what has a standard solution. Any suggestions?

+4
source share
4 answers

pylint lint . , basic_checker , ( , ..).

pre-commit git, , .

+2

(4). , , . __init__.py, :

import datetime

class datetime(datetime.datetime):
    # Override strftime, and make any other changes here

datetime.datetime = datetime

datetime.datetime . , , datetime.strftime(), .

EDIT: , , Python, . :

import warnings

class datetime(datetime.datetime):
    def strftime(self, *args, **kwargs):
        warnings.warn('strftime is deprecated', DeprecationWarning, stacklevel=2)
        return super(datetime, self).strftime(*args, **kwargs)

. , Django. :

warnings.filterwarnings('error', 'strftime is deprecated', DeprecationWarning,
                        r'your_package(\..+)?') # regex matching your package

your_package. , - .

+1

del time.strftime .

, AttributeError .

+1

wx.DateTime wxPython - , strftime - , wx, wx.App .

:

  • : 4714 B.C. 480 .
  • : , , .
  • Many functions: not only all usual calculations with dates are supported, but also more exotic calculations by weeks and years, workday testing, standard astronomical functions, conversion to strings and from strings in a simple or free format.
  • Efficiency: wxDateTime objects are small (8 bytes) and work with them quickly
  • DST in some time zones.
+1
source

All Articles