-
August 3rd, 2019, 04:40 AM
#1
[RESOLVED] Node.js Discord Bot development - how can I parse a custom time string?
Hi. I am making a Discord chat bot and I need to parse a time duration string in this format:
Exactly in this order. The seconds modifier does not need to be used (default time unit), and the specified time can omit any of the unit counts (including the unit), which renders that unit zero and is not used.
I have following structure for the parsed time prepared:
Code:
let duration = {
years: 0,
months: 0,
weeks: 0,
days: 0,
hours: 0,
minutes: 0,
seconds: 0
};
and the constants to mupltiply the specified time to milliseconds:
Code:
module.exports.time_constants = {
year: 3.1556926e10,
month: 2.62974383e9,
week: 604800000,
day: 86400000,
hour: 3600000,
minute: 60000,
second: 1000
};
The question is - do you know the most efficient way to parse the given time string? I'd like to avoid using ANTLR, because I don't quite understand how to use it and can't find any simplified information on how to use it, what each part of the generated code actually does and how to use that for own bussiness.
But better be safe than sorry, so I also prepared a gramar file for the time specification:
Code:
grammar time;
fragment INT:
[1-9] [0-9]*
;
fragment Y: [yY];
fragment M: [mM];
fragment W: [wW];
fragment D: [dD];
fragment H: [hH];
fragment I: [iI];
fragment N: [nN];
fragment S: [sS];
time:
years? months? weeks? days? hours? minutes? seconds?
// #y#m#w#d#h#min#s
;
years: (INT Y)?;
months: (INT M)?;
weeks: (INT W)?;
days: (INT D)?;
hours: (INT H)?;
minutes: (INT M I N)?;
seconds: (INT S?)?;
The issue with it is that "m" also matches "min" when I use regex. Please, will someone help me?
-
August 4th, 2019, 01:41 PM
#2
Re: Node.js Discord Bot development - how can I parse a custom time string?
Nevermind, I did it using a simple regex:
Code:
let timeRegex = /^(?<y>[1-9][0-9]*y)?(?<m>[1-9][0-9]*m)?(?<w>[1-9][0-9]*w)?(?<d>[1-9][0-9]*d)?(?<h>[1-9][0-9]*h)?(?<min>[1-9][0-9]*min)?(?<s>[1-9][0-9]*s?)?$/i;
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|