-
Notifications
You must be signed in to change notification settings - Fork 89
Adjust teamviewer plugin to account for daylight savings time in timestamps #1614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
1e13071
a7cdb7d
334d7fa
1d7e4c5
3562f9f
2d708e1
5e46152
f92238d
3c2e678
8734848
475f91d
5a29da7
c442096
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -131,6 +131,8 @@ def logs(self) -> Iterator[RemoteAccessLogRecord]: | |
| logfile = self.target.fs.path(logfile) | ||
|
|
||
| start_date = None | ||
| prev_timestamp = None | ||
| fold = 0 | ||
| for line in logfile.open("rt", errors="replace"): | ||
| if not (line := line.strip()) or line.startswith("# "): | ||
| continue | ||
|
|
@@ -141,6 +143,18 @@ def logs(self) -> Iterator[RemoteAccessLogRecord]: | |
| except Exception as e: | ||
| self.target.log.warning("Failed to parse Start message %r in %s", line, logfile) | ||
| self.target.log.debug("", exc_info=e) | ||
| # Unset start_date if it was already defined | ||
| start_date = None | ||
|
|
||
| fold = 0 | ||
| if start_date is None: | ||
| continue | ||
|
|
||
| # See whether the utcoffset with the two different timezones are the same | ||
| target_start_date = start_date.replace(tzinfo=target_tz) | ||
| if target_start_date.utcoffset() == start_date.utcoffset(): | ||
| # Adjust the start_date so it uses the timezone known to target throughout | ||
| start_date = target_start_date | ||
|
|
||
| continue | ||
|
|
||
|
|
@@ -177,13 +191,22 @@ def logs(self) -> Iterator[RemoteAccessLogRecord]: | |
| time += ".000000" | ||
|
|
||
| try: | ||
| timestamp = datetime.strptime(f"{date} {time}", "%Y/%m/%d %H:%M:%S.%f").replace( | ||
| tzinfo=start_date.tzinfo if start_date else target_tz | ||
| ) | ||
| tz_info = start_date.tzinfo if start_date else target_tz | ||
| timestamp = datetime.strptime(f"{date} {time}", "%Y/%m/%d %H:%M:%S.%f").replace(tzinfo=tz_info) | ||
| except Exception as e: | ||
| self.target.log.warning("Unable to parse timestamp %r in file %s", line, logfile) | ||
| self.target.log.debug("", exc_info=e) | ||
| timestamp = 0 | ||
| timestamp = prev_timestamp or start_date | ||
|
|
||
| if timestamp and prev_timestamp and prev_timestamp > timestamp: | ||
| # We might currently be in a grey area where the dst period ended. | ||
| # replace the fold value of the timestamp | ||
| fold = 1 | ||
|
|
||
| if timestamp: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming we have the target timezone what if we renormalize every timestamp, instead of detecting deltas: Would that not also help with "spring forward" events? In other words, why not rely on the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know that the fold was used for this. I did notice when working on it that the fold doesn't get passed to flow.record. I made this PR for it fox-it/flow.record#217 |
||
| timestamp = timestamp.replace(fold=fold) | ||
|
|
||
| prev_timestamp = timestamp | ||
|
|
||
| yield self.RemoteAccessLogRecord( | ||
| ts=timestamp, | ||
|
|
@@ -247,6 +270,7 @@ def parse_start(line: str) -> datetime | None: | |
|
|
||
| Start: 2021/11/11 12:34:56 | ||
| Start: 2024/12/31 01:02:03.123 (UTC+2:00) | ||
| Start: 2025/01/01 12:28:41.436 (UTC) | ||
| """ | ||
| if match := RE_START.search(line): | ||
| dt = match.groupdict() | ||
|
|
@@ -256,13 +280,21 @@ def parse_start(line: str) -> datetime | None: | |
| dt["time"] = dt["time"].rsplit(".")[0] | ||
|
|
||
| # Format timezone, e.g. "UTC+2:00" to "UTC+0200" | ||
| if dt["timezone"]: | ||
| name, operator, amount = re.split(r"(\+|\-)", dt["timezone"]) | ||
| amount = int(amount.replace(":", "")) | ||
| dt["timezone"] = f"{name}{operator}{amount:0>4d}" | ||
| if timezone := dt["timezone"]: | ||
| identifier = " %Z%z" | ||
| # Handle just UTC timezone | ||
| if timezone.lower() == "utc": | ||
| timezone = " UTC+00:00" | ||
| else: | ||
| name, operator, amount = re.split(r"(\+|\-)", timezone) | ||
| amount = int(amount.replace(":", "")) | ||
| timezone = f" {name}{operator}{amount:0>4d}" | ||
| else: | ||
| timezone = "" | ||
| identifier = "" | ||
|
|
||
| return datetime.strptime( # noqa: DTZ007 | ||
| f"{dt['date']} {dt['time']}" + (f" {dt['timezone']}" if dt["timezone"] else ""), | ||
| "%Y/%m/%d %H:%M:%S" + (" %Z%z" if dt["timezone"] else ""), | ||
| f"{dt['date']} {dt['time']}{timezone}", | ||
| f"%Y/%m/%d %H:%M:%S{identifier}", | ||
| ) | ||
| return None | ||
Uh oh!
There was an error while loading. Please reload this page.