Last active
October 4, 2017 14:50
-
-
Save estevesd/07b2776ba39df092906235135df46027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Getting floating date, calendar start and end date for day, week, month and year | |
# main goal is to get dates in epoch format | |
# the output in human readable format are mainly for debugging purpose | |
# NOW | |
function now_epoch () { | |
date +%s | |
} | |
function now () { | |
date | |
} | |
echo "Now : $(now)" | |
echo "" | |
# DAY | |
function day_epoch () { | |
# $1 : day [integer], 0 = today(default), -1 = yesterday, 1 = tomorrow, etc... | |
DAY=$1 | |
[ -z "$DAY" ] && DAY=0 | |
date +%s -d "$DAY day" | |
} | |
function day_start_epoch () { | |
# $1 : day [integer], 0 = today(default), -1 = yesterday, 1 = tomorrow, etc... | |
DAY=$1 | |
[ -z "$DAY" ] && DAY=0 | |
date +%s -d "$DAY day 0:00" | |
} | |
function day_end_epoch () { | |
# $1 : day [integer], 0 = today(default), -1 = yesterday, 1 = tomorrow, etc... | |
DAY=$1 | |
[ -z "$DAY" ] && DAY=0 | |
date +%s -d "$DAY day 23:59:59" | |
} | |
function day () { | |
# $1 : day [integer], 0 = today(default), -1 = yesterday, 1 = tomorrow, etc... | |
date -d "@$(day_epoch $1)" | |
} | |
function day_start () { | |
# $1 : day [integer], 0 = today(default), -1 = yesterday, 1 = tomorrow, etc... | |
date -d "@$(day_start_epoch $1)" | |
} | |
function day_end () { | |
# $1 : day [integer], 0 = today(default), -1 = yesterday, 1 = tomorrow, etc... | |
date -d "@$(day_end_epoch $1)" | |
} | |
echo "Yesterday : $(day -1)" | |
echo "Yesterday start : $(day_start -1)" | |
echo "Yesterday end : $(day_end -1)" | |
echo "Today : $(day 0)" | |
echo "Today start : $(day_start)" | |
echo "Today end : $(day_end)" | |
echo "Tomorrow : $(day 1)" | |
echo "Tomorrow start : $(day_start 1)" | |
echo "Tomorrow end : $(day_end 1)" | |
echo "" | |
# WEEK | |
function week_epoch () { | |
# $1 : week [integer], 0 = this_week(default), -1 = last_week, 1 = next_week, etc... | |
WEEK=$1 | |
[ -z "$WEEK" ] && WEEK=0 | |
date +%s -d "$WEEK week" | |
} | |
function week_start_epoch () { | |
# $1 : week [integer], 0 = this_week(default), -1 = last_week, 1 = next_week, etc... | |
# $2 : week start day, 0 = sunday, 1 = monday(default) | |
WEEK=$1 | |
WEEK_START_DAY=$2 | |
[ -z "$WEEK" ] && WEEK=0 | |
[ -z "$WEEK_START_DAY" ] && WEEK_START_DAY=1 | |
date +%s -d "$WEEK week -$(($(date +%u)-WEEK_START_DAY)) days 0:00" | |
} | |
function week_end_epoch () { | |
# $1 : week [integer], 0 = this_week(default), -1 = last_week, 1 = next_week, etc... | |
# $2 : week start day, 0 = sunday, 1 = monday(default) | |
WEEK=$1 | |
WEEK_START_DAY=$2 | |
[ -z "$WEEK" ] && WEEK=0 | |
[ -z "$WEEK_START_DAY" ] && WEEK_START_DAY=1 | |
WEEK=$((WEEK+1)) | |
date +%s -d "$WEEK week -$((7-WEEK_START_DAY-$(date +%u))) days 23:59:59" | |
} | |
function week () { | |
# $1 : week [integer], 0 = this_week(default), -1 = last_week, 1 = next_week, etc... | |
date -d "@$(week_epoch $1)" | |
} | |
function week_start () { | |
# $1 : week [integer], 0 = this_week(default), -1 = last_week, 1 = next_week, etc... | |
# $2 : week start day, 0 = sunday, 1 = monday(default) | |
date -d "@$(week_start_epoch $1 $2)" | |
} | |
function week_end () { | |
# $1 : week [integer], 0 = this_week(default), -1 = last_week, 1 = next_week, etc... | |
# $2 : week start day, 0 = sunday, 1 = monday(default) | |
date -d "@$(week_end_epoch $1 $2)" | |
} | |
echo "Week is starting on Monday" | |
echo "Last week : $(week -1)" | |
echo "Last week start : $(week_start -1)" | |
echo "Last week end : $(week_end -1)" | |
echo "This week : $(week 0)" | |
echo "This week start : $(week_start 0)" | |
echo "This week end : $(week_end 0)" | |
echo "Next week : $(week 1)" | |
echo "Next week start : $(week_start 1)" | |
echo "Next week end : $(week_end 1)" | |
echo "Week is starting on Sunday" | |
echo "Last week : $(week -1)" | |
echo "Last week start : $(week_start -1 0)" | |
echo "Last week end : $(week_end -1 0)" | |
echo "This week : $(week 0)" | |
echo "This week start : $(week_start 0 0)" | |
echo "This week end : $(week_end 0 0)" | |
echo "Next week : $(week 1)" | |
echo "Next week start : $(week_start 1 0)" | |
echo "Next week end : $(week_end 1 0)" | |
echo "" | |
# MONTH | |
function month_epoch (){ | |
# $1 : month [integer], 0 = this_month(default), -1 = last_month, 1 = next_month, etc... | |
MONTH=$1 | |
[ -z "$MONTH" ] && MONTH=0 | |
date +%s -d "$MONTH month" | |
} | |
function month_start_epoch (){ | |
# $1 : month [integer], 0 = this_month(default), -1 = last_month, 1 = next_month, etc... | |
MONTH=$1 | |
[ -z "$MONTH" ] && MONTH=0 | |
date +%s -d "$MONTH month -$(($(date +%d)-1)) days 0:00" | |
} | |
function month_end_epoch (){ | |
# $1 : month [integer], 0 = this_month(default), -1 = last_month, 1 = next_month, etc... | |
MONTH=$1 | |
[ -z "$MONTH" ] && MONTH=0 | |
MONTH=$((MONTH+1)) | |
date +%s -d "$MONTH month -$(date +%d) days 23:59:59" | |
} | |
function month () { | |
# $1 : month [integer], 0 = this_month(default), -1 = last_month, 1 = next_month, etc... | |
date -d "@$(month_epoch $1)" | |
} | |
function month_start () { | |
# $1 : month [integer], 0 = this_month(default), -1 = last_month, 1 = next_month, etc... | |
date -d "@$(month_start_epoch $1)" | |
} | |
function month_end () { | |
# $1 : month [integer], 0 = this_month(default), -1 = last_month, 1 = next_month, etc... | |
date -d "@$(month_end_epoch $1)" | |
} | |
echo "Last month : $(month -1)" | |
echo "Last month start : $(month_start -1)" | |
echo "Last month end : $(month_end -1)" | |
echo "This month : $(month)" | |
echo "This month start : $(month_start)" | |
echo "This month end : $(month_end)" | |
echo "Next month : $(month 1)" | |
echo "Next month start : $(month_start 1)" | |
echo "Next month end : $(month_end 1)" | |
echo "" | |
# YEAR | |
function year_epoch () { | |
# $1 : year [integer], 0 = this_year(default), -1 = last_year, 1 = next_year, etc... | |
YEAR=$1 | |
[ -z "$YEAR" ] && YEAR=0 | |
date +%s -d "$YEAR year" | |
} | |
function year_start_epoch () { | |
# $1 : year [integer], 0 = this_year(default), -1 = last_year, 1 = next_year, etc... | |
YEAR=$1 | |
[ -z "$YEAR" ] && YEAR=0 | |
date +%s -d "$YEAR year Jan 1 00:00:00" | |
} | |
function year_end_epoch () { | |
# $1 : year [integer], 0 = this_year(default), -1 = last_year, 1 = next_year, etc... | |
YEAR=$1 | |
[ -z "$YEAR" ] && YEAR=0 | |
date +%s -d "$YEAR year Dec 31 23:59:59" | |
} | |
function year () { | |
# $1 : year [integer], 0 = this_year(default), -1 = last_year, 1 = next_year, etc... | |
date -d "@$(year_epoch $1)" | |
} | |
function year_start () { | |
# $1 : year [integer], 0 = this_year(default), -1 = last_year, 1 = next_year, etc... | |
date -d "@$(year_start_epoch $1)" | |
} | |
function year_end () { | |
# $1 : year [integer], 0 = this_year(default), -1 = last_year, 1 = next_year, etc... | |
date -d "@$(year_end_epoch $1)" | |
} | |
echo "Last year : $(year -1)" | |
echo "Last year start : $(year_start -1)" | |
echo "Last year end : $(year_end -1)" | |
echo "This year : $(year)" | |
echo "This year start : $(year_start)" | |
echo "This year end : $(year_end)" | |
echo "Next year : $(year 1)" | |
echo "Next year start : $(year_start 1)" | |
echo "Next year end : $(year_end 1)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment