Skip to content

Instantly share code, notes, and snippets.

@shanecandoit
Last active March 1, 2023 21:31
Show Gist options
  • Save shanecandoit/b831315ddaa8e2f44fdeb34da66f369a to your computer and use it in GitHub Desktop.
Save shanecandoit/b831315ddaa8e2f44fdeb34da66f369a to your computer and use it in GitHub Desktop.
last_24_snapshot_dates
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"import datetime"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"def last_date_of_month(year, month) -> str:\n",
" import datetime\n",
" any_day = datetime.date(year, month, 1)\n",
" next_month = any_day.replace(day=28) + datetime.timedelta(days=4) # this will never fail?\n",
" # return next_month - datetime.timedelta(days=next_month.day) # 2022-02-28\n",
" return (next_month - datetime.timedelta(days=next_month.day)).strftime('%Y%m%d') # 20220228"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(['20220131',\n",
" '20220228',\n",
" '20220331',\n",
" '20220430',\n",
" '20220531',\n",
" '20220630',\n",
" '20220731',\n",
" '20220831',\n",
" '20220930',\n",
" '20221031',\n",
" '20221130',\n",
" '20221231',\n",
" '20230131',\n",
" '20230228',\n",
" '20230331',\n",
" '20230430',\n",
" '20230531',\n",
" '20230630',\n",
" '20230731',\n",
" '20230831',\n",
" '20230930',\n",
" '20231031',\n",
" '20231130',\n",
" '20231231'],\n",
" 24)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def last_24_snapshot_dates(current_year:int=None) -> list:\n",
" snapshots_dates = []\n",
" if current_year is None:\n",
" current_year = datetime.datetime.now().year\n",
" for year in range(current_year-1, current_year+1):\n",
" for month in range(1, 13):\n",
" snapshot = last_date_of_month(year, month)\n",
" snapshots_dates.append(snapshot)\n",
" return snapshots_dates\n",
"snapshot_dates = last_24_snapshot_dates()\n",
"snapshot_dates, len(snapshot_dates)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "e218684ef22cd7c6e817cfb651c8789c0d9dfdae9bc1c9b4658f2060146e6686"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment