Categorygithub.com/maniartech/gotime
repositorypackage
1.1.0
Repository: https://github.com/maniartech/gotime.git
Documentation: pkg.go.dev

# README

GoTime 🕧🕐🕜🕑🕝🕒

⚠️ Package name changed to gotime on community request.

Designed on the top of Golang's built-in time package, instead of reinventing the wheel gotime uses internal time package to provide additional day-to-day functionalities that are practical for real-world applications.

Before we understand why there is a need for yet another time paackage, let's see some of the code that shocases what this library is capable of doing.

import "github.com/maniartech/gotime"

// // Format the date using human-friendly case-insensitive specifiers like yyyy-mm-dd
dt = time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC)
s := gotime.Format(dt, "dt mmmm, yyyy")
fmt.Println(s) // 1st January, 2012

// Convert date string to different format
s, err := gotime.Convert("2012-01-01", "yyyy-mm-dd", "wwww, dt mmmm, yyyy")
fmt.Println(s) // Sunday, 1st January, 2012

// Use time ago to get relative time
s, err := gotime.TimeAgo(time.Now().Add(-5 * time.Minute))
fmt.Println(s) // 5 minutes ago

// Parse a date string using easy to remember specifiers
t, err := gotime.Parse("2012-01-01", "yyyy-mm-dd")
fmt.Println(t) // 2012-01-01 00:00:00 +0000 UTC

tz := time.FixedZone("IST", 5.5*60*60)
t, err := gotime.ParseInLocation("01/01/2020", "dd/mm/yyyy", tz)
fmt.Println(t) // 2020-01-01 00:00:00 +0530 IST

// Some handy date finders and other utility functions
gotime.Yesterday()  // Returns yesterday's date
gotime.NextWeek()   // Returns data exactly one week from now

// Other utility functions
d1 := gotime.Date(10) // Returns date of 10 days from now
d2 := gotime.Date(-2) // Returns date of 2 days ago
d3 := gotime.Date(10, d1) // Returns date of 10 days from t1
gotime.Earliest(d1, d2, d3) // Returns the earliest date from the given list of dates
gotime.Latest(d1, d2, d3) // Returns the latest date from the given list of dates
gotime.IsBetween(d1, d2, d3) // Returns true if d1 is between d2 and d3

gotime.IsLeapYear(2020) // Returns true if the given year is a leap year
weekDdays := bool{true, true, true, true, true, false, false}
gotime.WorkDay(time.Now(), 15, weekDays) // Returns the date after the specified
                                           // number of workdays, considering
                                           // holidays and weekends

// So on...

Why GoTime?

✨ Key Features (Designed for Practicality)

It provides features are practical and useful in real-world applications. These features are either missing or not easy to use in the standard time package.

  • Easy Formatting: Format dates using straightforward, human-readable and intuitive specifiers like dd/mm/yyyy.
  • Human-Friendly Parsing: Effortlessly parse dates using intuitive date specifiers
  • Format Conversion: Seamlessly convert dates between formats, e.g., from dd/mm/yyyy to yyyy-mm-dd.
  • Relative Time Processing: Translate datetime into relative terms like a few minutes ago, yesterday, 5 days ago, or 3 years from now.
  • Finder Functions: Utilize functions like Yesterday(), Tomorrow(), SoD() (Start of Day), and EoD() (End of Day) etc.
  • Utility Functions: Access a suite of utility functions including Latest(), Earliest(), IsBetween(), TruncateDate(), and more.

✨ Developer Friendly

It provides a comprehensive range of specifiers for all your date and time formatting needs, making it an indispensable tool for Go developers.

  • 💯% test coverage 👌🏼
  • TinyGo compatible 👌🏼
  • No external dependencies 👌🏼
  • Fully utilises the standard time package and does not reinvent the wheel 👌🏼
  • Simple, intuitive and hackable API 👌🏼
  • Fully documented 👌🏼
  • Performance focused 👌🏼

✨ Ideal Use Cases

  • Developers needing formatting and parsing dates using human-friendly specifiers like yyyy-mm-dd.
  • Applications requiring conversion between different date formats.
  • Systems that display relative time representations.
  • Software dealing with date range calculations and comparisons.
  • Projects where standard time package features are insufficient or cumbersome.

The gotime stands out by offering features that are either missing or not as user-friendly in the standard time package, making it an invaluable addition to any Go developer's toolkit.

Installation

Installation is simple. Just run the following command in your terminal to install the gotime package in your project.

go get github.com/maniartech/gotime

Intuitive Date Format Specifiers (IDFS)

We've developed the Intuitive Date Format Specifiers (IDFS) for gotime. IDFS is a cAsE-insensitive format, eliminating ambiguity often associated with dd-mm-yyyy formats. This intuitive specifiers makes date and time formatting simple and hackable. entry by removing the need to remember upper and lower case attributes, a common issue with other similar formats. For example, %Y represents a four-digit year, while %y denotes a two-digit year in strftime. In contrast, IDFS intuitively uses yyyy for a four-digit year and yy for a two-digit year. Typing dates is also more straightforward with IDFS, as the format yyyy-mm-dd is easier to remember and input compared to the less intuitive 2006-01-02.

It supports simple, human-friendly date-time formatting. The table below displays the supported formats. Internally, gotime utilizes time.Time.Format() and converts human-friendly formats into the time.Time format. For instance, it transforms yyyy-mm-dd into 2006-01-02 before using time.Time.Format() to format the date.

