Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a duration

Tags:

go

I have the following code which I can't run on play because I use the gin framework and filewalk.

package main

import (
    "fmt"
    "os"
    "path/filepath"
    "time"
    "regexp"
    "github.com/gin-gonic/gin"
    "sort"
    "strings"
    "github.com/davidscholberg/go-durationfmt"
)

var filext = regexp.MustCompile(`\.[mM][pP]4|\.[mM]4[vV]|\.jpg|\.[hH]264|\.go`)
var m map[int]string
var keys []int

func main() {

    if gin.IsDebugging() {
        fmt.Print("This progamm shows only the path and the age of a file in 'STARTDIR'\n")
        fmt.Print("only the following files will be handled '.[mM][pP]4|.[mM]4[vV$|.[hH]264|.go'\n")
        fmt.Print("The git repository: https://gitlab.com/aleks001/show-files-age \n")
    }

    if len(os.Getenv("LISTEN_IP_PORT")) == 0 {
        fmt.Print("I need a ip and port on which I should listen.\n")
        os.Exit(1)
    }
    router := gin.Default()

    gin.DisableConsoleColor()

    router.GET("/videoinfo",getInfo)

    router.Run(os.Getenv("LISTEN_IP_PORT"))
}

func getInfo(c *gin.Context)  {

    loc, _ := time.LoadLocation("Europe/Vienna")
    var startdir = ""

    if os.Getenv("STARTDIR") != "" {
        startdir = os.Getenv("STARTDIR")
    } else if c.GetHeader("STARTDIR") != "" {
        startdir = c.GetHeader("STARTDIR")
    } else {
        c.String(404,"Startdir not found <br>\n")
        return
    }

    m = make(map[int]string)
    keys = nil

    filepath.Walk(startdir,walkpath)

    for k := range m {
        keys = append(keys, k)
    }
    sort.Ints(keys)

    for _, k := range keys {
        t := time.Date(time.Now().Year(),time.Now().Month(),time.Now().Day(),time.Now().Hour(),k,time.Now().Second(),time.Now().Nanosecond(),loc)
        durStr, err := durationfmt.Format(time.Since(t), "%h:%m")

        if err != nil {
            fmt.Println(err)
        } else {
            //fmt.Println(durStr)
            fmt.Printf("Key: %s Value: %s\n", durStr , m[k])
            c.String(200,"Minutes: %s File: %s\n", durStr, m[k])
        }
    }
}

func walkpath(path string, f os.FileInfo, err error) error {

  if err != nil {
        fmt.Println(err)
    } else {
        if filext.MatchString(path) {
            age := time.Now().Sub(f.ModTime())
            path_new := strings.Replace(path,"/videos/","",1)
            // path_new := strings.Replace(path,"..\\","",1)
            /*
            fmt.Printf("Path: %s, ModTime: %s, Age: %s <br>\n", walker.Path(), walker.Stat().ModTime(), age)
            c.String(200,"Path: %s, ModTime: %s, Age: %s <br>\n", walker.Path(), walker.Stat().ModTime(), age)
            */
            fmt.Printf("Path: %s, Age: %d age minutes %0.2f  <br>\n", path_new, age, age.Minutes())
            m[int(age.Minutes())]=path_new
            //c.String(200,"Path: %s, Age: %0.2f <br>\n", path, age.Minutes())
        }
    //fmt.Printf("%s with %d bytes at motime %s\n", path,f.Size(), f.ModTime())
}
    return nil
}

What I want to do is a sorted output of files based on filext als filter and the modtime as sort criteria.

I was able to fulfil the most part of the request but the output looks ugly as you can see below.

I have used https://github.com/davidscholberg/go-durationfmt to format the duration but the output looks ugly or I missus the library.

Minutes: 0:6 File: upload/dir003/file1.m4v
Minutes: 0:5 File: transfer/dir5/file2.jpg
Minutes: -5:-48 File: transfer/dir001/file.mp4
Minutes: -6:-21 File: transfer/03.jpg
Minutes: -6:-22 File: transfer/02.mp4
like image 520
Aleksandar Avatar asked Nov 17 '17 00:11

Aleksandar


People also ask

