fananimi / pyzk

Unofficial library of zkteco fingerprint attendance machine
GNU General Public License v2.0
489 stars 319 forks source link

suggestion... get_attendance(date range) #198

Closed shadow046 closed 9 months ago

shadow046 commented 9 months ago

can you please add a datetime range to get the attendance.. your code is good.. thanks

kurenai-ryu commented 9 months ago

as mentioned before, date filtering should be done on your side, the device doesn't offer any way to filter the attendance data

shadow046 commented 9 months ago

ok thanks this is what i did to filter the date range.. hope his help others. try: conn = zk.connect() conn.disable_device() print ("use zkteco;")

Prompt the user for the date range with validation

      while True:
          try:
              from_date_str = input("Enter the 'from' date (YYYY-MM-DD): ")
              from_date = datetime.datetime.strptime(from_date_str, "%Y-%m-%d").replace(hour=0, minute=0, second=0)
              break  # Break out of the loop if the input is valid
          except ValueError:
              print("Invalid date format. Please use the format YYYY-MM-DD.")

      while True:
          try:
              to_date_str = input("Enter the 'to' date (YYYY-MM-DD): ")
              to_date = datetime.datetime.strptime(to_date_str, "%Y-%m-%d").replace(hour=23, minute=59, second=59)
              break  # Break out of the loop if the input is valid
          except ValueError:
              print("Invalid date format. Please use the format YYYY-MM-DD.")

      attendance = conn.get_attendance()
      sn = conn.get_serialnumber()

      for att in attendance:
          attn = att.timestamp.strftime("%Y-%m-%d %H:%M:%S")

          # Filter records based on the date range
          if from_date <= att.timestamp <= to_date:
              print("INSERT IGNORE INTO attendance (userid, timestamp, sn) VALUES ('{}', '{}', '{}');".format(att.user_id, attn, sn))
  except BasicException as e:
      print (e)
      print ('')
  except Exception as e:
      print ("Process terminate : {}".format(e))
      print ("Error: %s" % sys.exc_info()[0])
      print ('-'*60)
      traceback.print_exc(file=sys.stdout)
      print ('-'*60)
  finally:
      if conn:
          conn.enable_device()
          conn.disconnect()