Skip to content

Instantly share code, notes, and snippets.

@rustyeddy
Last active September 5, 2024 07:44
Show Gist options
  • Save rustyeddy/77f17f4f0fb83cc87115eb72a23f18f7 to your computer and use it in GitHub Desktop.
Save rustyeddy/77f17f4f0fb83cc87115eb72a23f18f7 to your computer and use it in GitHub Desktop.
Go filename friendly RFC3339 inspired timestamps
// GetTimeStamp returns a timestamp in a file name friendly, version of the RFC3339
// format. The string produced by RFC3339 includes a couple colons ':' that are not
// friendly to most filenames (unix and dos a like). The colons need to be escaped
// if used in a filename, since they are useless, we'll rip em.
func TimeStamp() string {
ts := time.Now().UTC().Format(time.RFC3339)
return strings.Replace(ts, ":", "", -1) // get rid of offensive colons
}
@rustyeddy
Copy link
Author

Fixed typo

@zed-wong
Copy link

func TimeStamp() string {
	ts := time.Now().UTC().Format(time.RFC3339)
	return strings.Replace(strings.Replace(ts, ":", "", -1), "-", "", -1)
}

Result
20091110T230000Z

This way might be better for linux files.

@ifduyue
Copy link

ifduyue commented Sep 5, 2024

Why not directly time.Now().UTC().Format("20060102T150405Z")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment