Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return time.strptime(datetime_string, "%Y%m%dT%H%M%S")
elif string_length == 8:
# YYYYMMDD
return time.strptime(datetime_string, "%Y%m%d")
elif string_length == 27:
# 2011-11-02T14:48:54.908371Z
datetime_string = datetime_string.split(".")[0] + "Z"
return time.localtime(
calendar.timegm(
time.strptime(datetime_string,
"%Y-%m-%dT%H:%M:%SZ")))
else:
logging.error("string has no correct format: %s",
datetime_string)
except ValueError as e:
raise TimestampParseException(e)
def strdatetime(datetime_string, inactive=False):
"""
returns a date string in org format
i.e.: *
@param date-string: has to be a str in
following format: YYYY-MM-DD HH:MM
@param inactive: (boolean) True: use inactive time-stamp; else use active
"""
assert datetime_string.__class__ == str
try:
tuple_date = time.strptime(datetime_string, "%Y-%m-%d %H:%M")
except ValueError as e:
raise TimestampParseException(e)
if inactive:
return OrgFormat.inactive_date(tuple_date, show_time=True)
else:
return OrgFormat.date(tuple_date, show_time=True)
def datetimetupeliso8601(datetime_string):
"""
returns a time_tupel
@param datetime_string: YYYY-MM-DDTHH.MM.SS or
YYYY-MM-DDTHH.MM
"""
assert datetime_string.__class__ == str
try:
if len(datetime_string) == 16: # YYYY-MM-DDTHH.MM
return time.strptime(datetime_string, "%Y-%m-%dT%H.%M")
elif len(datetime_string) == 19: # YYYY-MM-DDTHH.MM.SS
return time.strptime(datetime_string, "%Y-%m-%dT%H.%M.%S")
except ValueError as e:
raise TimestampParseException(e)
logging.debug('force_filedate_extraction and not skip_filetime_extraction: using datetime')
orgdate = OrgFormat.datetime(file_datetime)
self.__write_file(file, link, orgdate)
elif DATESTAMP_REGEX.match(file):
logging.debug('DATESTAMP_REGEX matches; trying __parse_filename_iso_timestamp() ...')
try:
# we put this in a try block because:
# if a timestamp is false i.e. 2011-14-19 or false time
# we can handle those not easy with REGEX, therefore we have
# an Exception TimestampParseException, which is thrown,
# when strptime (parse from string to time tupel) fails
orgdate = self.__parse_filename_iso_timestamp(file, link)
self.__write_file(file, link, orgdate)
except TimestampParseException:
logging.debug('__parse_filename_iso_timestamp() caused an TimestampParseException')
logging.warning("False date(time) in file: %s", link)
def datetupeliso8601(datetime_string):
"""
returns a time_tupel
@param datetime_string: YYYY-MM-DD
"""
assert datetime_string.__class__ == str
try:
return time.strptime(datetime_string, "%Y-%m-%d")
except ValueError as e:
raise TimestampParseException(e)