import sys

import re

# last regexp parts
LAST_USERNAME = "([^ ]+)"
LAST_DEVICE = "([^ ]+)"
LAST_ADDRESS = "([^ ]+)?"
LAST_FROM = "(... ... .. ..:..)"
LAST_DASH = " . "
LAST_TO = "(..:..)"
LAST_DURATION = "([0-9+:]+)"

LAST_RE = re.compile(
  LAST_USERNAME +
  " +" +
  LAST_DEVICE +
  " +" +
  LAST_ADDRESS +
  " +" +
  LAST_FROM +
  LAST_DASH +
  LAST_TO +
  " *\(" +
  LAST_DURATION +
  "\)"
)


def get_users_at(timestamp, last_output=None, now=None):
    """
    Return a set of strings corresponding to which users were logged in from
    which addresses at a given timestamp.

    Optional argument last_output is the output of "last". Will be filled in by
    actually executing "last" if not provided.

    Optional argument now is the current timestamp for parsing last_output. Will
    be taken from the system clock if not provided.
    """
    users = set()
    for line in last_output.splitlines():
        match = LAST_RE.match(line)
        if not match:
            sys.stderr.write(
                "WARNING: Please report unmatched last line at {}: <{}>\n".format(
                    "https://github.com/walles/px/issues/new", line))
            continue

        username = match.group(1)
        device = match.group(2)
        address = match.group(3)
        from_s = match.group(4)
        to_s = match.group(5)
        duration_s = match.group(6)
        print("FIXME: Remove this printout: u={}, d={}, a={}, f={}, t={}, d={}".format(
            username, device, address, from_s, to_s, duration_s
        ))

        from_timestamp = _to_timestamp(from_s, now)
        if timestamp < from_timestamp:
            continue

        duration_delta = _to_timedelta(duration_s)
        to_timestamp = from_timestamp + duration_delta
        if timestamp > to_timestamp:
            continue

        users.add(username)

    return users


def _to_timestamp(string, now):
    return None


def _to_timedelta(string):
    return None

@@ -1,12 +1,28 @@
+import datetime%0A
 import sys%0A%0A
@@ -32,16 +32,17 @@
 ort re%0A%0A
+%0A
 # last r
@@ -409,16 +409,85 @@
 %22%5C)%22%0A)%0A%0A
+TIMEDELTA_RE = re.compile(%22((%5B0-9%5D+)%5C+)?(%5B0-9%5D%5B0-9%5D):(%5B0-9%5D%5B0-9%5D)%22)%0A%0A
 %0Adef get
@@ -1617,16 +1617,33 @@
     ))%0A%0A
+        try:%0A    
         
@@ -1686,32 +1686,36 @@
 s, now)%0A        
+    
 if timestamp %3C f
@@ -1733,32 +1733,36 @@
 mp:%0A            
+   
+ 
 continue%0A%0A      
@@ -1746,33 +1746,283 @@
        continue%0A
-%0A
+        except Exception:%0A            sys.stderr.write(%0A                %22WARNING: Please report problematic1 last line at %7B%7D: %3C%7B%7D%3E%5Cn%22.format(%0A                    %22https://github.com/walles/px/issues/new%22, line))%0A            continue%0A%0A        try:%0A    
         duration
@@ -2064,16 +2064,20 @@
         
+ 
+   
 to_times
@@ -2115,32 +2115,36 @@
 n_delta%0A        
+   
+ 
 if timestamp %3E t
@@ -2172,155 +2172,636 @@
     
-continue%0A%0A        users.add(username)%0A%0A    return users%0A%0A%0Adef _to_timestamp(string, now):%0A    return None%0A%0A%0Adef _to_timedelta(string):%0A    return None
+    continue%0A        except Exception:%0A            sys.stderr.write(%0A                %22WARNING: Please report problematic2 last line at %7B%7D: %3C%7B%7D%3E%5Cn%22.format(%0A                    %22https://github.com/walles/px/issues/new%22, line))%0A            continue%0A%0A        users.add(username)%0A%0A    return users%0A%0A%0Adef _to_timestamp(string, now):%0A    return None%0A%0A%0Adef _to_timedelta(string):%0A    match = TIMEDELTA_RE.match(string)%0A%0A    days = match.group(2)%0A    if days is None:%0A        days = 0%0A    else:%0A        days = int(days)%0A%0A    hours = int(match.group(3))%0A%0A    minutes = int(match.group(4))%0A%0A    return datetime.timedelta(days, hours, minutes)
 %0A

Implement px_loginhistory._to_timedelta()