Time · Gopher Coding (original) (raw)

Tag: Time

Get Unix Time in Go (Time Since Epoch)Get Unix Time in Go (Time Since Epoch)Unix time is the number of seconds since ‘Epoch’ (00:00:00 UTC on 1 January 1970). It’s a widely adopted way of representing time in a simple, uniform, manner - as it’s an integer and not a string. Because of it’s simplicity, it also used it many programming languages. In Go, you can easily access it from the time package by getting the current time, then calling the Unix() function.

#time#unix#current#format#seconds#now#january#epoch

Check If a Date is in The Future?Check If a Date is in The Future?Whether you’re setting up a calendar event or issuing a JWT token, knowing how to manipulate and validate time can save you from many potential headaches. In Go, the built-in time package provides a comprehensive set of tools to work with dates and times. View Time Docs How to If you check the date in question, is after the current time then this basically does a ‘am I in the future’ check.

#date#future#compare#time#now#calendar

How to Ignore Weekends in GoGiven a date, we’re looking at how you can add a duration to it, like a month or two, but ignoring weekends. This is usually most used for businesses focused on working-days - but it could easily be adapted to do the opposite, only weekends. It’s built around the standard lib time package so no extra packages needed. We’ve got some example code below that makes a date and adds on 14 days, while skipping the weekend days out. We are doing this manually just checking each day on the Weekday() function and comparing it to time.Saturday or time.Sunday.

#weekend#ignore#skip#business#holiday#time#weekday#duration#month

Add Date Ordinals (UK Date Format) in GoAdd Date Ordinals (UK Date Format) in GoGo, despite its robust standard library, does not support ordinal dates out of the box like ‘th’ and ‘st’. This makes producing dates in the format “1st January 2000” much harder - which we use a lot here in the UK. In this post we’ll aim to show how you can make this easier. For reference, these are the options Go uses when chosing a new format: 1 Mon Jan 2 15:04:05 MST 2006 Our example below implements this for ourselves, as we create formatDateWithOrdinal() to print a given time in this format. This uses all three of the .Day() .Month() and .Year() functions within the time package, but passes the day through our extra function to add the ordinal.

#date#ordinal#format#uk#time#th

Sleeping in GolangSleeping in GolangSleeping in Go and how to pause execution and sleep for a number of seconds in Go (golang). We can use the time package from the standard library to sleep for a duration of time. You can use any duration of time, providing you use the constants provided. In our example below we sleep for two seconds, and to illustrate the point, we print out the time before and after we do this.

#sleep#golang#pause#execution#wait#goroutine#package#unix#time