How to format a duration in Java?

Java Date Time - Duration toString() example Duration toString() returns a string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12. 345S. The format of the string will be PTnHnMnS , where n is the relevant hours, minutes or seconds part of the duration.

Can Java duration be negative?

Checks if this duration is zero length. A Duration represents a directed distance between two points on the time-line and can therefore be positive, zero or negative. This method checks whether the length is zero.

What is PT duration Java?

PT means 'Period of Time'.


3 Answers

FYI, if you just want to quickly display a duration, the built-in formatting works well:

fmt.Sprintf("duration: %s", d)

will display something like this:

duration: 7h3m45s
like image 191
tothemario Avatar answered Oct 22 '22 19:10

tothemario


For example, to provide a custom format for a duration,

package main

import (
    "fmt"
    "time"
)

func fmtDuration(d time.Duration) string {
    d = d.Round(time.Minute)
    h := d / time.Hour
    d -= h * time.Hour
    m := d / time.Minute
    return fmt.Sprintf("%02d:%02d", h, m)
}

func main() {
    modTime := time.Now().Round(0).Add(-(3600 + 60 + 45) * time.Second)
    since := time.Since(modTime)
    fmt.Println(since)
    durStr := fmtDuration(since)
    fmt.Println(durStr)
}

Playground: https://play.golang.org/p/HT4bFfoA5r

Output:

1h1m45s
01:02

If you want to sort on a duration then use the Go sort package. I would sort on ModTime to defer the calculation of the duration, Since(ModTime), to be accurate at the time it is printed. For example,

package main

import (
    "fmt"
    "os"
    "path/filepath"
    "sort"
    "strings"
    "time"
)

func isVideo(path string) bool {
    videos := []string{".mp4", ".m4v", ".h264"}
    ext := strings.ToLower(filepath.Ext(path))
    for _, video := range videos {
        if ext == video {
            return true
        }
    }
    return false
}

type modTimeInfo struct {
    path    string
    modTime time.Time
}

func walkModTime(root string) ([]modTimeInfo, error) {
    var infos []modTimeInfo
    err := filepath.Walk(
        root,
        func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }
            if info.Mode().IsRegular() {
                path = filepath.Clean(path)
                if !isVideo(path) {
                    return nil
                }
                sep := string(filepath.Separator)
                dir := sep + `Videos` + sep
                path = strings.Replace(path, dir, sep, 1)
                infos = append(infos, modTimeInfo{
                    path:    path,
                    modTime: info.ModTime()},
                )
            }
            return nil
        },
    )
    if err != nil {
        return nil, err
    }
    return infos, nil
}

func sortModTime(infos []modTimeInfo) {
    sort.SliceStable(
        infos,
        func(i, j int) bool {
            return infos[i].modTime.Before(infos[j].modTime)
        },
    )
}

func fmtAge(d time.Duration) string {
    d = d.Round(time.Minute)
    h := d / time.Hour
    d -= h * time.Hour
    m := d / time.Minute
    return fmt.Sprintf("%02d:%02d", h, m)
}

func main() {
    root := `/home/peter/Videos` // Testing ...
    infos, err := walkModTime(root)
    if err != nil {
        fmt.Println(err)
        return
    }
    sortModTime(infos)
    now := time.Now()
    for _, info := range infos {
        age := fmtAge(now.Sub(info.modTime))
        fmt.Println("Age (H:M):", age, "File:", info.path)
    }
}

Playground: https://play.golang.org/p/j2TUmJdAi4

like image 33
peterSO Avatar answered Oct 22 '22 20:10

peterSO


Another way to format the duration if you don't care about the day, month or year

package main

import (
    "fmt"
    "time"
)

type Timespan time.Duration

func (t Timespan) Format(format string) string {
    z := time.Unix(0, 0).UTC()
    return z.Add(time.Duration(t)).Format(format)
}

func main() {
    dur := 7777 * time.Second
    fmt.Println(Timespan(dur).Format("15:04:05")) // 02:09:37
}

https://play.golang.org/p/XM-884oYMvE

like image 5
Timo Huovinen Avatar answered Oct 22 '22 19:10

Timo Huovinen