All right dates and times in this lecture. And before going through the
code I would first like to mention a few examples when you'd use dates and times
because for now I believe you don't have much of an idea on how you're going to
use dates and times in Python or how it does this code look like. So dates and
times you may use them you know let's say you want to create a script that
generates files automatically and you want those files to have as a name,
to have the current date, you know instead of sample dot txt you'd have like
2016 and 5 14, and so one. So that could be you know you could have a Python program
where you call the program and you pass a balance of your bank account if you
want to store a balance and then you want to create a single file every day when you
call the script, so that file would get as a name the current date
automatically without you having to type that down. So python is able to give you
the current data and time up to the microsecond accuracy. Also other examples
would be you know, you may want to know the difference between two dates.
So how do you do that? You know how to use numbers and strings and you can probably
do that with numbers, but that'll be not very efficient, so you really want to
know how to use date and time objects which are special objects in Python.
Are not just strings and numbers. So another example, you may want to plot some
time series data along data time axis. And yeah, you can generate that graph
with Python and we'll do that in the course later on, so we will be using dates
and times in many of our applications that we will be building. And we have two
modules that provide data time objects for us. The first module is called date time
and the other is time. The most important one is the date time module
and these two modules are built in in Python, so you don't have to install
them. And yeah, date time is basically used
to manipulate dates and find differences and many many other things, and also the
time module is more frequently used to control the time when the script runs.
We will go through that in just a bit. So first let's start with a date time module.
And I'd open a command line. Start a Python session, import date time.
That's what you have to do and yeah let me go through an example. Very frequently
you'll be using the now function of the date time class, just like that, so date
time is the module name and then you have a class with the same name, and that
class has a method called now. If you'd execute that, you get a date time object.
And you know, that's really a date time object. So it's a special object.
If you check class, date time. What it says is you know it has a series of numbers
where the first one is the current year, and then the current month, the current day,
and the current hour, minutes, seconds, and microseconds, and that means every time
you execute that, you know you get the current time, so it was 58 now it's 59 seconds.
Sorry minutes, this is seconds. This is depended on your computer time,
so if I change the time now. Change time settings, you change date and time.
Go 11 and press ok, if you execute that again,
now it goes up to 11, so what date time does it just grabs the time from your computer.
Yeah, let me set that back. All right.
And I seem to have to did p.m. Sorry about that.
So yeah this is good because you know now know that this is a 24-hour system.
Yeah I put it p.m. and it's 11:00.
And that should be good. Great. Now what if you want to find the
difference between the current time and a specific time? In this case you want
to pass that specific time manually, so you'd want to write that down manually.
And to do that you know you access the data time class and then you pass the numbers
of your date. Let's see it was 2016, 5. Let's say yesterday and 11, 0, 0. Now I can
either subtract the day time now from this or I can store this in a variable.
Let's say yesterday. Yesterday equals to that. And then now you can also store
this in a variable so now is this. Yesterday is that and you can see that
this has truncates the seconds and microseconds because we passed zero.
So it's doing something intelligent. Something that you should be aware of
you know is that whenever you print out now, you get the same time because
that time was stored in the moment you called date time dot now, so you get the
same value again and to make the difference, you do now minus yesterday.
And what you get is yet another date time object which is called a time delta.
And what that gives you know, it gives you an array with the day as the
first element and then you have seconds and then you have microseconds. Now if
you want, you know you can substract from that, let me store that in a variable.
Let's say delta. Delta and that. Delta.days and you get a number of days there.
If you want you can get the number
of total seconds. Sorry, you need to pass brackets in there, so that would be the
total number of seconds, the difference between those two dates and you let's now
look at an example. I would use date time in this example. So what is that is
you know we are passing this file name to this variable and we use this variable
to name the file that you are creating. So what this will do, it will create
a file with this name and that file is here. So sample1.txt and that will
be an empty file. Now how about creating a file that has as name the current date?
For that what you do you know is datetime.datetime.now. And you also need
to import date time like that, and yeah, let's go one step at a time.
Open a command line in here. Terminal plus is not working at the moment,
so it has some problems and I hope it will work in the future, but that
will not affect any of the activities we're doing here.
So is the same as keeping the window, the command line here. Terminal plus is just
a bit more convenient. And yeah, Python example.py execute.
And nothing happened. We didn't get an error. Everything is working fine. No file.
We haven't called the function in here. So create file and yeah, just call it like that.
Try that again and you get an error.
The reason for that is you know, this is not a string object, this datetime.datetime.
This is a date time object, so when you name files, you want to use a string, so let's
do just that and say strings convert date time into a string, you know because that's,
what that will do is like now is a date time object but if you say string now as argument,
that would be converted into a string
of this format, so let's execute the script again and this time you get
yet another error. This is invalid argument and the reason
to that is you know Python is trying to name a file with that has a colon in the name.
Now that is not allowed by the operating system. So you get an error.
The solution here would probably be to you know to convert the date and time
into a format like 2016 maybe 05 and 14, 11 and so on, so you use dashes there
which are acceptable and to do that you want to use formatting characters and
the way you use formatting characters is like this.
You know, you have now there, so the date time object and then you apply the string
from time method. Strftime and then you pass a string with formatting characters.
There are quite a few formatting characters actually which you can find
in this site, so for instance if you want to use, to get the year out of your date
as a string you'd want to pass a %Y with the capital Y.
Or if you want to just get the year as a zero padded decimal number like this, you'd want
to pass the %y with small y. And yeah, let me try this.
And so we would want to get the year and then we'd want to have a dash.
Like anything will be reflected in the string, so let me pass dash there and then we may
want to have the month, then the day, and then the hour, the minutes, seconds,
and microseconds. Execute that and this is a string,
so this is a string object and you got the year, the month and so on. And of course
you can't do any concept of combinations that you like zero, so for instance you
could say B, percentage B and maybe a space there, and then the year. Then maybe the day.
Execute that and you get may 2016 and 14 for the day, so you can literally make
any kinds of combinations that you like with this format. And yeah,
let's apply this in our example, so we remove string and what we do is you know file
name dot strftime, and then inside the strings here you'd year, month
and day, hour, and minutes that that would be enough, so let's keep it like that.
Save that and go here to execute it. Yeap, so we got a file with this name as you see.
Great, so that is working and if you want to add something there, you know if you want to
add an accession txt, you'd want to say plus dot txt and execute again and you
get a file with a txt extension as you see here. And lastly about the date time
module you know we were able to substract between two dates, but how about adding a
period of time to a date and so that we get another date? Well let's go to
our Python shell, so we have no now let's add two days in now. What you do is you know
let's say after would be equal to now plus datetime.timedelta, and then
you pas days equals to 2. Execute.
And here is after, so it's two days after 16. You can also pass seconds in there
as a parameter. Say like that, after and this is what you get, so python will
actually get the seconds out of this. Will add to the seconds and then it will
convert the remain to minutes and hours if necessary and so on and it will give
you a correct date that is exactly this period of time after this and now
date time, so that's about date time. Very quickly now we also have a module called time.
Now this comes very handy when you want to delay some actions and
operations in your Python script. For instance let me create an empty list
and what I want to do with this list I want to add five items in there and each of
these items will be the date and time and there will be a difference of two
seconds between this date times items and so what you want to is create a loop.
Let's say for i in range five one two three four lst dot
append datetime.datetime.now. So what I'm appending is the first iteration
of a loop, I am appending the current date and
time and then I want to wait for two seconds or just one to keep it quick,
so the number you pass there is seconds and if I execute that now, what it will
happen is you know the script will record the first date time in the list
and that will wait for one second, and then it will record a second time.
And the second date time and so it will wait for one second and so on and lst will be
a list or let me print it out for i in lst print i and yeah, this is what we got.
We got five dates and times and as you can see they have one second of
difference so the second six, seven, eight, nine, ten and you can also see the
microseconds here. This reveals the accuracy of the time
module so your computer is recording the microseconds in this execution of the
loop and then the time module does the loop to wait for one second,
but that one second of the time module is not exactly you know a million
microseconds, so that's a certain flow in accuracy which is not a problem practically.
So yeah, this is the time module and we're done with this lecture.
I hope you learnt a lot of things from this. I will be using time and date time a lot
in the course, so yeah let's move on.