{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "⊕ [dateparser – python parser for human readable dates — DateParser 0.7.0 documentation](https://dateparser.readthedocs.io/en/latest/)\n",
    "\n",
    "⊕ [scrapinghub/dateparser: python parser for human readable dates](https://github.com/scrapinghub/dateparser)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:52:38.406263Z",
     "start_time": "2019-09-14T07:52:38.089780Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2014, 10, 21, 0, 0)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import dateparser\n",
    "dateparser.parse(u'Martes 21 de Octubre de 2014')  # Spanish (Tuesday 21 October 2014)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:52:50.804070Z",
     "start_time": "2019-09-14T07:52:50.733175Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2014, 12, 11, 9, 0)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> dateparser.parse(u'Le 11 Décembre 2014 à 09:00')  # French (11 December 2014 at 09:00)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:53:05.795792Z",
     "start_time": "2019-09-14T07:53:05.699176Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2015, 1, 13, 13, 34)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> dateparser.parse(u'13 января 2015 г. в 13:34')  # Russian (13 January 2015 at 13:34)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:53:27.465071Z",
     "start_time": "2019-09-14T07:53:27.447805Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2015, 8, 15, 13, 8)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dateparser.parse('2015, Ago 15, 1:08 pm', languages=['pt', 'es'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:53:41.148099Z",
     "start_time": "2019-09-14T07:53:41.131173Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2010, 12, 22, 0, 0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dateparser.parse(u'22 Décembre 2010', date_formats=['%d %B %Y'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:55:05.957147Z",
     "start_time": "2019-09-14T07:55:05.923145Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 9, 14, 15, 53, 5, 954067, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from dateparser import parse\n",
    "parse('2 minutes ago', settings={'RETURN_AS_TIMEZONE_AWARE': True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:55:21.058593Z",
     "start_time": "2019-09-14T07:55:21.033545Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 9, 14, 7, 55, 21, 49283, tzinfo=<StaticTzInfo 'UTC'>)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse('now EST', settings={'TO_TIMEZONE': 'UTC'})  # relative dates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:55:53.367823Z",
     "start_time": "2019-09-14T07:55:53.349397Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 9, 14, 7, 51, 53, 357948)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse('4 minutes ago', settings={'TIMEZONE': 'UTC'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:56:22.004661Z",
     "start_time": "2019-09-14T07:56:21.990776Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 9, 14, 15, 52, 21, 996606)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse('4 minutes ago')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:56:39.431965Z",
     "start_time": "2019-09-14T07:56:39.417703Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 3, 14, 0, 0)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse(u'March')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:00:48.327025Z",
     "start_time": "2019-09-14T08:00:48.299758Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-03-14 00:00:00 2019-03-14 00:00:00 2019-09-11 00:00:00\n"
     ]
    }
   ],
   "source": [
    "print(parse(u'3月', languages=['zh', 'ja']),\n",
    "      parse(u'三月', languages=['zh', 'ja']),\n",
    "      parse(u'星期三', languages=['zh', 'ja']),\n",
    "     )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:58:50.872313Z",
     "start_time": "2019-09-14T07:58:50.858280Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 3, 14, 0, 0)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse(u'March', settings={'PREFER_DATES_FROM': 'future'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T07:59:19.349901Z",
     "start_time": "2019-09-14T07:59:19.142457Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('in March 3rd, 2004 and', datetime.datetime(2004, 3, 3, 0, 0)),\n",
       " ('on May 6th 2004', datetime.datetime(2004, 5, 6, 0, 0))]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> from dateparser.search import search_dates\n",
    ">>> search_dates(\"The client arrived to the office for the first time in March 3rd, 2004 and got serviced, after a couple of months, on May 6th 2004, the customer returned indicating a defect on the part\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:01:51.766674Z",
     "start_time": "2019-09-14T08:01:51.753338Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('三月', datetime.datetime(2019, 3, 14, 0, 0))]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_dates('三月开始去上学', languages=['zh'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:02:14.854368Z",
     "start_time": "2019-09-14T08:02:14.837999Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('分钟', datetime.datetime(2019, 9, 14, 0, 0))]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_dates('十分钟之后开始去上学', languages=['zh'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:02:58.718657Z",
     "start_time": "2019-09-14T08:02:58.705883Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('10分', datetime.datetime(2019, 9, 14, 15, 52, 58, 713384))]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_dates('私は10分後に学校に行きました。', languages=['ja'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:05:30.697452Z",
     "start_time": "2019-09-14T08:05:30.682906Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('2008年12月', datetime.datetime(10, 9, 14, 16, 5, 30, 691914))]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_dates('2008年12月に上海に行きたいです。', languages=['ja'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:06:08.546596Z",
     "start_time": "2019-09-14T08:06:08.516986Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('في ديسمبر 2008', datetime.datetime(2008, 12, 14, 0, 0))]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_dates('أريد أن أذهب إلى شنغهاي في ديسمبر 2008.', languages=['ar'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:07:36.992910Z",
     "start_time": "2019-09-14T08:07:36.970247Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('می', datetime.datetime(2019, 5, 14, 0, 0)),\n",
       " ('در دسامبر 2008', datetime.datetime(2008, 12, 14, 0, 0))]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_dates('من می خواهم در دسامبر 2008 به شانگهای بروم.', languages=['fa'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using DateDataParser\n",
    "dateparser.parse() uses a default parser which tries to detect language every time it is called and is not the most efficient way while parsing dates from the same source.\n",
    "\n",
    "DateDataParser provides an alternate and efficient way to control language detection behavior.\n",
    "\n",
    "The instance of DateDataParser reduces the number of applicable languages, until only one or no language is left. It assumes the previously detected language for all the subsequent dates supplied.\n",
    "\n",
    "This class wraps around the core dateparser functionality, and by default assumes that all of the dates fed to it are in the same language."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:11:09.488289Z",
     "start_time": "2019-09-14T08:11:09.473520Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'date_obj': datetime.datetime(2015, 3, 14, 0, 0),\n",
       " 'period': 'month',\n",
       " 'locale': 'en'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from dateparser.date import DateDataParser\n",
    "DateDataParser().get_date_data(u'March 2015')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:11:21.949483Z",
     "start_time": "2019-09-14T08:11:21.933475Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'date_obj': datetime.datetime(2000, 3, 23, 13, 21, tzinfo=<StaticTzInfo 'CET'>),\n",
       " 'period': 'day',\n",
       " 'locale': 'en'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DateDataParser().get_date_data(u'23 March 2000, 1:21 PM CET')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-14T08:16:47.293107Z",
     "start_time": "2019-09-14T08:16:47.256988Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'date_obj': datetime.datetime(2014, 1, 24, 12, 49),\n",
       " 'period': 'day',\n",
       " 'locale': 'nl'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ddp = DateDataParser(languages=['de', 'nl'])\n",
    "ddp.get_date_data(u'vr jan 24, 2014 12:49')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T08:02:58.749150Z",
     "start_time": "2019-09-16T08:02:58.729389Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'date_obj': datetime.datetime(2014, 10, 18, 22, 56),\n",
       " 'period': 'day',\n",
       " 'locale': 'de'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r=ddp.get_date_data(u'18.10.14 um 22:56 Uhr')\n",
    "print(type(r))\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T08:32:28.730611Z",
     "start_time": "2019-09-16T08:32:28.714157Z"
    }
   },
   "outputs": [],
   "source": [
    "def xfun(**kwargs):\n",
    "    pass\n",
    "\n",
    "xfun(xεy=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T15:39:21.338478Z",
     "start_time": "2019-09-18T15:39:21.282000Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-1, 86399, 999999)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> from datetime import timedelta\n",
    ">>> d = timedelta(microseconds=-1)\n",
    ">>> (d.days, d.seconds, d.microseconds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T15:40:00.201326Z",
     "start_time": "2019-09-18T15:40:00.190333Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(days=-1, seconds=68400)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> timedelta(hours=-5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T15:40:07.456067Z",
     "start_time": "2019-09-18T15:40:07.450601Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1 day, 19:00:00\n"
     ]
    }
   ],
   "source": [
    ">>> print(_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T15:48:24.595893Z",
     "start_time": "2019-09-18T15:48:24.588554Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "31536000.0"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> from datetime import timedelta\n",
    ">>> year = timedelta(days=365)\n",
    ">>> another_year = timedelta(weeks=40, days=84, hours=23,\n",
    "...                          minutes=50, seconds=600)  # adds up to 365 days\n",
    ">>> year.total_seconds()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16.0,
    "lenType": 16.0,
    "lenVar": 40.0
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
