github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/example_duration_period_test.go (about)

     1  package chrono_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/go-chrono/chrono"
     7  )
     8  
     9  func ExampleParseDuration() {
    10  	p, d, _ := chrono.ParseDuration("P3Y6M4DT1M5S")
    11  
    12  	fmt.Println(p.Years, "years;", p.Months, "months;", p.Weeks, "weeks;", p.Days, "days;", d.Seconds(), "seconds")
    13  	// Output: 3 years; 6 months; 0 weeks; 4 days; 65 seconds
    14  }
    15  
    16  func ExampleFormatDuration() {
    17  	p := chrono.Period{Years: 3, Months: 6, Days: 4}
    18  	d := chrono.DurationOf(1*chrono.Hour + 30*chrono.Minute + 5*chrono.Second)
    19  
    20  	fmt.Println(chrono.FormatDuration(p, d))
    21  	// Output: P3Y6M4DT1H30M5S
    22  }
    23  
    24  func ExamplePeriod_Parse() {
    25  	var p chrono.Period
    26  	_ = p.Parse("P3Y6M4D")
    27  
    28  	fmt.Println(p.Years, "years;", p.Months, "months;", p.Weeks, "weeks;", p.Days, "days")
    29  	// Output: 3 years; 6 months; 0 weeks; 4 days
    30  }
    31  
    32  func ExamplePeriod_Format() {
    33  	p := chrono.Period{Years: 3, Months: 6, Days: 4}
    34  
    35  	fmt.Println(p.Format())
    36  	// Output: P3Y6M4D
    37  }
    38  
    39  func ExampleDuration_Parse() {
    40  	var d chrono.Duration
    41  	_ = d.Parse("PT1M5S")
    42  
    43  	fmt.Println(d.Seconds())
    44  	// Output: 65
    45  }
    46  
    47  func ExampleDuration_Format() {
    48  	d := chrono.DurationOf(1*chrono.Hour + 30*chrono.Minute + 5*chrono.Second)
    49  
    50  	fmt.Println(d.Format())
    51  	// Output: PT1H30M5S
    52  }