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

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

Related

How to convert GMT date to UNIX time in golang?

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.

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

How to set the correct local time zone in git bash?

I am using git-bash on a Windows system.
The Windows clock shows local time, but inside git-bash everything is in GMT time:
$ date
Mon Mar 31 16:08:57 GMT 2014
Also setting TZ will not change things:
$ TZ="Europe/Berlin" date
Mon Mar 31 16:09:01 GMT 2014
Similarly all times it git log are GMT only.
Is there a way to set the correct timezone in git-bash?
On Windows the TZ variable seems to work differently.
To get the German timezone you have to write:
TZ=GST-1GDT date
If you set it to some "invalid" value like "Europe/Berlin" it will default to GMT.
The same seems to happen on my system when TZ is not set at all.
With the above setting I get Thu Apr 17 16:23:23 GDT 2014 which is not exactly the same as Thu Apr 17 16:23:23 CEST 2014, but at least the time looks right.
Same problem here in my script. Windows was showing 15:47 and the "date" command in gitbash was answering 13:47.
export TZ="CEST-2"
This fixed the problem for me. I wanted Paris time.

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")

The right ISO 8601 format

I am refactoring some code for a Ruby library. This code includes a Date parser.
One of the tests was to parse this string "2008-02-20T8:05:00-010:00" which is supposed to be ISO 8601.
The previous code would actually output: "Wed Feb 20 18:05:00 UTC 2008".
My new code outputs that: "Wed Feb 20 16:05:00 UTC 2008".
My question is: which one is the right one?
Time.parse in Ruby gives the second one. But again, I want to be 100% sure that the previous code AND test were buggy.
Which one is right? (By maybe parsing the string with a library in another language? - I only know Ruby.)
The correct UTC time is 1805. The time group indicates 0805 in zone -10, so to get UTC add the 10 to the given time. Thus 1805. Since 1805 is less than 2400 it's the same day.
If your code is giving 1605, then you almost certainly have the timezone set incorrectly to zone -8, which happens to be Pacific Standard Time.
Aha, looks like your input format is messed up. Observe:
irb(main):003:0> Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
I happen to be in zone -7 so it's suiting that to my locale. But
irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):005:0> t
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):006:0> t.getutc
=> Wed Feb 20 15:05:00 UTC 2008
I'm getting an unexpected result. Now observe:
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
=> Wed Feb 20 11:05:00 -0700 2008
irb(main):008:0> t.getutc
=> Wed Feb 20 18:05:00 UTC 2008
There's the expected result. See the difference? First example vs second:
irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
I took the spurious extra 0 out (which I certainly didn't notice either) and whoosh, it works.
I know this is pretty old, but I just ran across it.
I'll bet that something somewhere is interpreting 010 as an octal number with the value 8. Perhaps it's a bug in the implementation of Time.parse()?

Resources