Formatting a Go date in a bash-friendly manner - bash

Using buildah, I can find out the date my image was built at with this call:
buildah images --format '{{.CreatedAt}}' my_image
The --format argument is a Go template, as described for a related command.
This returns:
Nov 13, 2018 08:04
As far as I can tell that is my current timezone it uses, but it's not localised, and it's missing timezone information. If I feed the output into Linux's date like so:
date -d "`buildah images --format '{{.CreatedAt}}' my_container`" +%s
This gives me what I want, UNIX epoch seconds:
1542063840
However, since my '{{.CreatedAt}}' is a Go template that I should be able to format, how can I directly print out epoch seconds (or RFC-3339, etc) rather than relying on date.
As you can guess, I am an utter Go newbie and the documentation provided nothing I could copy-paste
NOTE: Following the below answer, enhancement request posted to the buildah issues db.

Unfortunately you are out of luck.
The parameter value passed to the template execution is of type imageOutputParams, which is declared in images.go:
type imageOutputParams struct {
Tag string
ID string
Name string
Digest string
CreatedAt string
Size string
}
As you can see, the CreatedAt field is of type string, not a time.Time, so you can't call time.Time methods on it. Neither can you do any useful date/time processing on it without custom registered functions. But since you're just supplying the template text, you can't register custom functions either.
The template you pass is executed in function outputUsingTemplate() like this:
err = tmpl.Execute(os.Stdout, params)
Where params is a value of the above mentioned struct.
Recommend the project owners to add a new field holding the CreatedAt timestamp as a time.Time value, so you can get the epoch seconds using Time.Unix(). The template would look like this then:
{{.CreatedAtTime.Unix}}

Related

Reading CloudWatch log query status in go SDK v2

I'm running a CloudWatch log query through the v2 SDK for Go. I've successfully submitted the query using the StartQuery method, however I can't seem to process the results.
I've got my query ID in a variable (queryID) and am using the GetQueryResults method as follows:
results, err := svc.GetQueryResults(context.TODO(), &cloudwatchlogs.GetQueryResultsInput{QueryId: queryId,})
How do I actually read the contents? Specifically, I'm looking at the Status field. If I run the query at the command line, this comes back as a string description. According to the SDK docs, this is a bespoke type "QueryStatus", which is defined as a string with enumerated constants.
I've tried comparing to the constant names, e.g.
if results.Status == cloudwatchlogs.GetQueryResultsOutput.QueryStatus.QueryStatusComplete
but the compiler doesn't accept this. How do I either reference the constants or get to the string value itself?
The QueryStatus type is defined in the separate types package. The Go SDK services are all organised this way.
import "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
if res.Status == types.QueryStatusComplete {
fmt.Println("complete!")
}

Hyperledger Fabic 2.2.0 Error handling success response. Value did not match schema

I am using Hyperledger Fabric 2.2.0 and fabric-network 2.1 (not that important).
My chaincode is written in Go. So I have some structs which have ,omitempty in JSON tag. Here is my struct:
type LeaseDetails struct {
EndOfTerm string `json:"endOfTerm"`
Info string `json:"info,omitempty"`
Option string `json:"option,omitempty"`
}
But I am getting the following error as a return value from my chaincode:
peer=peer0.org1.example.com:7051, status=500, message=Error handling success response. Value did not match schema:
1. return.0.leaseDetails: info,omitempty is required
2. return.0.leaseDetails: option,omitempty is required
If I remove ,omitempty from my struct, and provide default value everything works fine.
In the docs for fabric-contract-api-go it is mentioned that there is some kind of serializer built upon json marshal/unmarshal, but to me it doesn't seem to detect the ,omitempty keyword.
Was this intentional? Or am I missing something here?
Thanks in advance
I got the answer on Hyperledger Chat from user #awjh.
This is as intended, the json is compared against the metadata schema.
By default all fields are required, using omitempty will mean that the
JSON process will remove that field when it has no value. This means a
required field will be missing. To fix this add a metadata tag to mark
the field as optional metadata:",optional"
So in my case, the solution is:
type LeaseDetails struct {
EndOfTerm string `json:"endOfTerm"`
Info string `json:"info,omitempty" metadata:",optional"`
Option string `json:"option,omitempty" metadata:",optional"`
}

Does anyone know what TimezoneOffset does on LuisPredictionOptions?

