Fetches a ``datetime.datetime`` parameter via :func:`get_string`.
968076
def get_datetime(strings: Sequence[str],
                 prefix: str,
                 datetime_format_string: str,
                 ignoreleadingcolon: bool = False,
                 precedingline: str = "") -> Optional[datetime.datetime]:
    
    x = get_string(strings, prefix, ignoreleadingcolon=ignoreleadingcolon,
                   precedingline=precedingline)
    if len(x) == 0:
        return None
    # For the format strings you can pass to datetime.datetime.strptime, see
    # http://docs.python.org/library/datetime.html
    # A typical one is "%d-%b-%Y (%H:%M:%S)"
    d = datetime.datetime.strptime(x, datetime_format_string)
    return d
399668
def string_to_datetime(self, obj):
        
        if isinstance(obj, six.string_types) and len(obj) == 19:
            try:
                return datetime.strptime(obj, "%Y-%m-%dT%H:%M:%S")
            except ValueError:
                pass
        if isinstance(obj, six.string_types) and len(obj) > 19:
            try:
                return datetime.strptime(obj, "%Y-%m-%dT%H:%M:%S.%f")
            except ValueError:
                pass
        if isinstance(obj, six.string_types) and len(obj) == 10:
            try:
                return datetime.strptime(obj, "%Y-%m-%d")
            except ValueError:
                pass
        return obj
132821
def strptime(cls, data, format, scale=DEFAULT_SCALE):  # pragma: no cover
        
        return cls(datetime.strptime(data, format), scale=scale)
1086476
def _get_string_slice(self, key):
        """"""
        parsed, resolution = _parse_iso8601_with_reso(self.date_type, key)
        try:
            loc = self._partial_date_slice(resolution, parsed)
        except KeyError:
            raise KeyError(key)
        return loc
930820
def parse_datetime(value):
    """"""

    if isinstance(value, (string_types, text_type, binary_type)):
        value = dateutil.parser.parse(value)
        value.replace(tzinfo=dateutil.tz.tzutc())
        return value
    elif isinstance(value, datetime.datetime):
        value.replace(tzinfo=dateutil.tz.tzutc())
        return value
    elif isinstance(value, datetime.date):
        value = datetime.datetime(value.year, value.month, value.day)
        value.replace(tzinfo=dateutil.tz.tzutc())
        return value
    else:
        raise ValueError('Value must be parsable to datetime object. Got `{}`'.format(type(value)))
932214
def get_cgi_parameter_datetime(form: cgi.FieldStorage,
                               key: str) -> Optional[datetime.datetime]:
    
    try:
        s = get_cgi_parameter_str(form, key)
        if not s:
            # if you dateutil.parser.parse() an empty string,
            # you get today's date
            return None
        d = dateutil.parser.parse(s)
        if d.tzinfo is None:  # as it will be
            d = d.replace(tzinfo=dateutil.tz.tzlocal())
        return d
    except ValueError:
        return None
351134
def str2dt(time: datetime) -> np.ndarray:
    
    if isinstance(time, datetime):
        return time
    elif isinstance(time, str):
        return parse(time)
    elif isinstance(time, np.datetime64):
        return time.astype(datetime)
    else:  # some sort of iterable
        try:
            if isinstance(time[0], datetime):
                return time
            elif isinstance(time[0], np.datetime64):
                return time.astype(datetime)
            elif isinstance(time[0], str):
                return [parse(t) for t in time]
        except (IndexError, TypeError):
            pass

        # last resort--assume pandas/xarray

        return time.values.astype('datetime64[us]').astype(datetime)
515191
def parse_date(dateString):
    ''''''
    if not dateString:
        return None
    for handler in _date_handlers:
        try:
            date9tuple = handler(dateString)
        except (KeyError, OverflowError, ValueError):
            continue
        if not date9tuple:
            continue
        if len(date9tuple) != 9:
            continue
        return date9tuple
    return None
283023
def datetime_to_str(date_time=None, str_format='%Y-%m-%d_%H:%M:%S.fff'):
    
    date_time = date_time or cst_now()
    if DEFAULT_TIMEZONE_AWARE:
        date_time += timedelta(hours=DEFAULT_TIMEZONE)
    ret = date_time.strftime(str_format.split('.f')[0])
    if '.f' in str_format:
        sig_digits = len(str_format.split('.')[-1]) + 1
        ret += str(date_time.microsecond / 1000000.0
                   )[1:].ljust(sig_digits, '0')[:sig_digits]
    return ret
943282
def strptime(cls, date_string, format):
        ''
        import _strptime
        return _strptime._strptime_datetime(cls, date_string, format)
137258
def extract(cls, timestamp_str):
        
        if not timestamp_str:
            raise DateTimeFormatterException(
                'timestamp_str must a valid string {}'.format(timestamp_str))

        if isinstance(timestamp_str, (date, datetime)):
            return timestamp_str

        try:
            return cls.extract_datetime(timestamp_str)
        except DateTimeFormatterException:
            pass

        try:
            return cls.extract_datetime_hour(timestamp_str)
        except DateTimeFormatterException:
            pass

        try:
            return cls.extract_date(timestamp_str)
        except DateTimeFormatterException as e:
            raise DateTimeFormatterException(e)
86026
def getDateTimeFromGet(request,key):
    '''
    
    '''
    if request.GET.get(key,''):
        try:
            return ensure_timezone(datetime.strptime(unquote(request.GET.get(key,'')),'%Y-%m-%d'))
        except (ValueError, TypeError):
            pass
    return None
678132
def time_str (time_t, slug = False):
  '''
  '''
  return datetime.fromtimestamp (int (time_t)).strftime (
    DEFAULT_SLUGFORMAT if slug else DEFAULT_TIMEFORMAT)
751963
def parse(self, s):
        
        return datetime.datetime.strptime(s, self.date_format).date()
1340455
def string_to_date(value):
    
    if isinstance(value, datetime.date):
        return value

    return dateutil.parser.parse(value).date()
281883
def parse_datetime(value):
    
    if not value:
        return None
    elif isinstance(value, datetime.datetime):
        return value
    return dateutil.parser.parse(value)
169431
def DateStringToDateObject(date_string):
  """"""
  # If this becomes a bottleneck date objects could be cached
  if re.match('^\d{8}$', date_string) == None:
    return None
  try:
    return datetime.date(int(date_string[0:4]), int(date_string[4:6]),
                         int(date_string[6:8]))
  except ValueError:
    return None