discord.py reminder command time conversion - discord

I was wondering how I can make a time conversion for a reminder command so doing 1s converts to 1 second 1m converts to 1 minute and 1h converts to 1 hour
#client.command(aliases=["reminder"])
async def remind(ctx, remindertime, *, msg):
seconds=seconds % (24 * 3600)
hour=seconds // 3600
seconds %= 3600
minutes=seconds // 60
seconds %= 60
s=seconds
h=hour
m=minutes
await asyncio.sleep(remindertime)
await ctx.send(f"{ctx.author.mention} You were going to: {msg} and asked me to remind you.")

You can use regular expressions to check string is a time string:
import re
TIME_REGEX = re.compile("([0-9]+)(d|h|m|s)?")
MULTIPLER = {
"d": 86400,
"h": 3600,
"m": 60,
"s": 1,
"None": 1 #if they type "30", this will be 30 seconds"
}
match = TIME_REGEX.match(time_string)
if not match:
return await ctx.send("Please enter a valid time.")
seconds = int(match.groups()[0]) * MULTIPLER.get(str(match.groups()[1]))

The way you are trying to do here is a difficult way. This would do nothing but consume time and confuse you.
The way it is done usually is extremely easy.
You create a dictionary and then add the time conversion values in there. Then create a variable which converts time as per the requirement. Don't Worry I would be explaining it just now. It would clear your doubt. :)
Step 1: Create the function.
This would be the reminder function.
#client.command(aliases=["reminder"])
async def remind(ctx, time, *, msg):
Step 2: Create a time dictionary.
This dictionary will play the major role in conversion of the time for the execution. The KEYS of this dictionary will be the time unit and the VALUES will be the number in seconds.
time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400}
This will be used to convert our time to seconds and use in the sleep() function.
Step 3: Create a time convert variable.
This variable will be the time in seconds according to the value entered in the command.
remindertime = int(time[0]) * time_conversion[time[-1]]
This first takes the number value attached to the time input and multiply it with the integer VALUE from the dictionary.
Step 4: Use this time in the command.
Now when we use this time, our command would look like:
#client.command(aliases=["reminder"])
async def remind(ctx, time, *, msg):
time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400}
remindertime = int(time[0]) * time_conversion[time[-1]]
await asyncio.sleep(remindertime)
await ctx.send(f"{ctx.author.mention} You were going to: {msg} and asked me to remind you.")
So, this was the way you could actually use the time in the command.
I was glad to help. If you still have any difficulty in anything I explained, please free to ask me in the comments. :)
Thank You! :D

Related

3 byte array-for time format {Hour}{Minute}{Second}

I am reading system time(Register: TM11), and I want to get minute data from system time.
system time is in this data format = 3 bytes:{Hour}{Minute}{Second}
I am not sure, how to extract "minute" data, using array format, C code as below.
In my C code below, i use read_register function for reading system time, and use pointer (byte*)&systime[1]) to extract "minute". -not sure this is correct way to do so.
let's say, time now is 07:48:29 AM then, TM11 will show 07,48,29
I want to extract "48", which is minute, from TM11.
time interval: 15 minute.
Time Passed = 48 % 15 = 3 minute.
Putting this calculation in the C,
byte systime[2];
//declare "systime" variable as 3 byte array and to store TM11 //
byte time_interval = 15; //time interval is 15 minute
read_register (TM11,(byte*)&systime[1]);
//let's say read data value of TM11 is, 07:48:29 AM
//"read_register"function is to read the value of TM11 register
//(byte*)&systime[1] =try to point "minute" in TM11 register, systime[1]=48
//I am not sure whether hour will store in systime[0], minute will store in systime[1],//
elaps_time = systime[1] % time_interval;
//elapsed time calculation = 48 % 15 = 3

GMT subtraction on MATLAB

