chopstick
All widgets

World Clock

Comes with Chopstick

Current time in other cities. Show one city by name, or a list of common ones.

Already installed? It's in your widget library.

Type it in the notch's search

  • Time in city

What it runs

A widget is a folder of plain files. Here is all of its code.

widget.json
{
  "id": "world-clock",
  "name": "World Clock",
  "icon": "icon.png",
  "description": "Current time in other cities. Show one city by name, or a list of common ones.",
  "initial": "list",
  "commands": {
    "show": {
      "description": "Show the current time in a city (e.g. New York, Tokyo).",
      "phrase": "Time in {city}", "preview": { "state": "city", "run": "lookup.sh" },
      "inputs": {
        "city": {
          "type": "string",
          "description": "City name"
        }
      },
      "do": [
        {
          "set": {
            "city": "{{input.city}}"
          }
        },
        {
          "run": "lookup.sh",
          "then": "city",
          "else": "failed"
        }
      ]
    }
  },
  "states": {
    "list": {
      "task": true,
      "on_enter": [
        {
          "run": "all.sh"
        }
      ],
      "compact": {
        "leading": {
          "symbol": "clock.fill"
        },
        "trailing": {
          "text": "{{result.items.0.time}}"
        }
      },
      "expanded": {
        "size": "regular",
        "header": {
          "symbol": "clock.fill",
          "title": "World Clock"
        },
        "blocks": [
          {
            "input": {
              "placeholder": "City…",
              "do": [
                {
                  "set": {
                    "city": "{{item.text}}"
                  }
                },
                {
                  "run": "lookup.sh",
                  "then": "city",
                  "else": "failed"
                }
              ]
            }
          },
          {
            "list": {
              "from": "result.items",
              "title": "{{item.name}}",
              "subtitle": "{{item.day}}",
              "do": [
                {
                  "set": {
                    "city": "{{item.name}}"
                  }
                },
                {
                  "run": "lookup.sh",
                  "then": "city",
                  "else": "failed"
                }
              ]
            }
          }
        ]
      },
      "keys": [
        {
          "key": "esc",
          "do": [
            "dismiss"
          ]
        }
      ]
    },
    "city": {
      "task": true,
      "compact": {
        "leading": {
          "symbol": "clock.fill"
        },
        "trailing": {
          "text": "{{result.time}}"
        }
      },
      "expanded": {
        "size": "compact",
        "header": {
          "symbol": "clock.fill",
          "title": "{{result.name}}"
        },
        "blocks": [
          {
            "text": "{{result.time | …}}",
            "style": "title"
          },
          {
            "note": "{{result.day | Looking it up…}}"
          }
        ]
      },
      "keys": [
        {
          "key": "esc",
          "do": [
            "dismiss"
          ]
        },
        {
          "key": "cmd+c",
          "label": "Copy",
          "do": [
            {
              "copy": "{{result.time}}"
            }
          ]
        }
      ]
    },
    "failed": {
      "task": true,
      "expanded": {
        "size": "compact",
        "header": {
          "symbol": "clock.fill",
          "title": "World Clock"
        },
        "blocks": [
          {
            "note": "{{error | City not found}}"
          }
        ]
      },
      "keys": [
        {
          "key": "esc",
          "do": [
            "dismiss"
          ]
        }
      ]
    }
  },
  "preview": {
    "states": [
      "list",
      "city"
    ],
    "data": {
      "result": {
        "name": "New York",
        "time": "5:31 AM",
        "day": "Sunday, Oct 25",
        "items": [
          {
            "name": "New York",
            "time": "5:31 AM",
            "day": "Sun"
          },
          {
            "name": "London",
            "time": "10:31 AM",
            "day": "Sun"
          }
        ]
      }
    }
  }
}
all.sh
out=""
for z in America/New_York America/Los_Angeles Europe/London Europe/Berlin Asia/Tokyo Australia/Sydney; do
  t=$(TZ=$z date '+%-I:%M %p'); d=$(TZ=$z date '+%a')
  n=$(echo "${z#*/}" | tr _ ' ')
  out="$out{\"name\":\"$n\",\"time\":\"$t\",\"day\":\"$d\"},"
done
echo "{\"items\":[${out%,}]}"
lookup.sh
q=$(echo "$CHOPSTICK_CITY" | tr ' ' '_')
f=$(cd /usr/share/zoneinfo && find America Europe Asia Africa Australia Pacific Atlantic Indian -iname "*$q*" 2>/dev/null | head -1)
[ -z "$f" ] && { echo "No time zone found for $CHOPSTICK_CITY" >&2; exit 1; }
t=$(TZ=$f date '+%-I:%M %p'); d=$(TZ=$f date '+%A, %b %-d')
n=$(basename "$f" | tr _ ' ')
echo "{\"name\":\"$n\",\"time\":\"$t\",\"day\":\"$d\"}"