Date Formats

FormatOutputDescription
yy06Two-digit year with leading zero
yyyy2006Four-digit year
m1Month without leading zero
mm01Month in two digits with leading zero
mt1stMonth in ordinal format (not for parsing)
mmmJanMonth in short name
mmmmJanuaryMonth in full name
d2Day without leading zero
dd02Day in two digits with leading zero
db 2Day in blank-padded two digits
dt2ndDay in ordinal format (not for parsing)
ddd002Zero padded day of year
wwwMonThree-letter weekday name
wwwwMondayFull weekday name

Time Formats

FormatOutputDescription
h3Hour in 12-hour format without leading zero
hh03Hour in 12-hour format with leading zero
hhh15Hour in 24-hour format without leading zero
apmAM/PM in lowercase
aaPMAM/PM in uppercase
ii04Minute with leading zero
i4Minute without leading zero
ss05Second with leading zero
s5Second without leading zero
.0.00Microsecond with leading zero
.9.99Microsecond without leading zero

Timezone Formats

FormatOutputDescription
zZThe Z literal represents UTC
zzMSTTimezone abbreviation
o±07Timezone offset with leading zero (only hours)
oo±0700Timezone offset with leading zero without colon
ooo±07:00Timezone offset with leading zero with colon

Built-in Formats

It provides all the built-in formats supported by the standard time package. Such as time.Layout, time.ANSIC, time.UnixDate, time.RubyDate, time.RFC822, time.RFC822Z, time.RFC850, time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano, time.Kitchen, etc.

API Reference

Date Parsing, Formatting and Conversion

FunctionDescription
ParseParses the given date string using IDFS, a human-friendly, hackable date format
ParseInLocationParses the given date string in the specified location using IDFS, a human-friendly, hackable date format, and the given location
FormatFormats the given date using IDFS format specifiers
FormatTimestampFormats the given timestamp in Unix format using IDFS format specifiers
ConvertConverts the given date string from one format to another using IDFS format specifiers

Time Ago

FunctionDescription
TimeAgoReturns the humanized relative time from the given date

Relative Date Finders

Years

FunctionDescription
YearStartReturns the start date of the given year
YearEndReturns the end date of the given year
LastYearReturns the date exactly one year before today
NextYearReturns the date exactly one year after today
YearsReturns the date of the given number of years from now or the given date

Months

FunctionDescription
MonthStartReturns the start date of the given month
MonthEndReturns the end date of the given month
LastMonthReturns the date exactly one month before today
NextMonthReturns the date exactly one month after today
MonthsReturns the date of the given number of months from now or the given date

Weeks

FunctionDescription
WeekStartReturns the start date of the given week
WeekStartOnConsiders the given day as the start of the week and returns the start date of the week
WeekEndReturns the end date of the given week
WeekEndOnConsiders the given day as the end of the week and returns the end date of the week
LastWeekReturns the date exactly one week before today
NextWeekReturns the date exactly one week after today
WeeksReturns the date of the given number of weeks from now or the given date

Days

FunctionDescription
SoDReturns the start of the given day
EoDReturns the end of the given day
YesterdayReturns the date exactly one day before today
TomorrowReturns the date exactly one day after today
DaysReturns the date of the given number of days from now or the given date

DateRange Functions

FunctionDescription
IsInRangeReturns true if the given date is in the specified range
IsInDateRangeReturns true if the given date is in the specified range. Before performing inclusive comparison, it sets the time to the start of the day for the start date and the end of the day for the end date.

DateTime Utility Functions

FunctionDescription
IsLeapYearReturns true if the given year is a leap year
DaysInMonthReturns the number of days in the given month
DaysInYearReturns the number of days in the given year
DaysInQuarterReturns the number of days in the given quarter
NewDateReturns the date from the given year, month, and day
NewTimeReturns the time from the given hour, minute, and second
DateValueReturns the serial number of the given date from 1900-01-01
DiffReturns the difference between two durations. Can also return a rounded result.
LatestReturns the latest time from the specified list of times
EarliestReturns the earliest time from the specified list of times
IsBetweenReturns true if the given date is between the specified range
TruncateTimeTruncates the time part from the given date
TruncateDateTruncates the date part from the given date
NetWorkDaysReturns the number of workdays between two dates, considering holidays and weekends
WorkDayReturns the date after the specified number of workdays, considering holidays and weekends
PrevWorkDayReturns the date before the specified number of working days, considering holidays and weekends
ReplaceDateReplaceDate lets you replace the date part of a time.Time object with a new date.
ReplaceTimeReplaceTime lets you replace the time part of a time.Time object with a new time.

For more information, see the time package documentation.

Code Clean up - WIP ⚠️

While the gotime is fully functional, and API has been finalized thoroughly tested and documented, and can be used in production, there are few area that needs to be cleaned up. Such as:

  • Some of the source code and tests are not well organized
  • In some cases, documentation needs to be improved
  • Uncomented code in the source files etc

We shall be working on these issues in the coming days. If you find any issues or have any suggestions, please feel free to open an issue or submit a pull request.

Contributing

Contributions to gotime are welcome. Please ensure that your code adheres to the existing style and includes tests covering new features or bug fixes.

License

gotime is MIT licensed.