Date parsers

This module contains the following parsers:

Date - command date

DateUTC - command date --utc

TimeDateCtlStatus - command timedatectl status

class insights.parsers.date.Date(context, extra_bad_lines=None)[source]

Bases: DateParser

Class to parse date command output.

Sample in:

Fri Jun 24 09:13:34 CST 2016

Examples

>>> from insights.parsers.date import Date
>>> from insights.tests import context_wrap
>>> date_content = "Mon May 30 10:49:14 CST 2016"
>>> shared = {Date: Date(context_wrap(date_content))}
>>> date_info = shared[Date]
>>> date_info.data
'Mon May 30 10:49:14 CST 2016'
>>> date_info.datetime is not None
True
>>> date_info.timezone
'CST'
exception insights.parsers.date.DateParseException[source]

Bases: Exception

class insights.parsers.date.DateParser(context, extra_bad_lines=None)[source]

Bases: CommandParser

Base class implementing shared code.

parse_content(content)[source]

Parses the output of the date and date --utc command.

Sample: Fri Jun 24 09:13:34 CST 2016 Sample: Fri Jun 24 09:13:34 UTC 2016

datetime

A native datetime.datetime of the parsed date string

Type:

datetime.datetime

timezone

The string portion of the date string containing the timezone

Type:

str

Raises

DateParseException: Raised if any exception occurs parsing the content.

class insights.parsers.date.DateUTC(context, extra_bad_lines=None)[source]

Bases: DateParser

Class to parse date --utc command output.

Sample in:

Fri Jun 24 09:13:34 UTC 2016

Examples

>>> from insights.parsers.date import DateUTC
>>> from insights.tests import context_wrap
>>> date_content = "Mon May 30 10:49:14 UTC 2016"
>>> shared = {DateUTC: DateUTC(context_wrap(date_content))}
>>> date_info = shared[DateUTC]
>>> date_info.data
'Mon May 30 10:49:14 UTC 2016'
>>> date_info.datetime
datetime.datetime(2016, 5, 30, 10, 49, 14)
>>> date_info.timezone
'UTC'
class insights.parsers.date.TimeDateCtlStatus(context, extra_bad_lines=None)[source]

Bases: CommandParser, dict

Class to parse the timedatectl status command output. It saves the infomartion in each line into a dict. Since the colon in all the lines except warning is aligned, every line is splited by the same colon index. The key is the lowercase of the first part joined by underscore after splitting it by colon, and the value is the left part after the colon. If the next line is continue line, then append it to the previous key. And also it converts the value to datetime format for “Local time”, “Universal time” and “RTC time”.

Sample in:

               Local time: Mon 2022-11-14 23:04:06 PST
           Universal time: Tue 2022-11-15 07:04:06 UTC
                 RTC time: Tue 2022-11-15 07:04:05
                Time zone: US/Pacific (PST, -0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: yes
          Last DST change: DST ended at
                           Sun 2022-11-06 01:59:59 EDT
                           Sun 2022-11-06 01:00:00 EST
          Next DST change: DST begins (the clock jumps one hour forward) at
                           Sun 2023-03-12 01:59:59 EST
                           Sun 2023-03-12 03:00:00 EDT

Warning: The system is configured to read the RTC time in the local time zone.
        This mode cannot be fully supported. It will create various problems
        with time zone changes and daylight saving time adjustments. The RTC
        time is never updated, it relies on external facilities to maintain it.
        If at all possible, use RTC in UTC by calling
        'timedatectl set-local-rtc 0'.
Raises:
  • DateParseException -- when the datetime in “Local time”, “Universal time”, “RTC time” are not in expected format.

  • ParseException -- when the colon in each line except warning is not aligned.

Examples

>>> ctl_info['ntp_service']
'active'
>>> ctl_info['system_clock_synchronized']
'yes'
>>> ctl_info['local_time']
datetime.datetime(2022, 11, 14, 23, 4, 6)
parse_content(content)[source]

This method must be implemented by classes based on this class.