timeutil

repository·master·Indexed 42 days ago

https://github.com/leekchan/timeutil

A Go library providing extensions to the standard time package, featuring a Python-inspired Timedelta for duration arithmetic and a Strftime function for flexible date formatting with Unicode support.

Tokens
1.1K
Snippets
6
Records
7
Agent score
2%

What's inside timeutil

  1. What is Timedelta?

    master

    A Timedelta represents a duration between two dates, inspired by Python's timedelta. It allows you to define durations using human-readable units like Days, Weeks, or Minutes, which can then be converted into a standard time.Duration to be used with Go's native time.Time objects.

    base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)
    td := timeutil.Timedelta{Days: 10, Minutes: 17, Seconds: 56}
    
    result := base.Add(td.Duration())
    // result is 2015-02-13 00:17:56 +0000 UTC
  2. Perform arithmetic operations on Timedelta

    master

    You can perform arithmetic on Timedelta objects using the following methods:

    • Add(t2 *Timedelta): Returns the sum of t + t2.
    • Subtract(t2 *Timedelta): Returns the difference t - t2.
    • Abs(): Returns the absolute value of the Timedelta.
    td1 := timeutil.Timedelta{Days: 1}
    td2 := timeutil.Timedelta{Days: 2}
    
    // Addition
    sum := td1.Add(&td2)
    
    // Subtraction
    diff := td1.Subtract(&td2)
    
    // Absolute value
    abs := diff.Abs()
  3. Format dates with Strftime

    master

    The Strftime function formats a *time.Time according to a format string containing directives starting with %. Unlike standard Go time formatting, Strftime supports Unicode characters within the format string.

    date := time.Date(2015, 7, 2, 15, 24, 30, 35, time.UTC)
    str := timeutil.Strftime(&date, "작성일 : %a %b %d %I:%M:%S %p %Y")
    // str: "작성일 : Thu Jul 02 03:24:30 PM 2015"
  4. Reference: Strftime directives

    master

    The following directives are supported by timeutil.Strftime:

    DirectiveMeaningExample
    %aWeekday as locale’s abbreviated nameSun, Mon
    %AWeekday as locale’s full nameSunday, Monday
    %wWeekday as a decimal number (0=Sunday)0, 1
    %dDay of the month (zero-padded)01, 02
    %bMonth as locale’s abbreviated nameJan, Feb
    %BMonth as locale’s full nameJanuary, February
    %mMonth as a zero-padded decimal number01, 02
    %yYear without century (zero-padded)00, 01
    %YYear with century1970, 2013
    %HHour (24-hour clock, zero-padded)00, 23
    %IHour (12-hour clock, zero-padded)01, 12
    %pMeridian indicatorAM, PM
    %MMinute (zero-padded)00, 59
    %SSecond (zero-padded)00, 59
    %fMicrosecond (zero-padded)000000
    %zUTC offset (+HHMM or -HHMM)+0000
    %ZTime zone nameUTC
    %jDay of the year (zero-padded)001, 366
    %UWeek number (Sunday as first day)00, 53
    %WWeek number (Monday as first day)00, 53
    %cDate and time representationTue Aug 16 21:30:00 1988
    %xDate representation08/16/88
    %XTime representation21:30:00
    %%A literal '%' character%
  5. Use the Timedelta struct

    master

    The Timedelta struct is used to define a span of time. All fields are optional and default to 0. You can initialize it by specifying any combination of the following fields (all of type time.Duration):

    • Days
    • Seconds
    • Microseconds
    • Milliseconds
    • Minutes
    • Hours
    • Weeks

    Note: While the fields are named as units, they are technically time.Duration types.

    td := timeutil.Timedelta{Days: 10, Minutes: 17, Seconds: 56}
  6. Convert Timedelta to time.Duration

    master

    Use the Duration() method on a Timedelta instance to get a standard time.Duration. This value can be passed directly into Go's time.Time.Add() method.

    base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)
    td := timeutil.Timedelta{Days: 10}
    
    result := base.Add(td.Duration())