HSG-MCS-HS21_Julia/JuliaTutorial-master/Tutorial_08_DaysAndDates.ipynb
2021-11-15 21:14:51 +01:00

338 lines
7.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Days and Dates\n",
"\n",
"This notebook (a) introduces the Date type; (b) discusses how to convert from one date format (for instance, Excel and Matlab) to Julia dates; (c) and how to do date arithmetics."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Packages and Extra Functions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"printyellow (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Printf, Dates\n",
"\n",
"include(\"jlFiles/printmat.jl\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Building a Calendar"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dates and day of the week\n",
"2014-01-31 5 \n",
"2014-02-28 5 \n",
"2014-03-31 1 \n",
"2014-04-30 3 \n",
"2014-05-31 6 \n",
"2014-06-30 1 \n",
"2014-07-31 4 \n",
"2014-08-31 7 \n",
"2014-09-30 2 \n",
"2014-10-31 5 \n",
"2014-11-30 7 \n",
"2014-12-31 3 \n",
"\n"
]
}
],
"source": [
"dNb = Date(2014,1,31):Month(1):Date(2014,12,31) #build a monthly calendar\n",
"\n",
"println(\"Dates and day of the week\")\n",
"printmat([dNb Dates.dayofweek.(dNb)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Converting from Other Date Formats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Converting from yyyymmdd\n",
"\n",
"Background: financial data is often downloaded as CSV files (eg. from finance.yahoo), where the date may look like 20160331. The next cell shows a simple way to create a Julia Date."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2016-03-31\n",
"2016-04-01\n",
"\n"
]
}
],
"source": [
"csvDate = [20160331;20160401] #two dates\n",
"\n",
"jlDate = Date.(string.(csvDate),\"yyyymmdd\") #convert to string and then Julia Date\n",
"\n",
"printmat(jlDate)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Converting from DateTime (Excel) to Date\n",
"\n",
"Background: importing xls sheets with ExcelReaders gives DateTime, even if the sheets only contain dates (daily data, say)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"DateTime and then converted to Date:\n",
"2016-03-31T00:00:00\n",
"2016-04-01T00:00:00\n",
"\n",
"2016-03-31\n",
"2016-04-01\n",
"\n"
]
}
],
"source": [
"xlsDate = [DateTime(2016,3,31);DateTime(2016,4,1)] #to be converted\n",
"\n",
"jlDate = Date.(xlsDate)\n",
"\n",
"println(\"\\nDateTime and then converted to Date:\")\n",
"printmat(xlsDate)\n",
"printmat(jlDate)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Converting from Matlab's datenum to Date\n",
"\n",
"Background: in Matlab `datenum(2016,3,31)` gives 736420.0. In contrast, in Julia (which follows the ISO 8601 standard), `Dates.value(Date(2016,3,31))` gives 736054, which is 366 less (and it is an integer). A conversion is therefore required."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"matlab datenum and correct Julia Date:\n",
" 736420.000 2016-03-31\n",
" 736421.000 2016-04-01\n",
"\n"
]
}
],
"source": [
"dNml = [736420.0;736421.0] #to be converted, 2016-03-31;2016-04-01\n",
"\n",
"jlDate = round.(Int,dNml) .- 366\n",
"jlDate = Date.(rata2datetime.(jlDate))\n",
"\n",
"println(\"\\nmatlab datenum and correct Julia Date:\")\n",
"printmat([dNml jlDate],width=12)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Time Arithmetics\n",
"\n",
"You can add and subtract Dates from each other."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"difference between two dates: 30 days\n",
"as a fraction of the year: 0.082\n"
]
}
],
"source": [
"d1 = Date(2016,3,31)\n",
"d2 = Date(2016,4,30)\n",
"\n",
"dif = d2 - d1 #count the number of days between d2 and d1\n",
"difRel = Dates.value(dif)/daysinyear(d1) #Dates.value() is the datenumber, needs prefix Dates\n",
"\n",
"println(\"difference between two dates: \",dif)\n",
"printlnPs(\"as a fraction of the year: \",difRel)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"d1 and one month later: 2016-03-31 2016-04-30\n"
]
}
],
"source": [
"d3 = d1 + Month(1) #one month after d1\n",
"\n",
"println(\"d1 and one month later: \",d1,\" \",d3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Looking up Day of the Week and More"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"day of the week of date: 2016-03-31 4\n",
"day of the year of date: 2016-03-31 91\n",
"\n",
"Splitting up a date: 2016 3 31\n"
]
}
],
"source": [
"println(\"day of the week of date: \",d1,\" \",dayofweek(d1))\n",
"println(\"day of the year of date: \",d1,\" \",dayofyear(d1))\n",
"\n",
"(y,m,d)= yearmonthday(d1) #splitting up a date\n",
"println(\"\\nSplitting up a date: \",y,\" \",m,\" \",d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Printing a Date\n",
"\n",
"with your own formatting (see the manual for many other formatting options)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"31 Mar 2016\n",
"31-03-2016\n"
]
}
],
"source": [
"println(Dates.format(d1,\"d u yyyy\")) #needs prefix Dates\n",
"println(Dates.format(d1,\"dd-mm-yyyy\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Julia 1.6.2",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}