hi there serdar yagulp here for infoworld in this episode of smart go we're going to take a closer
look at the way go handles date and time values by way of the time module the time module and go
gives us access to objects and methods that let us get the current time create time and date values
from scratch and perform formatting on existing time values it also gives us tools for working
with time zones easily one of the least favorite aspects of dealing with date and time values if
you're a programmer but the time module helps handle those things in a straightforward way
let's start with a simple example getting the current time and printing it to the console here
first we import the time module and then we use the time.now method to get the current time and
date from the system and store it in the variable t now if we print this to the console like this
we're going to get a very complex and detailed output this includes the date the time the time
zone and some additional information about the current processes monotonic clock that's something
that's used to measure elapsed time between events inside a process instead of the absolute time of
the system at large that's something worth going into detail about in its own video though so for
now we'll leave it alone now if we just want to print a few details about the current date
and time instead of the whole thing we could use the format method of the time object to do this
with format we pass along a special formatting code which looks a little bit like a date and time
but is actually a way to indicate how to format specific components of the date and time
for instance in in the format string we have this code 2006 that's actually an indication we want
to insert a four digit year so when we run this code we will see only a few parts of the date and
time the ones that we most need now let's say i want to create a time and date value from scratch
not just by sampling the system time and to do this we can use the date method to create
a time.time object by supplying it with the needed details about our time and date
here in this example i've got a struct named movie which has two fields one is the name of
a movie a string and the other is its release date expressed as a time.time object now when
i create an instance of this struct and i want to populate the release field i make a call to
time.date and i pass along in this order the year the month the day and then the hour minute
second and millisecond of the time in question and then finally a time zone reference for now
i'm going to use utc as the time zone but we'll go into detail later about how we can alter that
now you notice for this structure that i am using a format function and in there it
converts the time object to a simple date when we print out the whole movie struct as a string
if we run this you can see that's what happens we don't need to know the the actual time the movie
was released just the day and date now if we want to provide adjustments for time zones we can use
the time dot load location method to create a time adjustment object for specific time zone
and then we pass that to the dot in method on a time object to adjust it here in this reworked
version of the earlier example i showed you i'm getting the current time which is pre-adjusted
by the way for utc minus five by way of my system clock that's because i'm on the east coast of the
united states and if i want to render this as just utc i get a utc adjustment object i'll call it tz
and i pass that to n and i can do the same thing if i want to get the time from the west coast
so when i run this program i will get three time outputs one is for my original time zone
one is for utc and one is for my friends in california
so to sum up time objects and go let you sample the current time or create a new time object from
scratch that lets you render time to strings using the format method and they let you apply time zone
adjustments to existing times and in future videos i'm going to show you how you could perform more
sophisticated operations with times and dates for instance performing math on times and dates
or using time intervals in your programs to do something on a regular basis or to
determine how long specific operations take within a program that's it for this video if you liked it
please leave a comment below and don't forget to subscribe to the tech talk channel on youtube and
for more smart go and more dev with serdar be sure to follow me on facebook youtube and infowold.com
you