Parse date string on client side

Hello anvil peeps.

I am trying to parse a date string (e.g. ‘2020-03-20’) to a date object on the client-side so I can do some filtering of table rows. I have searched around the forum, and haven’t seen anything specific about this error on the client-side.

When I try:

self.data_filters[column] = [datetime.datetime.strptime(d, "YYYY-MM-DD") for d in values]

I get:

NotImplementedError: _strptime is not yet implemented in Skulpt

But, the Skupt docs seem to suggest it has been implemented. I am trying to avoid calls to the server every time someone wants to filter a table by date.

Other than manually implementing a parser, any good recommendations.

I should mention that I am running Anvil on-prem, and maybe I am a couple of updates behind.

Thank you.

Anders.

I have added this bit of code to manually parse date strings in the client:

  def parse_date(self, date_string):
    year,month,day = [int(d) for d in date_string.split("-")]
    return datetime.date(year, month, day)
1 Like

Note that parameter self is not being used. If you remove it, then your parse_date becomes a simple, ordinary (non-member) function. It does not have to be part of a class (or class instance) in order to work.

Thank you @p.colbert. That is true. I appreciate that. I am using it as part of a specific component at the moment. If I need it more widely, I will factor it out to a utils module.

Or add a @staticmethod decorator. :slight_smile:

at the moment skulpt implements an early version of the python datetime module.

https://github.com/skulpt/skulpt/blob/master/src/lib/datetime.py

it could certainly do with strptime - you could make a request on the skulpt issues page and/or a feature request here.

Thank you @stucork. I did check out the code, and I was surprised that it has this in it (around line 1500 or so):

@classmethod
def strptime(cls, date_string, format):
    'string, format -> new datetime parsed from a string (like time.strptime()).'
    from _strptime import _strptime
    # _strptime._strptime returns a two-element tuple.  The first
    # element is a time.struct_time object.  The second is the
    # microseconds (which are not defined for time.struct_time).
    struct, micros = _strptime(date_string, format)
    return cls(*(struct[0:6] + (micros,)))

So, I was confused. Am I missing something?

good point - it’s the _strptime module that’s not implemented.

I wonder if there’s a version of python that has this module that would be easily compatible with skulpt :thinking:

Ahh… thank you… I’ll put in a request for implementation.

looking at the CPython source code it’d also need the calendar module implemented.