Skip to content

Instantly share code, notes, and snippets.

@d-chambers
Created January 5, 2018 19:26
Show Gist options
  • Save d-chambers/e1469318f79a84f5cc829545a60ad218 to your computer and use it in GitHub Desktop.
Save d-chambers/e1469318f79a84f5cc829545a60ad218 to your computer and use it in GitHub Desktop.
Some strange behavior in obspy 1.1.0 UTCDateTime object
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# UTC weirdness\n",
"\n",
"obspy version: 1.1.0\n",
"\n",
"python version: 3.6.2\n",
"\n",
"OS: Ubuntu 16.04\n",
"\n",
"Installed via Anaconda"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import obspy\n",
"\n",
"TIME_NAMES = ('year', 'month', 'day', 'hour', 'minute', 'second',\n",
" 'microsecond')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def can_serialize_with_time_params(arg):\n",
" \"\"\" return True if using UTC time values (year, month, day, etc.) \n",
" provide enough information to reconstruct the object. \"\"\"\n",
" utc = obspy.UTCDateTime(arg)\n",
" time_dict = {x: getattr(utc, x) for x in TIME_NAMES}\n",
" utc2 = obspy.UTCDateTime(**time_dict)\n",
" return utc == utc2\n",
"\n",
"\n",
"def compare_utc(arg1, arg2):\n",
" utc1 = obspy.UTCDateTime(arg1)\n",
" utc2 = obspy.UTCDateTime(arg2)\n",
" \n",
" print(f'testing {arg1} against {arg2}')\n",
" print('-'*80)\n",
" print(f'UTCDateTime objects are equal: {utc1==utc2}')\n",
" print(f'strings are equal: {str(utc1) == str(utc2)}')\n",
" print(f'datetimes are equal: {utc1.datetime == utc2.datetime}')\n",
" print(f'microseconds equal: {utc1.microsecond == utc2.microsecond}')\n",
" \n",
" # test if object can be serialized with ymd yms ms params\n",
" can_serialize_arg1 = can_serialize_with_time_params(arg1)\n",
" can_serialize_arg2 = can_serialize_with_time_params(arg2)\n",
" \n",
" print(f'can serialize arg1: {can_serialize_arg1}')\n",
" print(f'can serialize arg2: {can_serialize_arg2}')\n",
" \n",
" print('\\n')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"testing 1515174511.1984465 against 1515174511.1984463\n",
"--------------------------------------------------------------------------------\n",
"UTCDateTime objects are equal: True\n",
"strings are equal: False\n",
"datetimes are equal: True\n",
"microseconds equal: True\n",
"can serialize arg1: False\n",
"can serialize arg2: True\n",
"\n",
"\n"
]
}
],
"source": [
"compare_utc(1515174511.1984465, 1515174511.1984463)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"testing 1515174511.1984465 against 1515174511.198446\n",
"--------------------------------------------------------------------------------\n",
"UTCDateTime objects are equal: False\n",
"strings are equal: False\n",
"datetimes are equal: True\n",
"microseconds equal: True\n",
"can serialize arg1: False\n",
"can serialize arg2: True\n",
"\n",
"\n"
]
}
],
"source": [
"compare_utc(1515174511.1984465, 1515174511.1984460)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"testing 1515174511.198446 against 1515174511.1984458\n",
"--------------------------------------------------------------------------------\n",
"UTCDateTime objects are equal: True\n",
"strings are equal: True\n",
"datetimes are equal: False\n",
"microseconds equal: False\n",
"can serialize arg1: True\n",
"can serialize arg2: False\n",
"\n",
"\n"
]
}
],
"source": [
"compare_utc(1515174511.1984460, 1515174511.1984458)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"testing 1515174511.1984463 against 1515174511.1984458\n",
"--------------------------------------------------------------------------------\n",
"UTCDateTime objects are equal: False\n",
"strings are equal: True\n",
"datetimes are equal: False\n",
"microseconds equal: False\n",
"can serialize arg1: True\n",
"can serialize arg2: False\n",
"\n",
"\n"
]
}
],
"source": [
"compare_utc(1515174511.1984463, 1515174511.1984459)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"testing 0.1984465 against 0.1984463\n",
"--------------------------------------------------------------------------------\n",
"UTCDateTime objects are equal: True\n",
"strings are equal: True\n",
"datetimes are equal: True\n",
"microseconds equal: True\n",
"can serialize arg1: True\n",
"can serialize arg2: True\n",
"\n",
"\n"
]
}
],
"source": [
"compare_utc(.1984465, .1984463)"
]
}
],
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment