home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 274372547

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/1084#issuecomment-274372547 https://api.github.com/repos/pydata/xarray/issues/1084 274372547 MDEyOklzc3VlQ29tbWVudDI3NDM3MjU0Nw== 1217238 2017-01-23T00:21:49Z 2017-01-23T00:22:44Z MEMBER

I'm not particularly tied to one date parsing approach over another (here I was just mimicking pandas). In an ideal world, what would be your preference?

Honestly, it's probably more reliable to just parse ISO-8601 ourselves, which is intentionally pretty simple. That will give us complete control.

Here's something simple I wrote to parse ISO-8601 using a regex: ```python import re

def named(name, pattern): return '(?P<' + name + '>' + pattern + ')'

def optional(x): return '(?:' + x + ')?'

def trailing_optional(xs): if not xs: return '' return xs[0] + optional(trailing_optional(xs[1:]))

def build_pattern(date_sep='-', datetime_sep='T', time_sep='\:'): pieces = [(None, 'year', '\d{4}'), (date_sep, 'month', '\d{2}'), (date_sep, 'day', '\d{2}'), (datetime_sep, 'hour', '\d{2}'), (time_sep, 'minute', '\d{2}'), (time_sep, 'second', '\d{2}' + optional('.\d+'))] pattern_list = [] for sep, name, sub_pattern in pieces: pattern_list.append((sep if sep else '') + named(name, sub_pattern)) # TODO: allow timezone offsets? return '^' + trailing_optional(pattern_list) + '$'

def parse_iso8601(datetime_string): basic_pattern = build_pattern(date_sep='', time_sep='') extended_pattern = build_pattern() patterns = [basic_pattern, extended_pattern] for pattern in patterns: match = re.match(pattern, datetime_string) if match: return match.groupdict() raise ValueError('no ISO-8601 match for string: %s' % datetime_string)

test cases

print parse_iso8601('1999') print parse_iso8601('19990101') print parse_iso8601('1999-01-01') print parse_iso8601('1999-01-01T12') print parse_iso8601('1999-01-01T12:34') print parse_iso8601('1999-01-01T12:34:56.78') print parse_iso8601('19990101T123456.78') {'minute': None, 'year': '1999', 'second': None, 'month': None, 'hour': None, 'day': None} {'minute': None, 'year': '1999', 'second': None, 'month': '01', 'hour': None, 'day': '01'} {'minute': None, 'year': '1999', 'second': None, 'month': '01', 'hour': None, 'day': '01'} {'minute': None, 'year': '1999', 'second': None, 'month': '01', 'hour': '12', 'day': '01'} {'minute': '34', 'year': '1999', 'second': None, 'month': '01', 'hour': '12', 'day': '01'} {'minute': '34', 'year': '1999', 'second': '56.78', 'month': '01', 'hour': '12', 'day': '01'} {'minute': '34', 'year': '1999', 'second': '56.78', 'month': '01', 'hour': '12', 'day': '01'} ```

{
    "total_count": 1,
    "+1": 1,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  187591179
Powered by Datasette · Queries took 0.744ms · About: xarray-datasette