math.ceil groovy script return value - groovy

I am trying to generate a random date between two dates, could you check the below code?
What i notice is the output as : 02.0/01.0/1918.0
How can i save it as 02/02/1918 instead of 02.0/01.0/1918.0
var dob;
//set a range of years
var min = 1900;
var max = 2004;
// Math.ceil prevents the value from being 0;
var month = Math.ceil(Math.random() * 12);
var day = Math.ceil(Math.random() * 28);
var year = Math.floor(Math.random() * (max - min) + min);
//this ensures that the format will stay mm/dd/yyyy;
if(month < 10) {
month = "0" + month;
}
if(day < 10) {
day = "0" + day;
}
//concatenates random dob in mm/dd/yyyy format;
dob = month + "/" + day + "/" + year;
return dob;

Math.floor returns double. You have to convert it to int.
var year = Math.floor(...) as int

Related

I am trying to get the time difference between the execution of 2 commands, one is !on and the other !off (Discord.JS))

I have the following code. It works to an extent, but when I do the !off command, it shows me a 0 instead of the actual time difference.
Index Code
global.time = getDateTime()
global.oldTime = getNewDateTime().getTime
global.finalTime = parseFloat(time) - parseFloat(oldTime)
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour + ":" + min;
}``
console.log(parseFloat(getDateTime))
function getNewDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour + ":" + min;
}
And here is the off command, same with on almost
exports.run = async (client, message, args, tools) => {
const embed = new Discord.MessageEmbed()
.setColor("BLUE")
.addField("Timer ended. Total Time Online", `**: ${finalTime}**`)
message.channel.send(embed);
}

Generating bulk dates

I wanted to ask if anyone knows a way of generating bulk dates. I want to know about how can I generate all of the dates and times between 20130631_0000 till 20151231_2359
The month limit is 0-12
The day limit is 0-31
and the part you see after underscore is hour and minute, the hour limit is 00 till 23 and the minute is from 00 till 59. It's basically YYYYMMDD_HHMM
Few examples:
20130810_1154, 20140103_2357, 20150722_1049, 20140103_2358, 20140103_2359, 20140104_0000, 20140104_0001, 20140105_1159, 20140105_1200
If you simply want to print each date to the console:
let date = new Date(2013,6,31,0,0);
const endDate = new Date(2015,12,31,23,59);
const padWithZero = num => num.toString().padStart(2, '0');
while (date <= endDate) {
const year = date.getFullYear();
const month = padWithZero(date.getMonth()+1);
const day = padWithZero(date.getDate());
const hour = padWithZero(date.getHours());
const minute = padWithZero(date.getMinutes());
console.log(`${year}${month}${day}_${hour}${minute}`)
date = new Date(date.getTime() + 60000)
}
Try this
function convertStringToDate(date){
var year = date.substring(0,4)
var month = date.substring(4,6)
var day = date.substring(6,8)
var hour = date.substring(9,11)
var minute = date.substring(11,13)
return new Date(parseInt(year), parseInt(month), parseInt(day), parseInt(hour), parseInt(minute) )
}
function addMinute(date){
var new_date = date;
new_date.setMinutes(date.getMinutes()+1)
return new_date
}
function getDatesBetween(start_date_str, end_date_str){
var start_date = convertStringToDate(start_date_str)
var end_date = convertStringToDate(end_date_str)
dates = []
date = start_date
while(date.getTime() !== end_date.getTime()){
dates.push(new Date(date))
date = addMinute(date)
}
dates.push(end_date)
return dates
}
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
function convertDateToString(date){
var year = date.getFullYear().toString()
var month = pad(date.getMonth().toString(),2)
var day = pad(date.getDay().toString(),2)
var hour = pad(date.getHours().toString(),2)
var minutes = pad(date.getMinutes().toString(),2)
return year + month + day + "_" + hour + minutes
}
var start_date_str = "20130620_1224"
var end_date_str = "20130621 1224"
dates = getDatesBetween(start_date_str, end_date_str)
formatted_dates = dates.map(convertDateToString)
console.log(dates.length)
console.log(dates[dates.length-1])
console.log(formatted_dates.length)
console.log(formatted_dates[dates.length-1])

convert my string to AM or PM hour in ActionScript 3

I've got a String variable for my time :
var $myTime:String;
//Some calculation
trace(myTime);
// Result is : 14:25 (for example)
I'm looking for a way to convert this string ("14:25" in this example) to a simple AM / PM format.
So in this example it would be, for example, 2 PM
Any idea how I can simply do that ?
You can use DateFormatter class to set your wished pattern.
You can write your code as follow:
var df:DateFormatter = new DateFormatter();
df.formatString = "YYYY-MM-DD L:NN:SS";
df.format(myTime);
Where L is HOUR with PM/AM; NN are the minutes SS seconds.
You can see the complete guide, about DateFormatter pattern, here
var myTime:Date = new Date();
var timeString:String;
if (myTime.getHours() > 12)
{
timeString = String((myTime.getHours() - 12) + ":" + myTime.getMinutes() + " PM");
}
if (myTime.getHours() == 12)
{
timeString = String(myTime.getHours() + ":" + myTime.getMinutes() + " PM");
}
if (myTime.getHours() < 12)
{
timeString = String(myTime.getHours() + ":" + myTime.getMinutes() + " AM");
}
trace(timeString);
That should do the trick.