I'm currently working on a small project on handling time difference on MATLAB. I have two input files; Time_in and Time_out. The two files contain arrays of time in the format e.g 2315 (GMT - Hours and Minute)
I've read both Time_in' and 'Time_out on MATLAB but I don't know how to perform the subtraction. Also, I want the corresponding answers to be in minutes domain only e.g (2hrs 30mins = 150minutes)
this is one of several possible solutions:
First, you should convert your time strings to a MATLAB serial date number. If you've done this, you can do your calculation as you want:
% input time as string
time_in = '2115';
time_out = '2345';
% read the input time as datenum
dTime_in = datenum(time_in,'HHMM');
dTime_out = datenum(time_out,'HHMM');
% subtract to get the time difference
timeDiff = abs(dTime_out - dTime_in);
% Get the minutes of the time difference
timeout = timeDiff * 24 * 60;
Furthermore, to calculate the time differences correctly you also should put some information about the date in your time vector, in order to calculate the correct time around midnight.
If you need further information about the function datenum you should read the following part of the MATLAB documentation:
https://de.mathworks.com/help/matlab/ref/datenum.html
Any questions?
In a recent version of MATLAB, you could use textscan together with datetime and duration data types to do this.
% read the first file
fh1 = fopen('Time_in');
d1 = textscan(fh1, '%{HHmm}D');
fclose(fh1);
fh2 = fopen('Time_out');
d2 = textscan(fh2, '%{HHmm}D');
fclose(fh2);
Note the format specifier '%{HHmm}D' tells MATLAB to read the 4-digit string into a datetime array.
d1 and d2 are now cell arrays where the only element is a datetime vector. You can subtract these, and then use the minutes function to find the number of minutes.
result = minutes(d2{1} - d1{1})

MSSQL - Extract second from time

