I'm writing a validation and I have an hash with this structure
elements.map{ |e| [e.id,e.coverable.published_at] }.to_h
=> {305=>Fri, 17 Apr 2020 15:23:00 CEST +02:00,
306=>Fri, 17 Apr 2020 13:00:00 CEST +02:00,
307=>Fri, 17 Apr 2020 09:20:00 CEST +02:00,
308=>Fri, 17 Apr 2020 12:59:00 CEST +02:00,
309=>Fri, 17 Apr 2020 11:39:00 CEST +02:00}
I have a reference date...
published_at
=> Mon, 04 May 2020 23:51:00 CEST +02:00
I have to check if any of the element has a published_at datetime value bigger than my published_at.
Is there a short way to do that?
Try something like this
elements.any? { |e| e.coverable.published_at > your_published_at }
In case you need the element which passes the condition use find
element = elements.find { |e| e.coverable.published_at > your_published_at }
# if element is not nil such element is present
I have an array that contains a hash in each row containing created_at and a value. How do I get the min and max from the array for the value fields?
The array is called - channels_counts_for_history_graph
and
channels_counts_for_history_graph.max[1]
Gives me the max date rather than the max value?
[[Sun, 30 Dec 2018 15:03:55 UTC +00:00, 4305],
[Sun, 30 Dec 2018 15:05:42 UTC +00:00, 4305],
[Mon, 31 Dec 2018 09:24:06 UTC +00:00, 4306],
[Sat, 05 Jan 2019 09:04:50 UTC +00:00, 4308],
[Tue, 01 Jan 2019 11:26:04 UTC +00:00, 4306],
[Wed, 02 Jan 2019 17:24:19 UTC +00:00, 4305]]
Any help appreciated.
Thanks
I suggest using Enumerable#minmax_by to get the min and the max value in just one method call:
array = [['Sun, 30 Dec 2018 15:03:55 UTC +00:00', 4305],['Sun, 30 Dec 2018 15:05:42 UTC +00:00', 4305],['Mon, 31 Dec 2018 09:24:06 UTC +00:00', 4306],['Sat, 05 Jan 2019 09:04:50 UTC +00:00', 4308],['Tue, 01 Jan 2019 11:26:04 UTC +00:00', 4306],['Wed, 02 Jan 2019 17:24:19 UTC +00:00', 4305]]
array.minmax_by(&:last)
#=> [["Sun, 30 Dec 2018 15:03:55 UTC +00:00", 4305], ["Sat, 05 Jan 2019 09:04:50 UTC +00:00", 4308]]
By default when you sort an array sorts by the first element first.
You can reverse the array for the purposes of the sort.
channel_counts_for_history_graph.map(&:reverse).max[0]
I may guess that this is what you were asking for:
[{ created_at: Date.new(2017, 1, 1) }, { created_at: Date.new(2019, 1, 1) }, { created_at: Date.new(2018, 1, 1) }]
.minmax_by { |value| value[:created_at] }
Never tried Go before and currently doing a small project. One of the task is to get current system time and represent it in YYYY-MM-DDT00:00:00Z format. I believe that Z means that time is represented in UTC format but when i looked into db, all timestamps are like this i.e., 2011-11-22T15:22:10Z.
So how can i format like this in Go?
Update
I was able to format it using following code
t := time.Now()
fmt.Println(t.Format("2006-01-02T15:04:05Z"))
Now the question remains, what Z signifies here. Should i get UTC Time?
Another question, it looks like that the value i am using to format impacts the output i.e., when i used 2019-01-02T15:04:05Z the output became 2029-02-02T20:45:11Z, why?
Go provides very flexible way to parse the time by example. For this, you have to write the "reference" time in the format of your choice. The reference time is Mon Jan 2 15:04:05 MST 2006. In my case, I used this reference time to parse the Now():
fmt.Println(time.Now().UTC().Format(time.RFC3339))
There are also other reference types if you want to see:
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Or you can use you desired reference.
"If a time is in Coordinated Universal Time (UTC), a "Z" is added directly after the time without a separating space. "Z" is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". Likewise, "14:45:15 UTC" is written as "14:45:15Z" or "144515Z".[16]"
From https://en.wikipedia.org/wiki/Time_zone#UTC
// Some valid layouts are invalid time values for time.Parse, due to formats
// such as _ for space padding and Z for zone information.
and
// Replacing the sign in the format with a Z triggers
// the ISO 8601 behavior of printing Z instead of an
// offset for the UTC zone. Thus:
// Z0700 Z or ±hhmm
// Z07:00 Z or ±hh:mm
// Z07 Z or ±hh
From the source for package time/format.go
I'm trying to format the current time using this format yyyyMMddHHmmss.
t := time.Now()
fmt.Println(t.Format("yyyyMMddHHmmss"))
That is outputting:
yyyyMMddHHmmss
Any suggestions?
Use
fmt.Println(t.Format("20060102150405"))
as Go uses following constants to format date,refer here
const (
stdLongMonth = "January"
stdMonth = "Jan"
stdNumMonth = "1"
stdZeroMonth = "01"
stdLongWeekDay = "Monday"
stdWeekDay = "Mon"
stdDay = "2"
stdUnderDay = "_2"
stdZeroDay = "02"
stdHour = "15"
stdHour12 = "3"
stdZeroHour12 = "03"
stdMinute = "4"
stdZeroMinute = "04"
stdSecond = "5"
stdZeroSecond = "05"
stdLongYear = "2006"
stdYear = "06"
stdPM = "PM"
stdpm = "pm"
stdTZ = "MST"
stdISO8601TZ = "Z0700" // prints Z for UTC
stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
stdNumTZ = "-0700" // always numeric
stdNumShortTZ = "-07" // always numeric
stdNumColonTZ = "-07:00" // always numeric
stdFracSecond0 = ".0", ".00" // trailing zeros included
stdFracSecond9 = ".9", ".99" // trailing zeros omitted
)
This question comes in top of Google search when you find "golang current time format" so, for all the people that want to use another format, remember that you can always call to:
t := time.Now()
t.Year()
t.Month()
t.Day()
t.Hour()
t.Minute()
t.Second()
For example, to get current date time as "YYYY-MM-DDTHH:MM:SS" (for example 2019-01-22T12:40:55) you can use these methods with fmt.Sprintf:
t := time.Now()
formatted := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
As always, remember that docs are the best source of learning: https://golang.org/pkg/time/
Option 1: Go standard library
t.Format("20060102150405")
Unit
Golang Layout
Examples
Note
Year
06
21, 81, 01
Year
2006
2021, 1981, 0001
Month
January
January, February, December
Month
Jan
Jan, Feb, Dec
Month
1
1, 2, 12
Month
01
01, 02, 12
Day
Monday
Monday, Wednesday, Sunday
Day
Mon
Mon, Wed, Sun
Day
2
1, 2, 11, 31
Day
02
01, 02, 11, 31
zero padded day of the month
Day
_2
⎵1, ⎵2, 11, 31
space padded day of the month
Day
002
001, 002, 011, 031, 145, 365, 366
zero padded day of the year
Day
__2
⎵⎵1, ⎵⎵2, ⎵11, ⎵31, 365, 366
space padded day of the year
Part of day
PM
AM, PM
Part of day
pm
am, pm
Hour 24h
15
00, 01, 12, 23
Hour 12h
3
1, 2, 12
Hour 12h
03
01, 02, 12
Minute
4
0, 4 ,10, 35
Minute
04
00, 04 ,10, 35
Second
5
0, 5, 25
Second
05
00, 05, 25
10-1 to 10-9 s
.0 .000000000
.1, .199000000
Trailing zeros included
10-1 to 10-9 s
.9 .999999999
.1, .199
Trailing zeros omitted
Time zone
MST
UTC, MST, CET
Time zone
Z07
Z, +08, -05
Z is for UTC
Time zone
Z0700
Z, +0800, -0500
Z is for UTC
Time zone
Z070000
Z, +080000, -050000
Z is for UTC
Time zone
Z07:00
Z, +08:00, -05:00
Z is for UTC
Time zone
Z07:00:00
Z, +08:00:00, -05:00:00
Z is for UTC
Time zone
-07
+00, +08, -05
Time zone
-0700
+0000, +0800, -0500
Time zone
-070000
+000000, +080000, -050000
Time zone
-07:00
+00:00, +08:00, -05:00
Time zone
-07:00:00
+00:00:00, +08:00:00, -05:00:00
In Golang 1.17+ for fraction of seconds (.999 or .000) you can use , instead of . (,999 or ,000) but output is always with .!!! See https://github.com/golang/go/issues/48037
Option 2: strftime Go implementation
import strftime "github.com/itchyny/timefmt-go"
strftime.Format(t, "%Y%m%d%H%M%S")
See for more info
https://github.com/itchyny/timefmt-go
https://linux.die.net/man/3/strftime
import("time")
layout := "2006-01-02T15:04:05.000Z"
str := "2014-11-12T11:45:26.371Z"
t, err := time.Parse(layout, str)
if err != nil {
fmt.Println(err)
}
fmt.Println(t)
gives:
>> 2014-11-12 11:45:26.371 +0000 UTC
Time package in Golang has some methods that might be worth looking.
func (Time) Format
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,
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. 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.
Source (http://golang.org/pkg/time/#Time.Format)
I also found an example of defining the layout (http://golang.org/src/pkg/time/example_test.go)
func ExampleTime_Format() {
// layout shows by example how the reference time should be represented.
const layout = "Jan 2, 2006 at 3:04pm (MST)"
t := time.Date(2009, time.November, 10, 15, 0, 0, 0, time.Local)
fmt.Println(t.Format(layout))
fmt.Println(t.UTC().Format(layout))
// Output:
// Nov 10, 2009 at 3:00pm (PST)
// Nov 10, 2009 at 11:00pm (UTC)
}
Go standard library: time
now := time.Now()
fmt.Println(now) // 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
fmt.Println(now.Format("20060102150405"))
fmt.Println(now.Format("2006/01/02/15/04/05"))
fmt.Println(now.Format("2006-01-02 15:04:05"))
fmt.Println(now.Format("2006-01-02 15:04"))
fmt.Println(now.Format("2006/01/02 15:04:05"))
fmt.Println(now.Format("2006/01/02 15:04:05 (-0700)"))
fmt.Println(now.Format("2006年01月02日 15:04"))
fmt.Println(now.Format(time.Layout)) // 01/02 03:04:05PM '06 -0700
fmt.Println(now.Format(time.ANSIC)) // Mon Jan _2 15:04:05 2006
fmt.Println(now.Format(time.UnixDate)) // Mon Jan _2 15:04:05 MST 2006
fmt.Println(now.Format(time.RubyDate)) // Mon Jan 02 15:04:05 -0700 2006
fmt.Println(now.Format(time.RFC822)) // 02 Jan 06 15:04 MST
fmt.Println(now.Format(time.RFC850)) // Monday, 02-Jan-06 15:04:05 MST
fmt.Println(now.Format(time.Kitchen)) // 3:04PM
fmt.Println(now.Format(time.Stamp)) // Jan _2 15:04:05
Go playground
I hope this is obvious to someone. I have only had a vanilla use of GLPK/MathProg.
I am having trouble figuring out the syntax in GNU MathProg (within GLPK) to do the following, for example:
set PartsOfWeek;
set WeekDays;
data;
set PartsOfWeek := WorkWeek WeekEnd;
set WorkWeek := Mon Tue Wed Thu Fri;
set WeekEnd := Sat Sun;
set WeekDays := setof{d in (WorkWeek union WeekEnd)}(d);
The problem is that this is rejected by MathProg.
In general, I just want to be able to:
- declare a Partition (here PartsOfWeek) and a set (here Weekdays)
- build the partition from data
- populate the set with the elements of the of the sets from the partition.
A better example might be with seasons and months.
with #ALi's literature reference help:
set seasons;
set months;
set monthsOfseason {seasons} within months;
data;
set seasons := winter spring summer fall;
set months := jan feb mar apr may jun jul aug sep oct nov dec;
set monthsOfseason[winter] := dec jan feb;
set monthsOfseason[spring] := mar apr may;
set monthsOfseason[summer] := jun jul aug;
set monthsOfseason[fall] := sep oct nov;