GO: Date and Time Parsing in GO [duplicate] - go

This question already has answers here:
Parsing date/time strings which are not 'standard' formats
(4 answers)
Closed 7 years ago.
I have format string "day/month/year, hour:minute:second"
How do I parse into a time object?
I tried:
const longForm = "5/01/2015, 12:00:00"
t1, _ := time.Parse(longForm, "5/01/2015, 12:00:00")
0001-01-01 00:00:00 +0000 UTC
I get some UTC time, but this is not helpful if I want to compare times because I get the same UTC time for them all. Any help?

Rule #1 of fightclub, err Go, check your errors.
That being said, the format for time parsing is defined in the documentation (scroll down to constants).
For your specific question, the format is 1/_2/2006, 15:04:05.
playground

Related

Go time.Time String() adds "+0000 +0000", why?

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.

Time.strptime doesn't parse week formatters?

Using Ruby 2.1, I am trying to find the reciprocal of Time#strftime('%Y%U'). For example:
s = Time.parse("2014-05-07 16:41:48 -0700").strftime('%Y%U')
# 201418
t = Time.strptime(s, '%Y%U')
# Expected: 2014-05-04 00:00:00 -0700
# Actual: 2014-01-01 00:00:00 -0800
This topic suggested to use %G so I read the docs and tried it, but all I get out of it is the current Time. eg:
t = Time.strptime('201418', '%G%U')
# 2014-05-13 12:07:51 -0700
From the docs, it looks to me that %G is only intended to work with %V as both are ISO 8601 and %U is not, but even even using %G%V I get back the current time.
So what's the right way to turn a %Y%U string into the corresponding Time?
This could be a bug, since it seems to work fine if you use DateTime instead.
s = Time.parse("2014-05-07 16:41:48 -0700").strftime('%Y%U')
DateTime.strptime(s, "%Y%U")
#<DateTime: 2014-05-04T00:00:00+00:00 ((2456782j,0s,0n),+0s,2299161j)>
Edit:
If you look at the source code this is actually just dumb coding. Time.strptime calls Date._strptime which correctly parses out the year and week number. But then Time.strptime stupidly only looks for the usual year, month, day, hour, etc. and ignores the week number entirely.
I submitted a bug report.

My date format : 09/28/2012 16:35:34 , I want the date format like : 2012-09-28T16:35:34 , using gsub [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ruby - Change string in a date format to another format
My date format : 09/28/2012 16:35:34, I want the date to be formatted like: 2012-09-28T16:35:34, I need to compile the code in jruby.
You probably want to convert the date to something more useful:
require 'date'
dt = DateTime.strptime "09/28/2012 16:35:34", '%m/%d/%Y %H:%M:%S'
# => #<DateTime: 2012-09-28T16:35:34+00:00 (106107805067/43200,0/1,2299161)>
Now you can do any transformation:
dt.strftime '%FT%T'
# => "2012-09-28T16:35:34"
This also raises an exception when the date format is wrong, which is useful to notice when things break.
For more Information see the Apidocs for Date.
Find
(\d+)\/(\d+)\/(\d+) ([\d:]+)
replace with
$3-$1-$2T$4
Here you can see the groups (which in the regex are the parts in ()), $1 is the first group, $2 the second and so on. Basically you need to reorder the groups putting - in the middle and T before the hour.

Ruby - time.now in UTC [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I Convert DateTime.now to UTC in Ruby?
How do I get current time in Date-time-milliseconds & UTC?
Ex. 2012-03-22T18:48:40.873Z
I tried -
Time.now.utc_offset.to_s
Time.now.xs_datetime
Time.now.utc is what you must be using.

Parsing date like 1240915075

I am learning programming and I choose Ruby as the first language to learn.
I am parsing an XML where dates are in this form: 1240915075 1224855068
How is this format called? How to use that value in a Date or Time object?
Thank you!
This is UNIX time (sometimes called Epoch time). It measures the number of seconds elapsed since January 1, 1970 (The Unix epoch is the time 00:00:00 UTC on 1 January 1970)
Here's an example converter: http://www.esqsoft.com/javascript_examples/date-to-epoch.htm
A stackoverflow question regarding converting integer time using Ruby: Ruby / Rails: convert int to time OR get time from integer?
use the Time.at function to convert e.g.:
t = Time.at(i)
That's Epoch Time (the first one corresponds to Tue Apr 28 2009 11:37:55 GMT+0100).
You can get a datetime out of it, using Time.at, like this:
Time.at(1240915075)
That is a unix timestamp - the number of seconds since jan 1st 1970.
An example of how to use it in Ruby is here:
t = Time.at(1215163257)
puts t.to_date
>> 2008-07-04

Resources