I'd like to rename some files based on their modification date.
When I use the time.Format method to get the correct string, basically in this format YYYY-MM-DD_HH-MM-SS, the day has a trailing 0.
Am I doing something wrong here?
package main
import (
"time"
"fmt"
)
func main() {
loc, _ := time.LoadLocation("Europe/Berlin")
const layout = "2006-01-20_15-04-05"
t := time.Date(2013, 07, 23, 21, 32, 39, 0, loc)
fmt.Println(t)
fmt.Println(t.Format(layout))
}
Click to play
Output:
2013-07-23 21:32:39 +0200 CEST
2013-07-230_21-32-39
Your layout isn't using the reference date: change it to "2006-01-02_15-04-05"
When you use "2006-01-20_15-04-05", the formatter see the 2, and uses that for the day, then keeps the extra 0 since it doesn't match any part of the reference date.
Related
I have dates stored as timestamps in mongo, for example, 1564444800000000000. I want to check if a timestamp is between 2 dates - July 1, 2021 and July, 2021. I converted these 2 dates to timestamps using https://www.epochconverter.com/. I first fetched all records and while looping through the records, I did a check like so
cDate := 1564444800000000000
if cDate > 1625167488000 && cDate < 1627759488000 {
log.Fatal("found one!")
}
But this not seem to work as I get no matches. Is there a better way of doing this?
I mean just looking at the numbers:
cDate := 1564444800000000000
1625167488000
1627759488000
I don't see how one with six more digits than the one's it's supposed to be between would ever be between them. And even if we divided CDate by 1000000 it would be
cDate := 1564444800000
1625167488000
1627759488000
and it's still less than both of those, so it still wouldn't be between them. But that being said, you most likely do just need to divide cDate by 1000000 and your example is just not a good one (since 1564444800000 is July 29 2019)
I suggest to transform in a date then check with the before and after API, as example:
package main
import (
"fmt"
"time"
)
func main() {
cDate := int64(1564444800000000000)
firstJuly := time.Date(2019, 7, 1, 0, 0, 0, 0, time.UTC)
thrirtyOneJuly := time.Date(2019, 7, 31, 0, 0, 0, 0, time.UTC)
date := time.Unix(0, cDate)
// fmt.Println(date) //will print 2019-07-30 00:00:00 +0000 UTC
fmt.Println("Is July?", date.After(firstJuly) && date.Before(thrirtyOneJuly))
}
will print
Is July? true
See on playground
I am to get a time struct from a string. I am using the function time.ParseTime() with the layout "2006-01-02 15:04".
When I execute the function with any valid time string I get a time struct pointing to that time stamp but it is in UTC.
How can I change it to a different time zone? To be clear I want the same timestamp but with a different time zone. I don't want to convert between timezones; I just want to get the same time object but not in UTC.
Use time.ParseInLocation to parse time in a given Location when there's no time zone given. time.Local is your local time zone, pass that in as your Location.
package main
import (
"fmt"
"time"
)
func main() {
// This will honor the given time zone.
// 2012-07-09 05:02:00 +0000 CEST
const formWithZone = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.ParseInLocation(formWithZone, "Jul 9, 2012 at 5:02am (CEST)", time.Local)
fmt.Println(t)
// Lacking a time zone, it will use your local time zone.
// Mine is PDT: 2012-07-09 05:02:00 -0700 PDT
const formWithoutZone = "Jan 2, 2006 at 3:04pm"
t, _ = time.ParseInLocation(formWithoutZone, "Jul 9, 2012 at 5:02am", time.Local)
fmt.Println(t)
}
I have a list of date strings that appear in this format without any time zone or offset information:
[
"2019-04-30T12:34:00.000", // In 2019, DST started in March 10, 2019, so this should have the appropriate DST offset
"2017-11-20T13:45:00.000" // In 2017, DST ended on November 5, 2017 so this should have the appropriate standard time offset
]
I know the IANA region (eg, America/New_York) that these dates and times were created in, but I cannot figure out how to dynamically generate the appropriate offset given this information using go and the time package.
I have thought about the following:
Appending a hardcoded value to the end of the date string (ie, "2019-04-30T12:34:00.000" + "-04:00)
Write custom logic to determine if a date falls within the boundary of standard or daylight savings time
However, these solutions only work for some dates or the logic becomes exceedingly complicated.
I was able to figure it out
package main
import (
"log"
"time"
)
func main() {
Chicago, _ := time.LoadLocation("America/Chicago")
t := time.Date(2019, time.March, 1, 12, 30, 0, 0, Chicago)
log.Print(t) // 2019-03-01 12:30:00 -0600 CST
log.Print(t.UTC()) // 2019-03-01 18:30:00 +0000 UTC
t = time.Date(2019, time.November, 2, 12, 30, 0, 0, Chicago)
log.Print(t) // 2019-11-02 12:30:00 -0500 CDT
log.Print(t.UTC()) // 2019-11-02 17:30:00 +0000 UTC
}
go playground # https://play.golang.org/p/nP28y9jSDAk
An even cleaner solution by leveraging a custom layout and time.LoadLocation
package main
import (
"fmt"
"time"
)
func main() {
Chicago, _ := time.LoadLocation("America/Chicago")
cdt, _ := time.ParseInLocation("2006-01-02T15:04:05.999999", "2019-04-30T12:34:00.000", Chicago)
fmt.Println(cdt)
fmt.Println(cdt.UTC())
cst, _ := time.ParseInLocation("2006-01-02T15:04:05.999999", "2017-11-20T13:45:00.000", Chicago)
fmt.Println(cst)
fmt.Println(cst.UTC())
}
go playground # https://play.golang.org/p/3Ai4qVz0af5
I am writing a scraper that scrapes offers off websites and these offers have end dates. One such website has offers that expire every Sunday. I have gone through the golang time documentation but still dont get how that can be done the equivalence I found in PHP and is pretty simple.
$endDate = strtotime('this Sunday, 23:59:59');
Is there a golang way to do this?
Write a function in Go using the Go standard library time package. For example,
package main
import (
"fmt"
"time"
)
func endDate(t time.Time, wd time.Weekday) time.Time {
next := int((wd - t.Weekday() + 7) % 7)
y, m, d := t.Date()
return time.Date(y, m, d+next+1, 0, 0, 0, -1, t.Location())
}
func main() {
now := time.Now().Round(0)
fmt.Println(now, now.Weekday())
end := endDate(now, time.Sunday)
fmt.Println(end, end.Weekday())
}
Playground: https://play.golang.org/p/T0oZGRO9NV8
Output:
2018-11-08 05:25:01.104445722 -0500 EST Thursday
2018-11-11 23:59:59.999999999 -0500 EST Sunday
I am looking for an option to convert UTC time string to unix timestamp.
The string variable I have is 02/28/2016 10:03:46 PM and it needs to be converted to a unix timestamp like 1456693426
Any idea how to do that?
First of, the unix timestamp 1456693426 does not have the time 10:03:46 PM but 9:03:46 PM in UTC.
In the time package there is the function Parse with expects a layout to parse the time. The layout is constructed from the reference time Mon Jan 2 15:04:05 -0700 MST 2006. So in your case the layout would be 01/02/2006 3:04:05 PM. After using Parse you get a time.Time struct on which you can call Unix to receive the unix timestamp.
package main
import (
"fmt"
"time"
)
func main() {
layout := "01/02/2006 3:04:05 PM"
t, err := time.Parse(layout, "02/28/2016 9:03:46 PM")
if err != nil {
fmt.Println(err)
}
fmt.Println(t.Unix())
}