Helpful Golang Practices: Working with Time

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this tutorial will show you how to deal with dates and time stamps using go programming language there is a major difference between how date formatting and parsing is done and go in other languages today we will focus on that and explain it using a couple of examples the package that contains all time related types and functions is called time there you will find the duration type that is using go programs to represent a difference between two time instants internally it's just a counter with the highest possible resolution nanoseconds but the type defines a few methods to represent the duration in other units for example seconds and milliseconds and speaking of time instants they represented using time type of time package you can obtain one by using the date method or get the current time from the system by using the now method there are plenty of useful methods to find on a time type for example you can take an instant and add a duration or subtract one instant from another to get a duration between them a couple of methods to compare time instants for example after before in equal mind that time object carries information about the time zone so it's convenient to not care about time zones while doing comparisons you can convert a time and sentence unic seconds or a unix nanoseconds as well as a construct time object from them keep in mind though the time zone will be set as a local time zone so don't forget to change location using for example UTC method the most confusion among new gophers happens when you compare time for demanding and parsing methods and go with other methods of other languages let's focus on their time format patterns a set of cryptic patterns like PHP for the reference in PHP you have a bunch of placeholders F J ygi a which in fact should be a very simple format instead of that authors ago decided to specify the format by representing a reference time in a target format in the package layout there's a set of predefined constant layouts for most common timestamp formats as you can see they're the same instant in time which is magical and very special is being represented in formats the internal logic of time format will check how the layout looks like and will form at any time the same way let me show you an example here we have a particular time and sent initialized by a date method our task is to represent it using different formats by default go will print it as an RFC time stamp to specify the format you will need to write a layout using the magical time stamp just write that timestamp in the layout you would like to use in goal format the time accordingly this layout may look like anything but keep in mind the reference timestamp the same methodology applies to date parsing consider a case when you have a timestamp from an external system for example from a ruby application you can just specify the layout by representing the reference timestamp in the same layout as you would like a parse in this case the layout is pretty defined as a constant and package time the parsing is very strict so if there is any overflow or underflow it would fail because of this strictness you can parse a bigger string by sequential training on different formats and the only thing you would need to remember there is a reference time well that's it I hope you enjoyed the session and on the situation when time handling and go is a bit more clear for you
Info
Channel: Sphere Software
Views: 2,487
Rating: undefined out of 5
Keywords: Golang, Go, Software Development
Id: 2IAzxPAoVi0
Channel Id: undefined
Length: 4min 6sec (246 seconds)
Published: Wed Jan 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.