I'm sending LUIS a query that is based on a time value (e.g. "what is the time 10 minutes from now" - just an example). I want the time to come back in the local timezone, so on the LuisPredictionOptions object (C#) I set the TimezoneOffset (as an example I set it to 2 hours ahead, or 120 minutes).
In Fiddler I can see when it calls the LUIS endpoint it's correctly adding "timezoneOffset=120.0".
However, the timezone comes back as UTC - it doesn't matter whether the timezoneOffset is set, or even what it is set to, the time always comes back UTC, using the builtin datetimeV2 entity.
Does anyone know what the TimezoneOffset property is for? Am I just using it incorrectly? Is there another way perhaps to get a local time from LUIS?
[Update]: Here are some examples: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[AppId]?verbose=true&timezoneOffset=0&subscription-key=[subscriptionkey]&q=/luis/v2.0/apps/c1be57f4-3850-489e-8266-db376b82c011?timezoneOffset=120&log=true
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[AppId]?verbose=true&timezoneOffset=0&subscription-key=[subscriptionkey]&q=/luis/v2.0/apps/c1be57f4-3850-489e-8266-db376b82c011?timezoneOffset=240&log=true
and I'm trying the following example utterance: "in 10 minutes".
When I do this, the timex is in UTC (e.g. timex=2020-01-11T16:08:25) and the "value" comes back with the same value, minus the "T", as follows: value=2020-01-11 16:08:25
I could understand perhaps if the timex is in UTC, but then possibly "value" should be adjusted by the timezoneOffset?
It looks like there's an incorrect question mark in your URL, right before timezoneOffset.
Using the same query I was able to get the expected behavior, where the returned value is different by 10 minutes.
Which SDK are you using? Perhaps you're using the V3 Runtime SDK which uses the V3 endpoint that doesn't use timeZoneOffset but instead uses datetimeReference, and need to use the V2 Runtime SDK instead.
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[app-id]?verbose=true&timezoneOffset=10&subscription-key=[key]&q=in 10 minutes
The TimeZoneInfo class's FindSystemTimeZoneById method can be used to determine the correct timezoneOffset based on system time. An example in C# is shown below:
// Get CST zone id
TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
// Get local machine's value of Now
DateTime utcDatetime = DateTime.UtcNow;
// Get Central Standard Time value of Now
DateTime cstDatetime = TimeZoneInfo.ConvertTimeFromUtc(utcDatetime, targetZone);
// Find timezoneOffset
int timezoneOffset = (int)((cstDatetime - utcDatetime).TotalMinutes);
Reference:
https://learn.microsoft.com/en-us/azure/cognitive-services/luis/luis-concept-data-alteration?tabs=V2#c-code-determines-correct-value-of-timezoneoffset

App Engine logging.properties: customize Date format for lazy-evaluation Log

I have a very simple code
Logger logger = Logger.getLogger(HelloAppEngine.class.getName());
logger.log(Level.INFO, "This is now: {0}", new Date());
defined inside a (Maven based) Google App Engine project.
The output of this log is
giu 10, 2017 6:03:10 PM it.log.HelloAppEngine doGet
INFO: This is now: 10/06/17 18.03
with the default Date format. I want to customize the Date format with my own.
I don't really know where this configuration need to be placed, I think that can be inside the logging.properties file, but I'm unable to find the proper cofniguration
Which is the proper way to customize the Date format provided by the lazy-evaluted log message?
I found the relevant code inside the java.text.MessageFormat class.
which is triggered to format the Date object
It does not seem that is accessing to an external configuration in order to format the date, it uses a fixed DateFormat.SHORT format. Maybe there is a way to provide a custom formatter which will override that entire implementation.
I want to customize the Date format with my own.
I don't really know where this configuration need to be placed, I think that can be inside the logging.properties file, but I'm unable to find the proper configuration
You have to include the date format in the log string as described in the java.text.MessageFormat class level docs. As stated there you can use any of the java.text.SimpleDateFormat patterns.
For example, you can use the DateFormat constants:
logger.log(Level.INFO, "This is now: {0,date,long} at {0,time,full}", new Date());
Otherwise, you can use a pattern:
logger.log(Level.INFO, "This is now: {0,date,EEE, MMM dd HH:mm:ss:S ZZZ yyyy}", new Date());
If you use localization in your logging you can replace the messages with a shorter key and leave the date format patterns in your ResourceBundle.

How to get Java 8 hour as parameter?

I want to create a method that gets 24 hour format parameter for the user.
Something like: set24HourTime(LocalDateTime hourTIme).
I know I can do it simple by defining an int as a parameter, but I wondered if it can be done with java build in API?
If you have a LocalDateTime param, then the value is already in 24 hour format.
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#getHour--
just return hourTIme.getHour()

Resources