How to convert GMT date to UNIX time in golang? - go

I need to convert a GMT date like (Mon, 23 Dec 2019 18:52:45 GMT) to time.Time.Unix in Go
I've tried to parse it before and then convert it to Unix format. But it's not so clean what are the predefined layouts in time.Parse
https://golang.org/pkg/time/#Parse
How to do that?

The layout of the date is RFC1123. Parse it with time.Parse() then convert it to Unix with time.Time.Unix().
t, _ := time.Parse(time.RFC1123, "Mon, 23 Dec 2019 18:52:45 GMT")
tUnix:= t.Unix()
fmt.Printf("%s in Unix is %d",t,tUnix)
// Output: 2019-12-23 18:52:45 +0000 GMT in Unix is 1577127165
Try it online

That looks like time.RFC1123. If not, refer to the same section to create your own layout reference string.

Related

How to convert Tue Jun 04 2019 00:00:00 GMT+0530 (India Standard Time) into date format in vue js

I am using vue2-datepicker but i can't save date into database because the date format binded as Tue Jun 04 2019 00:00:00 GMT+0530 (India Standard Time)
How can i solve this?
my code is
<date-picker
type="date"
:lang="lang"
v-model="form.join_date"
>
</date-picker>
but console.log(this.form.join_date) is Tue Jun 04 2019 00:00:00 GMT+0530 (India Standard Time)
How can i get date like '2019-06-18'
Anyone can help me?
thanks in advance
There is an attribute called value-type that would let you bind a string with a specific format instead of a Date object. Based on their examples, I believe it would be something like this:
<date-picker
value-type="format"
format="YYYY-MM-dd"
:lang="lang"
v-model="stringValue">
</date-picker>
See here: https://github.com/mengxiong10/vue2-datepicker#value-type

Ruby unix date incorrect

I have the following Unix timestamp: 1478698378000
And I'm trying to show this as a datetime in Ruby, e.g.
<%= Time.at(#timestamp).to_datetime %>
Which should be returning a date of: Wed, 09 Nov 2016 13:32:58 GMT but the above code actually returns a date of: 48828-02-01T13:26:40+00:00 Ignore formatting!
As you can see it thinks that timestamp is 2nd Feb 48828 13:26:40.
Why is the datetime coming out completely incorrect and the year so far into the future like that? Checking the timestamp on http://www.epochconverter.com/ reveals the timestamp to be correct, so it's Ruby that's returning it incorrectly.
Time.at expects seconds as an argument and your timestamp is an amount of milliseconds. See documentation on Time.at
Why won’t you check the unix timestamp correctness against “Fashion Week Magazine” or “Cosmopolitan” Site?
Unix timestamp is an amount of seconds lasted since 1970-01-01 UTC:
date --date='#1478698378000'
mar feb 1 14:26:40 CET 48828
BTW, dropping last three zeroes gives you back what you’ve expected:
date --date='#1478698378'
mié nov 9 14:32:58 CET 2016

Is DateTime.parse() in Ruby dependent on locale?

I'm wondering regarding the output of the following example:
when parsing 01/03, will it be resolved as Mar, 1st or Jan, 3rd?
Ruby is not locale dependent. Because Ruby is a server-side language and not a client-side language like JavaScript, Ruby uses the system clock from your web app server - and uses this information to calculate the time. Whatever timezone you have your system set to is what your Ruby app will use.
When parsing a date from a string, DateTime will make its best guess based on how the input is formatted:
DateTime.parse('01/03')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.parse('2019/01/03')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.parse('01/03/2019')
#=> Fri, 01 Mar 2019 00:00:00 +0000
You can also explicitly tell DateTime how you want your string parsed using strptime:
date = '01-03-2019'
DateTime.strptime(date, '%m-%d-%Y')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.strptime(date, '%d-%m-%Y')
#=> Fri, 01 Mar 2019 00:00:00 +0000

Convert Date Time to UTC in Ruby

I want to display the date and time in the following format
Sat, Jul 07, 2012 19:28:06 UTC
I haven't been able to find a conversion method that leaves the UTC at the end.
I am currently using the following
t = Time.now.utc.strftime("%a, %B %d, %Y %H:%M:%S %Z")
However this displays the following
Sat, Jul 07, 2012 19:28:06 GMT
I want to see UTC not GMT at the end.
According to the Time documentation:
The Time class treats GMT (Greenwich Mean Time) and UTC (Coordinated Universal Time) as equivalent.
My system shows "UTC" at the end when running your code. For whatever reason, your system prefers the "GMT" moniker. Since this is just cosmetic, you can use:
Time.now.utc.strftime("%a, %B %d, %Y %H:%M:%S UTC")

How can I format date -u so that the results include timezone offset in a Mac OSX terminal session?

In a terminal session I can use date -u to get
Mon Mar 16 03:34:39 2009 UTC
However, I'd like to include the offset. I'm modifying a TextMate tab trigger so that I can insert the full date including the local offset, in standard UTC format. I believe that would be in the following form:
Mon Mar 16 03:34:39 2009 UTC -0500
So, as you can see, I don't know how to get the timezone offset and combine that with formatted date results.
Try this:
echo `date -u` `date +%z`

Resources