Skip to content

Instantly share code, notes, and snippets.

@mdawaffe
Created December 5, 2013 02:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdawaffe/7799053 to your computer and use it in GitHub Desktop.
Save mdawaffe/7799053 to your computer and use it in GitHub Desktop.
Express the Current Approximate Phase of the Moon as an Emoji Character in UTF-8
#!/bin/bash
# Approximates the phase of the moon using a known new-moon time and a fixed lunar phase of 2551443 seconds.
# Outputs the phase as an emoji in UTF-8
CHARS=(
"\xf0\x9f\x8c\x91" # 0 - NEW
"\xf0\x9f\x8c\x91" # 1 - NEW
"\xf0\x9f\x8c\x92" # 2 - WAXING CRESCENT
"\xf0\x9f\x8c\x92" # 3 - WAXING CRESCENT
"\xf0\x9f\x8c\x92" # 4 - WAXING CRESCENT
"\xf0\x9f\x8c\x92" # 5 - WAXING CRESCENT
"\xf0\x9f\x8c\x93" # 6 - FIRST QUARTER
"\xf0\x9f\x8c\x93" # 7 - FIRST QUARTER
"\xf0\x9f\x8c\x93" # 8 - FIRST QUARTER
"\xf0\x9f\x8c\x93" # 9 - FIRST QUARTER
"\xf0\x9f\x8c\x94" # 10 - WAXING GIBBOUS
"\xf0\x9f\x8c\x94" # 11 - WAXING GIBBOUS
"\xf0\x9f\x8c\x94" # 12 - WAXING GIBBOUS
"\xf0\x9f\x8c\x94" # 13 - WAXING GIBBOUS
"\xf0\x9f\x8c\x95" # 14 - FULL
"\xf0\x9f\x8c\x95" # 15 - FULL
"\xf0\x9f\x8c\x95" # 16 - FULL
"\xf0\x9f\x8c\x96" # 17 - WANING GIBBOUS
"\xf0\x9f\x8c\x96" # 18 - WANING GIBBOUS
"\xf0\x9f\x8c\x96" # 19 - WANING GIBBOUS
"\xf0\x9f\x8c\x96" # 20 - WANING GIBBOUS
"\xf0\x9f\x8c\x97" # 21 - LAST QUARTER
"\xf0\x9f\x8c\x97" # 22 - LAST QUARTER
"\xf0\x9f\x8c\x97" # 23 - LAST QUARTER
"\xf0\x9f\x8c\x97" # 24 - LAST QUARTER
"\xf0\x9f\x8c\x98" # 25 - WANING CRESCENT
"\xf0\x9f\x8c\x98" # 26 - WANING CRESCENT
"\xf0\x9f\x8c\x98" # 27 - WANING CRESCENT
"\xf0\x9f\x8c\x98" # 28 - WANING CRESCENT
"\xf0\x9f\x8c\x91" # 29 - NEW
)
PHASE=$(( ( ( $( date -j -f '%M:%S' 00:00 +%s ) - 1386030360 ) % 2551443 ) / 86400 ))
echo -ne "${CHARS[$PHASE]}"
if [ -z "$NONEWLINE" ]
then
echo
fi
@mdawaffe
Copy link
Author

mdawaffe commented Dec 5, 2013

You can use this in your prompt :)

In your ~/.profile:

export PS1="\$(NONEWLINE=1 ~/bin/moon.sh)  \W/\$ "

Copy link

ghost commented Jul 20, 2015

What 'date' command do you use in the script?

I have no such option keys like -j or -f in my 'date' utility.

Copy link

ghost commented Jul 20, 2015

Okay I see now this is Mac OS X 'date'. I believe that the Linux equivalent is "date --date='00:00' +%s".

@lepidopteralady
Copy link

I'm using this script to make a geeklet and the phase is off. I'm showing a last quarter and it should be waning crescent.

@bglopez
Copy link

bglopez commented Jul 1, 2020

Here's a version that checks for Darwin or Linux and runs the correct date command depending:

#!/bin/bash

# Approximates the phase of the moon using a known new-moon time and a fixed lunar phase of 2551443 seconds.
# Outputs the phase as an emoji in UTF-8

CHARS=(
        "\xf0\x9f\x8c\x91" #  0 - NEW
        "\xf0\x9f\x8c\x91" #  1 - NEW

        "\xf0\x9f\x8c\x92" #  2 - WAXING CRESCENT
        "\xf0\x9f\x8c\x92" #  3 - WAXING CRESCENT
        "\xf0\x9f\x8c\x92" #  4 - WAXING CRESCENT
        "\xf0\x9f\x8c\x92" #  5 - WAXING CRESCENT

        "\xf0\x9f\x8c\x93" #  6 - FIRST QUARTER
        "\xf0\x9f\x8c\x93" #  7 - FIRST QUARTER
        "\xf0\x9f\x8c\x93" #  8 - FIRST QUARTER
        "\xf0\x9f\x8c\x93" #  9 - FIRST QUARTER

        "\xf0\x9f\x8c\x94" # 10 - WAXING GIBBOUS
        "\xf0\x9f\x8c\x94" # 11 - WAXING GIBBOUS
        "\xf0\x9f\x8c\x94" # 12 - WAXING GIBBOUS
        "\xf0\x9f\x8c\x94" # 13 - WAXING GIBBOUS

        "\xf0\x9f\x8c\x95" # 14 - FULL
        "\xf0\x9f\x8c\x95" # 15 - FULL
        "\xf0\x9f\x8c\x95" # 16 - FULL

        "\xf0\x9f\x8c\x96" # 17 - WANING GIBBOUS
        "\xf0\x9f\x8c\x96" # 18 - WANING GIBBOUS
        "\xf0\x9f\x8c\x96" # 19 - WANING GIBBOUS
        "\xf0\x9f\x8c\x96" # 20 - WANING GIBBOUS

        "\xf0\x9f\x8c\x97" # 21 - LAST QUARTER
        "\xf0\x9f\x8c\x97" # 22 - LAST QUARTER
        "\xf0\x9f\x8c\x97" # 23 - LAST QUARTER
        "\xf0\x9f\x8c\x97" # 24 - LAST QUARTER

        "\xf0\x9f\x8c\x98" # 25 - WANING CRESCENT
        "\xf0\x9f\x8c\x98" # 26 - WANING CRESCENT
        "\xf0\x9f\x8c\x98" # 27 - WANING CRESCENT
        "\xf0\x9f\x8c\x98" # 28 - WANING CRESCENT

        "\xf0\x9f\x8c\x91" # 29 - NEW
)


if [ "$(uname)" == "Darwin" ]; then
  PHASE=$(( ( ( $( date -j -f '%M:%S' 00:00 +%s ) - 1386030360 ) % 2551443 ) / 86400 ))
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
  PHASE=$(( ( ( $( date --date='00:00' +%s ) - 1386030360 ) % 2551443 ) / 86400 ))
fi
echo -ne "${CHARS[$PHASE]}"
if [ -z "$NONEWLINE" ]
then
        echo
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment