github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/cmd/snap/times.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package main
    21  
    22  import (
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/snapcore/snapd/i18n"
    27  	"github.com/snapcore/snapd/strutil/quantity"
    28  	"github.com/snapcore/snapd/timeutil"
    29  )
    30  
    31  var timeutilHuman = timeutil.Human
    32  
    33  type timeMixin struct {
    34  	AbsTime bool `long:"abs-time"`
    35  }
    36  
    37  var timeDescs = mixinDescs{
    38  	// TRANSLATORS: This should not start with a lowercase letter.
    39  	"abs-time": i18n.G("Display absolute times (in RFC 3339 format). Otherwise, display relative times up to 60 days, then YYYY-MM-DD."),
    40  }
    41  
    42  func (mx timeMixin) fmtTime(t time.Time) string {
    43  	if mx.AbsTime {
    44  		return t.Format(time.RFC3339)
    45  	}
    46  	return timeutilHuman(t)
    47  }
    48  
    49  type durationMixin struct {
    50  	AbsTime bool `long:"abs-time"`
    51  }
    52  
    53  var durationDescs = mixinDescs{
    54  	// TRANSLATORS: This should not start with a lowercase letter.
    55  	"abs-time": i18n.G("Display absolute times (in RFC 3339 format). Otherwise, display short relative times."),
    56  }
    57  
    58  func (mx durationMixin) fmtDuration(t time.Time) string {
    59  	if mx.AbsTime {
    60  		return t.Format(time.RFC3339)
    61  	}
    62  	return strings.TrimSpace(quantity.FormatDuration(time.Since(t).Seconds()))
    63  }