Instantiate tuple value in Cassandra UDA function with map and tuple value (for daily average)

I am trying to create a function which counts and sums values by day (to later calculate the average). I got this far:
CREATE OR REPLACE FUNCTION state_group_count_and_sum( state map<timestamp, frozen<tuple<bigint,double>>>, timestamp timestamp, value double )
CALLED ON NULL INPUT
RETURNS map<timestamp, frozen<tuple<bigint,double>>>
LANGUAGE java AS '
Date date = (Date) timestamp;
Calendar cal = Calendar.getInstance(); // locale-specific
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
TupleValue tupleValue = state.get(date);
Long count = (Long) tupleValue.getLong(0);
if (count == null) count = 1L;
else count = count + 1L;
Double sum = (Double) tupleValue.getDouble(1);
if (sum == null) sum = value;
else sum = sum + value;
//if (tupleValue == null) ?
tupleValue.setLong(0, count);
tupleValue.setDouble(1, sum);
state.put(date, tupleValue);
return state; ' ;
CREATE OR REPLACE AGGREGATE group_count_and_sum(timestamp, double)
SFUNC state_group_count_and_sum
STYPE map<timestamp, frozen<tuple<bigint,double>>>
INITCOND {};
This fails because tupleValue is null at every new day which is not in the map yet. How do I instantiate a tuple value in a UDA?
Fixed it.
CREATE OR REPLACE FUNCTION state_group_count_and_sum( state map<timestamp, frozen<tuple<bigint,double>>>, timestamp timestamp, value double )
CALLED ON NULL INPUT
RETURNS map<timestamp, frozen<tuple<bigint,double>>>
LANGUAGE java AS '
Date date = (Date) timestamp;
Calendar cal = Calendar.getInstance(); // locale-specific
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
TupleValue tupleValue = state.get(date);
if (tupleValue == null) {
com.datastax.driver.core.TupleType tupleType = com.datastax.driver.core.TupleType.of(com.datastax.driver.core.ProtocolVersion.NEWEST_SUPPORTED, com.datastax.driver.core.CodecRegistry.DEFAULT_INSTANCE, com.datastax.driver.core.DataType.bigint(), com.datastax.driver.core.DataType.cdouble());
tupleValue = tupleType.newValue(0L, 0.0);
}
Long count = (Long) tupleValue.getLong(0);
if (count == null) count = 1L;
else count = count + 1L;
Double sum = (Double) tupleValue.getDouble(1);
if (sum == null) sum = value;
else sum = sum + value;
tupleValue.setLong(0, count);
tupleValue.setDouble(1, sum);
state.put(date, tupleValue);
return state; ' ;
CREATE OR REPLACE AGGREGATE group_count_and_sum(timestamp, double)
SFUNC state_group_count_and_sum
STYPE map<timestamp, frozen<tuple<bigint,double>>>
INITCOND {};

Trying to calculate the end of the next day in node.js

I'm trying to calculate the end of the next day for any given date as an expiry date.
So if the input is eg 10/26/2013 16:36:46 then I'd like to get 10/27/2013 23:59:59 and 7/31/2013 16:36:46 would give me 8/1/2013 23:59:59
It gets obviously difficult with leap years and when the next day is a new month. So I tried to use the dateParse function which gives me a number that I can easily add 86400000 (full day in milliseconds) and then I want to use basically the reverse of dateParse (turn a number into a date object) so that I can then take out the date component and replace the time component with 23:59:59
Here's my code which doesn't work - setTime doesn't seem to be the right function and makes my next_day variable "undefined"
var next_day = call_start.dateParse + 86400000; // adding a full day to it
next_day = next_day.setTime;
var day = next_day.getDate;
day = (day < 10 ? "0" : "") + day;
var month = next_day.getMonth + 1;
month = (month < 10 ? "0" : "") + month;
var year = next_day.getFullYear;
var hour = 23;
var min = 59;
var sec = 59;
var expiry = year + ":" + month + ":" + day + ":" + hour + ":" + min + ":" + sec;
console.log ('Streak expires at: ' + expiry);
You can use the set* functions to modify the date:
var date = new Date('7/31/2013 16:36:46');
date.setDate(date.getDate() + 1);
date.setHours(23);
date.setMinutes(59);
date.setSeconds(59);
console.log(date);
// Thu Aug 01 2013 23:59:59 GMT+0200 (CEST)

Resources