File: main.go

package info (click to toggle)
golang-github-rickb777-date 1.20.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 460 kB
  • sloc: sh: 63; makefile: 3
file content (99 lines) | stat: -rw-r--r-- 2,237 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2015 Rick Beton. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This tool prints equivalences between the string representation and the internal numerical
// representation for dates and clocks.
package main

import (
	"fmt"
	"os"
	"strconv"

	"github.com/rickb777/date"
	"github.com/rickb777/date/clock"
	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

func usage() {
	fmt.Printf("Usage: %s [-t] number | date | time\n\n", os.Args[0])
	fmt.Printf(" -t:    terse output\n")
	fmt.Printf(" date:  [+-]yyyy/mm/dd | yyyy.mm.dd | dd/mm/yyyy | dd.mm.yyyy\n")
	fmt.Printf(" time:  e.g. 11:15:20 | 2:45pm | 1:15:10.101\n")
	os.Exit(0)
}

var titled = false
var terse = false
var success = false
var printer = message.NewPrinter(language.English)

func sprintf(num interface{}) string {
	if terse {
		return fmt.Sprintf("%d", num)
	} else {
		return printer.Sprintf("%d", num)
	}
}

func title() {
	if !terse && !titled {
		titled = true
		fmt.Printf("%-15s %-15s %-15s %s\n", "input", "number", "clock", "date")
		fmt.Printf("%-15s %-15s %-15s %s\n", "-----", "------", "-----", "----")
	}
}

func printArg(arg string) {

	i, err := strconv.ParseInt(arg, 10, 64)
	if err == nil {
		title()
		d := date.NewOfDays(date.PeriodOfDays(i))
		c := clock.Clock(i)
		fmt.Printf("%-15s %-15s %-15s %-12s %s\n", arg, sprintf(i), c, d, d.Weekday())
		success = true
		return
	}

	d, e1 := date.AutoParse(arg)
	if e1 == nil {
		title()
		fmt.Printf("%-15s %-15s %15s %-12s %s\n", arg, sprintf(d.DaysSinceEpoch()), "", d, d.Weekday())
		success = true
	}

	c, err := clock.Parse(arg)
	if err == nil {
		title()
		fmt.Printf("%-15s %-15s %s\n", arg, sprintf(c), c)
		success = true
	}
}

func main() {
	argsWithoutProg := os.Args[1:]
	if len(argsWithoutProg) == 0 {
		usage()
	}

	if len(argsWithoutProg) > 0 && argsWithoutProg[0] == "-t" {
		terse = true
		argsWithoutProg = argsWithoutProg[1:]
	}

	for _, arg := range argsWithoutProg {
		printArg(arg)
	}

	if !success {
		usage()
	}

	if titled {
		fmt.Printf("\n# dates are counted using days since Thursday 1st Jan 1970\n")
		fmt.Printf("# clock operates via milliseconds since midnight\n")
	}
}