parse_datetime
- ketos.data_handling.data_handling.parse_datetime(to_parse, fmt=None, replace_spaces='0')[source]
Parse date-time data from string.
Returns None if parsing fails.
If the year is encoded with only two figures, it is parsed to the most recent past year ending in those two figures. For example, 45 would be parsed to 1945 (assuming that the program is being executed in a year earlier than 2045).
- Args:
- to_parse: str
String with date-time data to parse.
- fmt: str
String defining the date-time format. Example: %d_%m_%Y* would capture “14_3_1999.txt” See https://pypi.org/project/datetime-glob/ for a list of valid directives. In addition to the directives allowed by the datetime-glob package, it is also possible to specify %S.%ms for milliseconds. Note that the milliseconds (%ms) must follow the seconds (%S) separated by a period (.) or underscore (_) and can only be followed by an asterisk (*) or nothing.
- replace_spaces: str
If string contains spaces, replaces them with this string
- Returns:
datetime: datetime object
- Examples:
>>> #This will parse dates in the day/month/year format, >>> #separated by '/'. It will also ignore any text after the year, >>> # (such as a file extension ) >>> >>> from ketos.data_handling.data_handling import parse_datetime >>> fmt = "%d/%m/%Y*" >>> result = parse_datetime("10/03/1942.txt", fmt) >>> result.year 1942 >>> result.month 3 >>> result.day 10 >>> >>> # Now with the time (hour:minute:second) separated from the date by an underscore >>> fmt = "%H:%M:%S_%d/%m/%Y*" >>> result = parse_datetime("15:43:03_10/03/1918.wav", fmt) >>> result.year 1918 >>> result.month 3 >>> result.day 10 >>> result.hour 15 >>> result.minute 43 >>> result.second 3