Relative date value user defined variables - datetime

How can I create a user defined variable that has a relative date e.g. current date plus 1 year?
I have created a user define variable startDate
I have tried adding the code
LocalDate.now().plusYears(1).toString();
in multiple paces but I just can't get the variable value to be set by the code.

Use __timeShift function:
${__timeShift(dd/MM/yyyy,${startDate},P1D,,)}
The timeShift function returns a date in the given format with the specified amount of seconds, minutes, hours, days or months added

You need to include the java.time package into your expression
The correct syntax for the __groovy() function would be:
${__groovy(java.time.LocalDate.now().plusYears(1).toString(),startDate)}
Demo:
More information: Creating and Testing Dates in JMeter - Learn How

Related

What is the best way to convert character time variables from multiple files when timezone is unknown?

Lets say I have 2 files, each with a date column, e.g. 2022-11-14 and a timestamp, e.g. 18:36 column. No prior information is given on timezone this information was taken from. In my code, I create a new column, date_time_X corresponding to the file # where I concatenate date and time paste(as.character(date), timestamp).
I've discovered that base R's POSIXct default timezone is system-specific while lubridate's ymd_hds timezone defaults to UTC, so now I'm defaulting to applying ymd_hds(paste(as.character(date), timestamp)) across both files to keep consistent timezones, since I will be subtracting date_time_1 from date_time_2 to get a time difference diff_time = date_time_2 - date_time_1 .
My question is, what is the best approach to handle datetime variables from these different files when timezone is unknown? Should data be read in as UTC? Is there a way to completely remove the timezone component? I don't want to assume the data was collected at my local time, but I'm also not sure if defaulting to UTC is an acceptable approach. Ideally, removing the timezone would be the best option, but I'm not sure if this is possible without leaving the variables as character columns.

Moment js diff with different timezone

I want to calculate the difference of time in hours between 2 different time zones using the moment.js library.
I tried using diff function provided by the library but it is not providing correct answers, I tried:
moment('2016-12-18 6:00:00', 'YYYY-MM-DD HH:mm:ss')
.diff(moment('2016-12-18 6:00:00', 'YYYY-MM-DD HH:mm:ss').utcOffset('+09:30'), 'hours')
I am in UTC+5:30 and the moment(string, string) constructor returns the time in my current timezone. And I was expecting the answer to be 4 instead got 0. Is there any other function that moment library provides to get difference amongst timezones ?
As long as you are just using fixed time zone offsets, your above code will work if you make one adjustment:
.utcOffset('+09:30', true)
The second parameter tells the function to retain the date and time value the moment already has when the offset is set. The default (false) will shift the date and time from its current offset (your local time) to the offset provided.
If you are not working with fixed offsets, then you will indeed need to use the moment-timezone add-on.

Using Dates in SQLite

I have a TEXT column called "time" in a table meal and in a table pain which is TEXT formatted as YYYY-MM-DDTHH:MM. I'm trying to search for other times that are within 12 hours of a given time, although I can't figure out how to do that.
I've tried testing
WHERE pain.time < meal.time + "1:00" AND pain.time > meal.time
but this approach alters the year instead of the hour. I also tried testing the same query adding "0000-00-00T01:00", but it doesn't seem to do anything.
I'm not sure what else to test.
SQLite has no built-in date/time data type, so you have to use either numbers or strings and handle them correctly.
To do calculations, you have to either do them directly on the numerical value (which might require conversions into a number and back), or use the modifiers of the built-in date/time functions:
... WHERE meal.time BETWEEN datetime(pain.time, '-12 hours') AND pain.time

GetOptionChain from quantmod Package, how do you pull a full days worth of call/put options for past dates

I am trying to pull 2 days worth of call/put option on AAPl.
I am using this command to save it as a csv, but I just need 2 current days worth of information:
AAPL.csv <- getOptionChain("AAPL")
I have tried to merge commands from a different function but it doesn't seem to work. Code below:
getOptionChain("AAPL",what=yahooQF(c("Bid","Ask")),from = as.Date("2013-01-30"), to = as.Date("2013-01-31"), from EOD_time = "9:30:00", to EOD_time = "15:00:00")
Any suggestions on how to just pull 2 days worth of option and equity data?
AFAIK yahoo doesn't provide historical data for options. If it does, getOptionChain isn't intended to be used that way.
getOptionChain is more like getQuote than getSymbols. It gets quotes for an option chain. You can get quotes for multiple expirations and strikes, but you can't get a time series of prices.
The Value section of help("getOptionChain") tells you what the function returns:
A named list containing two data.frames, one for calls and one for puts. If more than one expiration was requested, this two-element list will be contained within list of length length(Exp). Each element of this list will be named with the expiration month and year (for Yahoo sourced data).
If Exp is set to NULL, all expirations will be returned. Not explicitly setting will only return the front month.

Index xts using string and return only observations at that exact time

I have an xts time series in R and am using the very handy function to subset the time series based on a string, for example
time_series["17/06/2006 12:00:00"]
This will return the nearest observation to that date/time - which is very handy in many situations. However, in this particular situation I only want to return the elements of the time series which are at that exact time. Is there a way to do this in xts using a nice date/time string like this?
In a more general case (I don't have this problem immediately now, but suspect I may run into it soon) - is it possible to extract the closest observation within a certain period of time? For example, the closest observation to the given date/time, assuming it is within 10 minutes of the given date/time - otherwise just discard that observation.
I suspect this more general case may require me writing a function to do this - which I am happy to do - I just wanted to check whether the more specific case (or the general case) was already catered for in xts.
AFAIK, the only way to do this is to use a subset that begins at the time you're interested in, then get the first observation of that.
e.g.
first(time_series["2006-06-17 12:00:00/2006-06-17 12:01"])
or, more generally, to get the 12:00 price every day, you can subset down to 1 minute of each day, then split by days and extract the first observation of each.
do.call(rbind, lapply(split(time_series["T12:00:00/T12:01"],'days'), first))
Here's a thread where Jeff (the xts author) contemplates adding the functionality you want
http://r.789695.n4.nabble.com/Find-first-trade-of-day-in-xts-object-td3598441.html#a3599887

Resources