-
Notifications
You must be signed in to change notification settings - Fork 0
/
filenames.go
113 lines (106 loc) · 4.15 KB
/
filenames.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
########################################################################################
# __ #
# /__ _ #
# \_|(_) #
# _______ _______ _______ _______ __ ______ _______ #
# ( ____ \( )( ___ ) Game ( ____ \ / \ / ___ \ ( __ ) #
# | ( \/| () () || ( ) | Master's | ( \/ \/) ) \/ \ \ | ( ) | #
# | | | || || || (___) | Assistant | (____ | | ___) / | | / | #
# | | ____ | |(_)| || ___ | (Go Port) (_____ \ | | (___ ( | (/ /) | #
# | | \_ )| | | || ( ) | ) ) | | ) \ | / | | #
# | (___) || ) ( || ) ( | Mapper /\____) ) _ __) (_/\___/ / _ | (__) | #
# (_______)|/ \||/ \| Client \______/ (_) \____/\______/ (_)(_______) #
# #
########################################################################################
*/
package util
import (
"os"
"strconv"
"time"
"github.com/lestrrat-go/strftime"
)
//
// FancyFileName expands tokens found in the path string to allow the user
// to specify dynamically-named files at runtime. If there's a problem with
// the formatting, an error is returned along with the original path.
//
// The tokens which may appear in the path include the following
// (note that all of these are modified as appropriate to the locale's
// national conventions and language):
// %A full weekday name
// %a abbreviated weekday name
// %B full month name
// %b abbreviated month name
// %C zero-padded two-digit year 00-99
// %c time and date
// %d day of month as number 01-31 (zero padded)
// %e day of month as number 1-31 (space padded)
// %F == %Y-%m-%d
// %H hour as number 00-23 (zero padded)
// %h abbreviated month name (same as %b)
// %I hour as number 01-12 (zero padded)
// %j day of year as number 001-366
// %k hour as number 0-23 (space padded)
// %L milliseconds as number 000-999
// %l hour as number 1-12 (space padded)
// %M minute as number 00-59
// %m month as number 01-12
// %P process ID
// %p AM or PM
// %R == %H:%M
// %r == %I:%M:%S %p
// %S second as number 00-60
// %s Unix timestamp as a number
// %T == %H:%M:%S
// %U week of the year as number 00-53 (Sunday as first day of week)
// %u weekday as number (1=Monday .. 7=Sunday)
// %V week of the year as number 00-53 (Monday as first day of week)
// %v == %e-%b-%Y
// %W week of the year as number 00-53 (Monday as first day of week)
// %w weekday as number (0=Sunday .. 6=Saturday)
// %X time
// %x date
// %Y full year
// %y two-digit year (00-99)
// %Z time zone name
// %z time zone offset from UTC
// %µ microseconds as number 000-999
// %% literal % character
//
// The extras parameter maps token names to static string values, so more
// tokens specific to the task at hand can be added, such as these that
// the mapper client adds:
// %G "GM" if logged in as the GM, otherwise ""
// %N username
// %n module name
//
func FancyFileName(path string, extras map[byte]string) (string, error) {
ss := strftime.NewSpecificationSet()
if err := ss.Delete('n'); err != nil {
return path, err
}
if err := ss.Delete('t'); err != nil {
return path, err
}
if err := ss.Delete('D'); err != nil {
return path, err
}
if err := ss.Set('P', strftime.Verbatim(strconv.Itoa(os.Getpid()))); err != nil {
return path, err
}
if extras != nil {
for tokenName, tokenValue := range extras {
if err := ss.Set(tokenName, strftime.Verbatim(tokenValue)); err != nil {
return path, err
}
}
}
return strftime.Format(path, time.Now(),
strftime.WithSpecificationSet(ss),
strftime.WithUnixSeconds('s'),
strftime.WithMilliseconds('L'),
strftime.WithMicroseconds('µ'),
)
}