A unix_timestamp of 1405936049 corresponds to: 2014-07-21 09:47:29. My goal is to derive the latter form from the timestamp.
After reading the format documentation, I came up with the following:
fmt.Println(time.Unix(1405936049, 0).Format("2006-01-02 15:04:05"))
which yields: 2014-07-21 02:47:29, which makes sense, since time.Unix(1405936049, 0) gives: 2014-07-21 02:47:29 -0700 PDT (to be clear, I want: 2014-07-21 09:47:29, the hour is incorrect).
I'm sure if I knew the correct terminology, I'd be able to find a solution in the documentation, but at this point, I'm uncertain how to tell the parser to account for -0700 or perhaps an alternative solution would be to use something besides time.Unix(), so that the resulting time would have already accounted for the hour difference? Any help would be appreciated.
You want the UTC time, not your local PDT time. For example,
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Unix(1405936049, 0).UTC().Format("2006-01-02 15:04:05"))
}
Output:
2014-07-21 09:47:29
You have to use Location for this:
loc, _ := time.LoadLocation("Europe/Paris")
fmt.Println(time.Unix(1405936049, 0).In(loc).Format("2006-01-02 15:04:05"))
I think the location you want is "UTC", but I let you check (otherwise, here is a list of all available locations). The reason why in playground the format is already 09:47:29 is that playground does not include locations and uses UTC by default.
Related
I would like something in Ruby roughly equivalent to time.asctime() in Python:
import time
print(time.asctime())
outputs:
Sun Sep 11 10:12:48 2022
I'd like to avoid having to use strftime and having to remember or look up the formats. Also, ideally I'd like both day of the week (e.g., Sun) and the UTC timezone difference (e.g., -0400), but I'd settle for just day of the week.
puts Time.now.asctime
outputs:
Sun Sep 11 10:24:46 2022
Simple String Output
Ruby supports lots of Time, Date, and DateTime objects and output formats. While I think the first answer is closer to the output format you want, the following is potentially simpler and possibly sufficient for many needs when just considering standard output or standard error:
p Time.now.to_s
#=> 2022-09-11 14:10:57 -0400
# using interpolation
p "Time: #{Time.now.to_s}"
#=> "Time: 2022-09-11 14:15:51 -0400"
Other Considerations
Note that if you want to use the results for any sort of comparison or calculation, you'll likely need to convert the result to one of the three object types described above. That's the main reason I mention them. Unless it's just printing to the screen, you should think about how you plan to use the result before deciding which of the objects will be most useful for you.
i'm currently looking for a solution for sometimes now,
i have this cron expression
time := '0 3,10,16,22 * * ?'
and i need to parse this into date and compare it to get a result
what my goal is to get time data from the time var and compare it,
if the time is not in between 00:00 and 00:06 it will return bool false
i understand for comparison i can use if clause but,
how to parse this cron expression and turn it into date solution were not found yet.
i've been reading cron package in godoc for sometimes and dont find it yet maybe i'm missing something?
any kind of solution or input were appreciated thanks!
You could use the package cronexpr from aptible/supercronic:
import "github.com/aptible/supercronic/cronexpr"
import "time"
nextTime := cronexpr.MustParse("0 3,10,16,22 * * ?").Next(time.Now())
Now that you have the next time, you can check if it is between 00:00 and 00:06.
I'm retrieving time from a Postgres DB.
Let's say it is:
2020-02-27 08:57:36.774147+00
Now I wanna write it in output like a string and I do it like this:
var myTime time.Time
fmt.Println(myTime.String())
In output it writes something different:
2020-02-27 08:57:36.774147 +0000 +0000
I need that value to be the same because I need to query something else with it.
Maybe the same issue is described here: https://github.com/golang/go/issues/11712
The function you need is time.Format. Replace "myTime.String()" with "myTime.Format(layout)" with a desired layout as the argument (e.g. the predefined "time.RFC3339" or a reference time format "2006-01-02 15:04:05.000000-07").
go doc:
func (t Time) Format(layout string) string
Format returns a textual representation of the time value formatted
according to layout, which defines the format by showing how the reference
time, defined to be
Mon Jan 2 15:04:05 -0700 MST 2006
would be displayed if it were the value; it serves as an example of the
desired output. The same display rules will then be applied to the time
value.
A fractional second is represented by adding a period and zeros to the end
of the seconds section of layout string, as in "15:04:05.000" to format a
time stamp with millisecond precision.
Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard and
convenient representations of the reference time. For more information about
the formats and the definition of the reference time, see the documentation
for ANSIC and the other constants defined by this package.
What is the equivalent code in golang for the following shell command ?
date -u +%Y-%m-%dT%T%z
If you're looking for a simple, but not perfect solution consider using time.RFC3339 constant. But also know that there are differences between ISO8601 which are too complex for this answer.
See https://ijmacd.github.io/rfc3339-iso8601/ for differences and also has a handy test file generator to show differences. There is also a good discussion on SO here What's the difference between ISO 8601 and RFC 3339 Date Formats?
package main
import (
"time"
"fmt"
)
func main(){
fmt.Println(time.Now().Format(time.RFC3339))
}
golang Time.Format
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05-0700"))
}
I had the following spec:
YYYY-MM-DDThh:mm:ss.sssZ
with the final Z being explicitly present in the examples.
Here's how I dealt with it:
first I found the time.RFCxxx that was the closest to my target
I copied its value
I fiddled with it until I found the expected result
which is
2006-01-02T15:04:05.999Z
ISO8601 allows for variable levels of granularity. You can have just a year, year+month, year+month+day, add a time portion, and optionally have a timezone portion. Go's built-in time parsing, however, requires you to know ahead-of-time which parts will be included.
The github.com/btubbs/datetime library provides a more flexible parser that can handle all the commonly used ISO8601 formats. See https://github.com/btubbs/datetime
Disclosure: I wrote that library.
Replacing the sign in the format with a Z triggers the ISO 8601 behavior. Which is exactly time.RFC3339. If you are wanting the string output to end in 'Z' what you need to do is convert to the UTC zone.
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Now().UTC().Format("2006-01-02T15:04:05Z07:00"))
}
// this is the same format used by RFC3339. just a note on why.
I'm trying to format a date like this: [daynumber] [monthname] [fullyear]
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Format("1 January 2014"))
}
However this prints out "11 November 10110" instead of the correct date "29 November 2014".
What is the correct way to use Time.Format?
Try:
fmt.Println(t.Format("2 January 2006"))
From Time.Format()
Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time,
Mon Jan 2 15:04:05 -0700 MST 2006
The article "Parsing and formatting date/time in Go " adds:
The use of a mnemonic over obscure formatting codes I think reflects the pragmatism of Go’s developers and their focus on creating a language that makes its users more productive
Ironically, I have trouble remembering the exact values and order of that format template.
(Especially the day and month that I keep mixing up, being used to the dd-mm convention, as opposed to mm-dd).