Created
May 12, 2022 03:22
-
-
Save JupyterJones/71ef19a03621c61bee6a97f0bb9c75a8 to your computer and use it in GitHub Desktop.
Python - Working with Dates, Epochs and Time
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# The Start (First day) of Epoch timestamps\n", | |
"### January 01 1970 08:00:00 , when the timestamp = 0" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## M2D.py\n", | |
"<b>\n", | |
"M2D.py is a module to convert a month name to number:<br />\n", | |
" Month2Num(month)<br />\n", | |
"</b>USE: month=\"March\"<br />\n", | |
" Month2Num(month)<br />\n", | |
" >>> 03<br />\n", | |
" <b>Also it prints the span in hours between two TimeStamps:<br />\n", | |
"span(timestamp1, timestamp2)<br /> \n", | |
"</b>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Help on module M2D:\n", | |
"\n", | |
"NAME\n", | |
" M2D\n", | |
"\n", | |
"DESCRIPTION\n", | |
" Month2Num(month)\n", | |
" span(timestamp1, timestamp2): This will show the span in hours between two timestamps.\n", | |
" Date = \"April 09 2020 10:00:00\"\n", | |
" DateEpoch(Date)\n", | |
"\n", | |
"FUNCTIONS\n", | |
" Date2Epoch(Date, last=1583621400)\n", | |
" \n", | |
" DateEpoch(Date)\n", | |
" \n", | |
" Month2Num(month)\n", | |
" \n", | |
" span(timestamp1, timestamp2)\n", | |
"\n", | |
"DATA\n", | |
" division = _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 1310...\n", | |
"\n", | |
"FILE\n", | |
" /home/jack/Desktop/NOTEBOOKS/M2D.py\n", | |
"\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"import M2D\n", | |
"help(M2D)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Overwriting M2D.py\n" | |
] | |
} | |
], | |
"source": [ | |
"%%writefile M2D.py\n", | |
"\"\"\"Month2Num(month)\n", | |
"span(timestamp1, timestamp2): This will show the span in hours between two timestamps.\n", | |
"Date = \"April 09 2020 10:00:00\"\n", | |
"DateEpoch(Date)\"\"\"\n", | |
"from __future__ import division\n", | |
"import time\n", | |
"def Month2Num(month):\n", | |
" number=\"\"\n", | |
" months=[\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",\\\n", | |
" \"August\",\"September\",\"October\",\"November\",\"December\"]\n", | |
" Numbers=[\"01\",\"02\",\"03\",\"04\",\"05\",\"06\",\"07\",\"08\",\"09\",\"10\",\"11\",\"12\"]\n", | |
" if month==months[0]:number=Numbers[0]\n", | |
" if month==months[1]:number=Numbers[1]\n", | |
" if month==months[2]:number=Numbers[2]\n", | |
" if month==months[3]:number=Numbers[3]\n", | |
" if month==months[4]:number=Numbers[4]\n", | |
" if month==months[5]:number=Numbers[5]\n", | |
" if month==months[6]:number=Numbers[6]\n", | |
" if month==months[7]:number=Numbers[7]\n", | |
" if month==months[8]:number=Numbers[8]\n", | |
" if month==months[9]:number=Numbers[9]\n", | |
" if month==months[10]:number=Numbers[10]\n", | |
" if month==months[11]:number=Numbers[11] \n", | |
" return number\n", | |
"\n", | |
"def span(timestamp1, timestamp2):\n", | |
" SPAN = timestamp2-timestamp1\n", | |
" res =SPAN/3600\n", | |
" result = round(res,2)\n", | |
" return result\n", | |
"\n", | |
"def DateEpoch(Date):\n", | |
" dt = time.strftime(Date)\n", | |
" Date= Date.replace(\",\",'')\n", | |
" DATE = Date.split(\" \")\n", | |
" date_ti = DATE[1]+\"/\"+Month2Num(DATE[0])+\"/\"+DATE[2]+\" \"+DATE[3]#[:-3]\n", | |
" #03-16-2020 02:48,3777\n", | |
" pattern = '%d/%m/%Y %H:%M:%S'\n", | |
" timestamp = int(time.mktime(time.strptime(date_ti, pattern)))\n", | |
" return timestamp\n", | |
"\n", | |
"def Date2Epoch(Date,last=1583621400):\n", | |
" dt = time.strftime(Date)\n", | |
" Date= Date.replace(\",\",'')\n", | |
" DATE = Date.split(\" \")\n", | |
" print(DATE[0],DATE[1],DATE[2],DATE[3])\n", | |
" date_ti = DATE[1]+\"/\"+Month2Num(DATE[0])+\"/\"+DATE[2]+\" \"+DATE[3]#[:-3]\n", | |
" #Recognized pattern\n", | |
" #16/03/2020 02:48:20\n", | |
" pattern = '%d/%m/%Y %H:%M:%S'\n", | |
" #pattern = '%m/%d/%Y %H:%M:%S'\n", | |
" epochs = int(time.mktime(time.strptime(date_ti, pattern)))\n", | |
" #print (\"dt_ti, epochs\",dt_ti, epochs)\n", | |
" Epoch = (date_ti, epochs,span(int(last),int(epochs)))\n", | |
" #EPOCHS.append(int(epochs))\n", | |
" return Epoch" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"January 01 1970 08:00:00\n", | |
"('01/01/1970 08:00:00', 0, -0.0)\n", | |
"----------- One day later (24 hours) ------------\n", | |
"January 02 1970 08:00:00\n", | |
"('02/01/1970 08:00:00', 86400, 24.0)\n", | |
"24 hours is a '86400' value as a timestamp and 24.0 hours have passed.\n" | |
] | |
} | |
], | |
"source": [ | |
"from M2D import *\n", | |
"Date = \"January 01 1970 08:00:00\"\n", | |
"print(Date2Epoch(Date,last=1))\n", | |
"print(\"----------- One day later (24 hours) ------------\")\n", | |
"Date = \"January 02 1970 08:00:00\"\n", | |
"print(Date2Epoch(Date,last=0))\n", | |
"print(\"24 hours is a '86400' value as a timestamp and 24.0 hours have passed.\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"April 09 2020 10:00:00\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"('09/04/2020 10:00:00', 1586397600, 2.0)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from M2D import Date2Epoch\n", | |
"Date = \"April 09 2020 10:00:00\"\n", | |
"Date2Epoch(Date,last=1586390400)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"April 09 2020 10:00:00\n", | |
"('09/04/2020 10:00:00', 1586397600, 2.0)\n", | |
"\n", | |
"Notice the result has three elements.\n", | |
"A, B, C = Date2Epoch(Date,last=1586390400)\n", | |
"April 09 2020 10:00:00\n", | |
"I can then use them independently.\n", | |
"\n", | |
"A: 09/04/2020 10:00:00\n", | |
"B: 1586397600\n", | |
"C: 2.0\n", | |
"The variables A B C may use any names.\n", | |
"Example: LastD, LastT, Lastspan = Date2Epoch(Date,last=1586390400)\n", | |
"April 09 2020 10:00:00\n", | |
"LastD: 09/04/2020 10:00:00\n", | |
"LastT: 1586397600\n", | |
"Lastspan: 2.0\n", | |
"---------------------------------\n", | |
"April 10 2020 10:30:00\n", | |
" The time passed hours has a 88200 value as a timestamp and 24.5 hours have passed \n", | |
" since 09/04/2020 10:00:00 and today, 10/04/2020 10:30:00\n", | |
"---------------------------------\n", | |
"10/04/2020 10:30:00\n", | |
"1586485800\n", | |
"24.5\n", | |
"0.0028488372093023254\n" | |
] | |
} | |
], | |
"source": [ | |
"from M2D import Date2Epoch\n", | |
"Date = \"April 09 2020 10:00:00\"\n", | |
"print(Date2Epoch(Date,last=1586390400))\n", | |
"print(\"\\nNotice the result has three elements.\")\n", | |
"print(\"A, B, C = Date2Epoch(Date,last=1586390400)\")\n", | |
"A, B, C = Date2Epoch(Date,last=1586390400)\n", | |
"print(\"I can then use them independently.\\n\")\n", | |
"print(\"A: \",A)\n", | |
"print(\"B: \",B)\n", | |
"print(\"C: \",C)\n", | |
"print(\"The variables A B C may use any names.\")\n", | |
"print(\"Example: LastD, LastT, Lastspan = Date2Epoch(Date,last=1586390400)\")\n", | |
"LastD, LastT, Lastspan = Date2Epoch(Date,last=1586390400)\n", | |
"print(\"LastD: \",LastD)\n", | |
"print(\"LastT: \",LastT)\n", | |
"print(\"Lastspan: \",Lastspan)\n", | |
"print(\"---------------------------------\")\n", | |
"last=1586397600\n", | |
"Date = \"April 10 2020 10:30:00\"\n", | |
"D,T,span = Date2Epoch(Date,last)\n", | |
"print(\" The time passed hours has a\",T-LastT,\"value as a timestamp and\",span,\"hours have passed \\\n", | |
"\\n since\",LastD,\"and today,\", D)\n", | |
"print(\"---------------------------------\")\n", | |
"print(D)\n", | |
"print(T)\n", | |
"print(span)\n", | |
"print(span/8600)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Writing DTE.py\n" | |
] | |
} | |
], | |
"source": [ | |
"%%writefile DTE.py\n", | |
"from M2D import *\n", | |
"import time\n", | |
"def Date2Epoch(Date,last=1583621400):\n", | |
" dt = time.strftime(Date)\n", | |
" Date= Date.replace(\",\",'')\n", | |
" DATE = Date.split(\" \")\n", | |
" print(DATE[0],DATE[1],DATE[2],DATE[3])\n", | |
" date_ti = DATE[1]+\"/\"+Month2Num(DATE[0])+\"/\"+DATE[2]+\" \"+DATE[3]#[:-3]\n", | |
" #03-16-2020 02:48,3777\n", | |
" pattern = '%d/%m/%Y %H:%M:%S'\n", | |
" epochs = int(time.mktime(time.strptime(date_ti, pattern)))\n", | |
" Epoch = (date_ti, epochs,span(int(last),int(epochs)))\n", | |
" return Epoch\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"January 01 1970 08:00:00\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"('01/01/1970 08:00:00', 0, -0.0)" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from DTE import Date2Epoch\n", | |
"Date = \"January 01 1970 08:00:00\"\n", | |
"Date2Epoch(Date,last=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"June is month 06\n", | |
"The span between 1586390400 and 1586397600 is 2.0 hours.\n", | |
"The date/time of April 09 2020 10:00:00, is the same as timestamp 1586397600 .\n" | |
] | |
} | |
], | |
"source": [ | |
"from M2D import *\n", | |
"month = \"June\"\n", | |
"print (month+\" is month \",Month2Num(month))\n", | |
"timestamp1 = 1586390400\n", | |
"timestamp2 = 1586397600\n", | |
"print(\"The span between\",timestamp1,\"and\",timestamp2,\"is\",span(timestamp1, timestamp2),\"hours.\")\n", | |
"Date = \"April 09 2020 10:00:00\"\n", | |
"print(\"The date/time of\",Date+\", is the same as timestamp\",DateEpoch(Date),\".\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1586397600" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def DateEpoch(Date):\n", | |
" dt = time.strftime(Date)\n", | |
" Date= Date.replace(\",\",'')\n", | |
" DATE = Date.split(\" \")\n", | |
" date_ti = DATE[1]+\"/\"+Month2Num(DATE[0])+\"/\"+DATE[2]+\" \"+DATE[3]#[:-3]\n", | |
" #03-16-2020 02:48,3777\n", | |
" pattern = '%d/%m/%Y %H:%M:%S'\n", | |
" timestamp = int(time.mktime(time.strptime(date_ti, pattern)))\n", | |
" return timestamp\n", | |
"Date = \"April 09 2020 10:00:00\"\n", | |
"DateEpoch(Date)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'06'" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from M2D import *\n", | |
"month = \"June\"\n", | |
"Month2Num(month)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#%%writefile DTE.py\n", | |
"from M2D import *\n", | |
"import time\n", | |
"def Date2Epoch(Date,last=1583621400):\n", | |
" dt = time.strftime(Date)\n", | |
" Date= Date.replace(\",\",'')\n", | |
" DATE = Date.split(\" \")\n", | |
" print(DATE[0],DATE[1],DATE[2],DATE[3])\n", | |
" date_ti = DATE[1]+\"/\"+Month2Num(DATE[0])+\"/\"+DATE[2]+\" \"+DATE[3]#[:-3]\n", | |
" #Recognized pattern\n", | |
" #16/03/2020 02:48:20\n", | |
" pattern = '%d/%m/%Y %H:%M:%S'\n", | |
" #pattern = '%m/%d/%Y %H:%M:%S'\n", | |
" epochs = int(time.mktime(time.strptime(date_ti, pattern)))\n", | |
" #print (\"dt_ti, epochs\",dt_ti, epochs)\n", | |
" Epoch = (date_ti, epochs,span(int(last),int(epochs)))\n", | |
" #EPOCHS.append(int(epochs))\n", | |
" return Epoch" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "base", | |
"language": "python", | |
"name": "base" | |
}, | |
"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.7" | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment