time.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. include_once("oracles/base.php");
  3. class time extends oracle {
  4. public $info = [
  5. "name" => "what time is it?"
  6. ];
  7. public function check_query($q) {
  8. $prompts = [
  9. "what", "time", "is", "it",
  10. "right", "now", "the", "current",
  11. "get", "date"
  12. ];
  13. $q = str_replace(",", "", $q);
  14. $q = str_replace("?", "", $q);
  15. $q = str_replace("what's", "what is", $q);
  16. $oq = $q;
  17. $q = explode(" ", $q);
  18. $count = 0;
  19. foreach ($q as $word) {
  20. if (in_array($word, $prompts)) {
  21. $count++;
  22. }
  23. }
  24. // remove one from total count if a timezone is specified
  25. return ($count/(count($q) + (str_contains($oq, "tz:") ? -1 : 0))) > 3/4;
  26. }
  27. public function generate_response($q) {
  28. $timezone = timezone_name_from_abbr("UTC");
  29. foreach (explode(" ", $q) as $word) {
  30. if (str_starts_with($word, "tz:")) {
  31. $decltz = timezone_name_from_abbr(substr($word, 3, 3));
  32. if ($decltz) {
  33. $timezone = $decltz;
  34. }
  35. }
  36. }
  37. date_default_timezone_set($timezone);
  38. return [
  39. "The time in ".$timezone => date("H:i:s"),
  40. " " => date("l, F jS"),
  41. "" => "include the string \"tz:XXX\" to use timezone XXX"
  42. ];
  43. }
  44. }
  45. ?>