I'm new to Microsoft SQL Server. And I wanted to extract second from the time datatype.
I've got experience in PostgreSQL and I extract the second the using this function.
EXTRACT(EPOCH FROM timestamp)
But in Microsoft SQL Server, I found this function -
DATEDIFF(second, 0, timestamp)
Which gives me the second if the time is less the 24 hours.
But when I try a query like this.
SELECT SUM(DATEDIFF(second, '0:00:00', '86:01:12'))
It gives me a error that.
Conversion failed when converting date and/or time from character string
Searching for a solution from Saturday and couldn't find it.
Some one help me out, how can I convert the time datatype which is greater then 24 hours to seconds.
Thanks in advance.
Maybe not the best way, but you can basically explode it to hours, minutes and seconds, then add them all up. If I understand right what you wish to do is to convert a time format to seconds.
This works on 2008 R2
DECLARE #inputTime AS VARCHAR(10) = '86:01:12'
DECLARE #timeSec AS INT = SUBSTRING(#inputTime,1,2) * 3600 + SUBSTRING(#inputTime,4,2) * 60 + SUBSTRING(#inputTime,7,2)
PRINT #timeSec
You can do this as a one-liner, to nest it in any of your queries:
SELECT SUBSTRING('86:01:12',1,2) * 3600 + SUBSTRING('86:01:12',4,2) * 60 + SUBSTRING('86:01:12',7,2)
As you can see it will treat it as a String, get the first two chars as hours, multiply by 3600 (60 seconds in a minute * 60 minutes in an hour). Next take the minutes and multiply them by 60 (60 seconds in a minute), and last add the seconds up, which is * 1 since that is the resolution you are looking for.
For easier use, you can create a function, and use that onwards to make your code less messy:
CREATE FUNCTION dbo.stampSeconds (#inputTime VARCHAR(10))
RETURNS INT
AS BEGIN
DECLARE #timeSec AS INT = SUBSTRING(#inputTime,1,2) * 3600 + SUBSTRING(#inputTime,4,2) * 60 + SUBSTRING(#inputTime,7,2)
RETURN #timeSec
END
Which you can then use like:
SELECT dbo.stampSeconds ('29:07:01')

How to accept time from the user and store them into an array in matlab?

I want to accept (say)3 time elements (for example 8:30, 8:20 & 8:00) from user and store it in an array using 'datenum'. How can i achieve that? Please help.
Assuming that you just want to prompt the user given the current day and year and you only want the current time (hours and minutes - seconds is 0), you can do the following:
dateNumArray = []; %// Store datenums here
%// Enter a blank line to quit this loop
while true
timestr = input('Enter a time: ', 's');
if (isempty(timestr))
break;
end
%// Split the string up at the ':'
%//splitStr = strsplit(timestr, ':'); %// For MATLAB R2012 and up
splitStr = regexp(timestr, ':', 'split');
%// Read in the current date as a vector
%// Format of: [Year, Month, Day, Hour, Minute, Second]
timeRead = clock;
%// Replace hours and minutes with user prompt
%// Zero the seconds
timeRead(4:6) = [str2num(splitStr{1}) str2num(splitStr{2}) 0];
%// Convert to datenum format
dateNumArray = [dateNumArray datenum(timeRead)];
end
What the above code does is that we will keep looping for user input, where the time is expected to be in HH:MM format. Note that I did not perform error checking, so it is expected that HH is between 0-23 while MM is between 0-59. You keep putting in numbers by pushing in ENTER or RETURN for each entry. It parses this as a string, splits the string up at the : character, and converts each part before and after the : character into a number. We then get the current time when each hour and minute was recorded using the clock command. This is in a 6 element vector where the year, the month, the day, the hour, the minute and the second are recorded. We simply replace the hour and minute with what we read in from the user, and zero the seconds. We finally use this vector and append this to a dateNumArray variable where each time the user writes in a time, we will append a datenum number into this array.
When you call this, here is an example scenario:
Enter a time: 8:30
Enter a time: 8:45
Enter a time: 8:00
Enter a time:
Here's the example output from above:
format bank %// Show whole numbers and little precision
dateNumArray
format %// Reset format
dateNumArray =
735778.35 735778.36 735778.33

How to generate a specific time of clock in "C" programming [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
For example, the following are sample runs of the program:
Enter the hour: 5
Enter the minute: 23
Enter A (for AM) or P (for PM): A
Enter how many minutes to display: 5
The new time is
5:24 AM
5:25 AM
5:26 AM
5:27 AM
5:28 AM
Enter the hour: 11
Enter the minute: 57
Enter A (for AM) or P (for PM): P
Enter how many minutes to display: 4
The new time is
11:58 PM
11:59 PM
12:00 AM
12:01 AM
Enter the hour: 12
Enter the minute: 55
Enter A (for AM) or P (for PM): P
Enter how many minutes to display: 7
The new time is
12:56 PM
12:57 PM
12:58 PM
12:59 PM
1:00 PM
1:01 PM
1:02 PM
Also, I'm not allowed to use the folling statements:
• break; (except when used in a switch() statement)
• continue;
• exit();
• abort();
• goto
I will preface this by answer by stating that using the in-built C time functions is overkill for this problem, which is unfortunately the kind of intuition that you only gain from experience.
From the problem statement, you can deduce that you require a "time" value that can represent 24 hours worth of time, at one minute granularity. The operations required on this time value are:
Set the value to a time provided by the user, as an "hour" value, a "minute" value, and an "AM/PM" value;
Output the value in the format "11:58 PM";
Add a minute to the time value.
You now have to decide how you're going to represent the "time" value in C. One option is to use two integers (representing the hour and the minute) and a boolean (representing AM or PM). When you use multiple values to represent a single logical value, it's conventional to wrap those into a struct, so our "time" type might look like:
struct time_of_day {
int hour; /* From 1 to 12 */
int minute; /* From 0 to 60 */
int is_pm; /* 0 or 1 */
};
(In this case we've followed the convention that an int is used to store a boolean value).
You now have to figure out how to implement the three operations above using this representation of a time. With this representation, the first operation Set the value to a time provided by the user becomes very easy: you simply need to check that the hour and minute values provided are in the correct range, then store them directly in the hour and minute members of the struct time_of_day. You then need to set the is_pm value to 0 if the user entered "AM", or 1 if the user entered "PM".
The second operation Output the value is also quite simple: you can directly use the printf(), if you know these hints:
The printf format specifier %.2d will print an integer padded to two places;
The expression is_pm ? "PM" : "AM" will evaluate to "PM if is_pm is true, and "AM" if it is not.
The third operation, Adding a minute to the time value, can be broken down like this:
Add 1 to the minute part of the time;
If the minute part is now less than 60, stop;
Set the minute part to 0 and add 1 to the hour part of the time;
If the hour part is now less than 12, stop;
If the hour part is now 12, change from AM to PM or PM to AM and stop;
Set the hour part to 1.
An additional hint for this part:
The expression is_pm = !is_pm will change is_pm from 0 to 1 and 1 to 0.
I'd say the simplest method is to use strftime(), see MSDN details. You'll have to manually do the multiple time outputs, but it's a simple enough process once you've filled in the tm struct.

Resources