aboutsummaryrefslogtreecommitdiff
path: root/quarter-timestamps
blob: 8f1bab50c35c0912df6867ad0bb28cb8c7f1101d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
#
# Determine the quarter's timestamps
#

# Parameters
BASENAME="`basename $0`"
DIRNAME="`dirname $0`"
QUARTER="$1"
YEAR="`echo $QUARTER | cut -d . -f 1`"
QUARTILE="`echo $QUARTER | cut -d . -f 2`"

# Syntax check
if [ -z "$QUARTILE" ]; then
  echo "usage: $BASENAME <quarter>"
  echo "quarter format: YYYY.QQ"
  echo "example: 2024.Q3"
  exit 1
fi

# Lookup table
case $QUARTILE in
  Q1)
    BEGIN="01-01"
    END="03-31"
    ;;

  Q2)
    BEGIN="04-01"
    END="06-30"
    ;;

  Q3)
    BEGIN="07-01"
    END="09-30"
    ;;

  Q4)
    BEGIN="10-01"
    END="12-31"
    ;;
esac

# Determine the UNIX timestamp for the beginning and end of the quarter
QUARTER_BEGIN="`date -d "${YEAR}-${BEGIN} 00:00:00 UTC" +%s`"
QUARTER_END="`date -d "${YEAR}-${END} 00:00:00 UTC" +%s`"

# Output
echo "Quarter begins at UNIX timestamp $QUARTER_BEGIN ($YEAR-$BEGIN)"
echo "Quarter ends   et UNIX timestamp $QUARTER_END ($YEAR-$END)"