How to get Java 8 hour as parameter? - java-8

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

Related

Discord API - get messages before a date from a channel

I want to implement a command in my bot, to allow fetching messages from a channel and process them further.
I tried to look for how to fetch messages from a specific channel, but could not find an explicit answer to it, except most of the forum pages simply refer to the API page, with some saying trying the discord.utils.get method.
I am wondering if it would be something like this, but I could not find the matching methods/attributes that I can use to do this:
#bot.command(name='fetchmessages', help='....')
async def fetchmessages(ctx, from_channel_id:str, from_date:str):
#check if the channels already exist
channel = discord.utils.get(ctx.guild.channels, id=from_channel_id)
from_date = .... #code to parse the string to a date object
discord.utils.get(channel.messages, # Q1
from=from_date) # Q2
####
However, I cannot really do this because
Q1: assuming the channel is a TextChannel link, the class does not have an attribute that gives me all the messages from that channel, but only last_message. While TextChannel has a method fetch_message but that only gets a single message with an ID parameter
Q2: I am not sure if I can pass a date object like this, because the API for discord.utils.get does not explicitly say what attributes (**attrs) can be specified.
Any hints much appreciated.
Thanks
You are right that you can't use fetch_message because it only returns a single message, but you can use channel.history (link can be found here)
from datetime import datetime
#bot.command()
async def fetchmessages(ctx, from_channel_id:str, from_date:str):
channel = client.get_channel(int(from_channel_id))
date = datetime.strptime(from_date, '%b %d %Y %I:%M%p')
messages = await ctx.channel.history(limit=200, after=date).flatten()
#To do something with messages
Referenced this question regarding string to datetime object.
Q1: you can use the channel.history() function but for that, you would have to have the read_message_history permissions to get access to those messages
Q2: as you can read in discord.utils.get "To have a nested attribute search (i.e. search by x.y) then pass in x__y as the keyword argument." you can access whatever variable you want but instead of using '.' you need to use '_'
so for example
discord.utils.get(channel.messages,created_at=from_date)
another thing that I just want to point out is the date attribute you're trying to get by has to be a datetime.datetime object

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

Formatting a Go date in a bash-friendly manner

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}}

Why does Ruby Time.at() only return 01/01/2000 inside Heroku?

I've got a tiny Sinatra app running on Heroku, and it has a method that receives a POST and creates a database object with the parameters of the POST.
One of those params is a timestamp of the form "1432565475.000007". I want that as a date & time in the database, so I added a column, then in the method that handles the post I have:
e.time = Time.at params["timestamp"].to_f
The times come out correctly, but the dates are all Jan 1, 2000.
If I run irb at heroku ("heroku run irb") and try the above line of code manually it converts correctly. It's only when it's running inside my server instance that it interprets the calendar date wrong.
So then I created a view that just iterates through the db, converting the original timestamp (I also have that in a column) into dates. The idea being that this is more closely approximating the code that handles the POST:
-MyEvent.all.each do |me|
%p
=Time.at me.timestamp.to_i
And that works perfectly.
Any ideas?

PersistentLocalDateTimeAsString results in DateTimeParseException

In the hibernate model when using jadira-usertype org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString as Type, the conversion is not reversible.
#Column(name="requested_start")
#Type(type="org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString")
private LocalDateTime requestedStartDate;
When creating object with datetime in following format;
myObject.setRequestedStartDate(LocalDateTime.parse("2014-12-28T19:30:00"));
is stored in DB (MariaDB) as "2014-12-28T19:30" ignoring the seconds part (don't know why).
when querying back the data, I'm getting the following exception
java.time.format.DateTimeParseException: Text '2014-12-28T19:30' could not be parsed at index 16
But, If I set date as "2014-12-28T19:30:01" with seconds set to "01", it is working fine.
I also tried setting the springframework's DateTimeFormat, still facing the same exception.
#Column(name="requested_start")
#Type(type="org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString")
#DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime requestedStartDate;
You can try this modified pattern "yyyy-MM-dd'T'HH:mm[:ss]".
The meaning of the square brackets is indicating an optional part during parsing - see javadoc.

Resources