Format POSIX in R (quantstrat) - r

I'm working on extracting a date from a variable: "curIndex."
Here's what the code looks likes
show(txntime1 <- timestamp(mktdata[curIndex+1L])[,1])
show(txntime <- strftime(txntime1, '%Y-%m-%d %H:%M:%OS6'))
And the output is this:
"##------ Tue Mar 08 14:31:58 2016 ------##"
"NULL"
I'm working within ruleOrderProc of the quantstrat package.
The order time needs to be POSIXlt for the order book. Does anyone know what to do with this funky date format that I'm getting?
If so, thanks!

When all else fails, read the documentation. ;-) ?timestamp says:
The timestamp function writes a timestamp (or other message)
into the history and echos it to the console. On platforms that
do not support a history mechanism only the console message is
printed.
You probably meant to call time or index. Also, the time needs to be POSIXct for the order book, not POSIXlt.

Related

How to change UK date format in LogicApp

Im trying to convert a U.K. input date (dd-MM-yyyy) to format (yyyy-MM-dd)
I tried
"#formatDateTime('15-03-2019','yyyy-MM-dd')" ==> Error
but got error:
'In function 'convertTimeZone', the value provided
for date time string '15-03-2019' was not valid. The datetime
string must match ISO 8601 format.'
How do I go about converting this input date? The input format is (dd-MM-yyyy) and cannot be changed.
I can easily convert from (MM-dd-yyyy) as shown below, but im not able to convert from (dd-MM-yyyy)
"#formatDateTime('03-15-2019','yyyy-MM-dd')" ==> OK
Date and time functions provided by azure logic app cannot recognize the timestamp in dd-MM-yyyy format.
After my research, there is no existing function that can directly solve this problem, but you can use substring and concat to deal with this problem.
The workflow of the logic app looks like this:
The expression of the formatDataTime:
formatDateTime(concat(substring(<your-date-string>,6,4),'-',substring(<your-date-string>,3,2),'-',substring(<your-date-string>,0,2)),'yyyy-MM-dd')

How to convert a local datetime to UTC with moment.js

I've found a couple of existing StackoverFlow questions on this but nothing very definite.
I have a local datetime. I want this in UTC. my local datetime does not have a 'Z' at the end or any offset information.
I first tried:
moment(mylocaldatetime).toISOString() #works fine because this method always returns time in UTC
But for consistency with other code I didn't want to use to ISOString() so I did this:
moment(mylocaldatetime).utc().format()
This seems to work fine. If the browser running this code is in UTC + 1 I get a datetime one hour less than mylocaldatetime (with an offset string if I specify that in the format). I.e. it has treated mylocaldatetime as a local time, taken account of my current time zone, and given me my local time as UTC.
However. This appears to contradict the moment.js docs which are pretty clear that:
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment(). - Notice the 'parse'.
and
Moment normally interprets input times as local times (or UTC times if moment.utc() is used).
If these doc comments were true this line:
moment(mylocaldatetime).utc().format()
should treat mylocaldatetime as if it were utc and then output this datetime in utc - no difference. No conversion. But that is not what I get.
Maybe what this line moment(mylocaldatetime).utc().format() is saying is:
create a moment object in local mode with mylocaldatetime. Then put the moment object into utc mode. So now when we format for display we output as utc. IF this is the case I think the docs could be made clearer.

moment.js and deprecated warning. timestamp to moment date object

I've read thru various posts on here in regards to similiar issues but none have solved my problem.
I manipulate the moment.js date object, and then store it as timestamp.
BUT, when I try to read in that timestamp again, I get that deprecated warning.
""Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info."
I've tried toDate(), format(), moment(myTimeStamp, 'ddd, DD MMM YYYY HH:mm:ss ZZ'); --> all generate the warning...
So, for example, my timestamp will look like this:
const timestamp = '1458586740000'
when I read that back and try to parse out the month/day/year, then the hour/min am/pm, etc... I need to get that timestamp into a moment.js object. Nothing is working for me. Any ideas.
How can I get this timestamp: '1458586740000', into a moment.js object so I can extract date date from it as I need?
EDIT: this is how I am storing the timestamp. So I would need to retrieve it from this.
let timeStamp = Moment(state[_Date])
.add({ hour: state[AMPM] === 'PM'
? +state[Hour] + 12
: state[Hour] ,
minute: state[Min] }).format('x')
The X token indicates a unix timestamp in seconds, and the x token indicates a unix millisecond timestamp (offset).
You appear to have a millisecond timestamp, so you would make a moment out of it by doing the following:
var a = moment('1458586740000', 'x')
It works without ' as well:
var a = moment(1458586740000, 'x')
You can also not specify the x and it should work:
moment(1458586740000)
Because you have a unix offset (milliseconds), not a unix timestamp (seconds), moment.unix is not what you want.
Then you can do the following:
a.format()
"2016-03-21T13:59:00-05:00"
Or you can use any of the other formatting tokens listed here to output whatever result you would like: http://momentjs.com/docs/#/displaying/format/
Based on the code you presented, I think you may be having problems because your timestamp is stored as a string (in ''). Parsing as a string causes an invalid date error, because it attempts to match ISO 8601 format and fails. Specifying that 'x' token will cause it to assume unix offset and work correctly.

momentJS UTC versus specifying the timezone in the moment constructor

Does the below 2 syntaxes are same,
moment(1456261200367, 'H:mm:ss.SSS').utc().valueOf() //1456343786120
moment(1456261200367 +0000, 'H:mm:ss.SSS Z').valueOf() //1456325786120
but as you could see if both of them coverts the given value to UTC mode then why there is a difference in the output?
Also I would like to know how a.valueOf() and b.valueOf() are same, when a.format() and b.format() are different, because moment() (moment parses and displays in local time) is different from moment.utc() (displays a moment in UTC mode)
var a = moment();
var b = moment.utc();
a.format();
b.format();
a.valueOf();
b.valueOf();
In the first part, you're using it incorrectly. You've passed numeric input which would normally be interpreted as a unix timestamp, but then you've supplied a string-based format string so the number is converted to a string. The format string here is telling moment how the input is specified, but it doesn't match what you're actually parsing.
This doesn't error though, because by default moment's parser is in "forgiving" mode. You can read more about this in the docs.
The correct way to pass a timestamp into moment is with one of these:
moment(1456261200367)
moment(1456261200367).utc()
moment.utc(1456261200367)
The last two are equivalent, but the moment.utc(timestamp) form is prefered.
With any of those, all three will have the same .valueOf(), which is just the timestamp you started with. The difference is in the mode that the moment object is in. The first one is in local mode, reflecting the time zone of the computer where it's running, while the other two are in UTC mode.
This is evident when you format the output using the format function, as with other many other functions. I believe that answers your second question as well.

Package to parse dates in Common Lisp?

I'm writing a simple web scraper in Common Lisp (SBCL) as a learning exercise, & would like to sort by date. To do this, I'll need to parse dates in the format "MM/DD/YYYY" into universal time.
I could simply tokenise the string & pass the bits into encode-universal-time, but I figure that there must be a built-in function (or popular third-party package) for date parsing. I'd greatly appreciate someone recommending one :-)
This answer is very late but the local-time library is featureful and widely used. It is based on the article The long painful history of time.
It supports :
Time and date arithmetic
ISO 8601 timestring formatted output and parsing
Reader macros to embed timestrings directly in code
Timezone handling (will read unix tzfile format)
Conversion between universal and unix time epochs
Julian date calculation
See the net-telent-date and simple-date-time libraries for Common Lisp. The former has a parse-time function you can use (see parse-time.lisp). Both are included in the QuickLisp library collection.
You could try net-telent-date, which has PARSE-TIME which I think will do what you want.
It's now 2022, and net-telent-date is on github and is also deprecated. Better to find something else.
Many implementations have a UNIX interface and, in same cases, this includes the strptime function.
Antik handles dates and times and includes date/time parsers. The result is a "timepoint" which by default is UTC (CL's "universal-time" is something different, but it can be converted to that).
I use local-time and cl-date-time-parser:
edit: and chronicity for parsing natural language dates and times.
(local-time:parse-timestring "2019-11-13T18:09:06.313650+01:00") ;; OK
(local-time:parse-timestring "2019-11-13") ;;OK
This fails with local-time by default:
(local-time:parse-timestring "2019/11/13")
but it works with Chronicity:
(chronicity:parse "2019/11/13")
#2019-11-13T00:00:00.000000+01:00
and we can set the date separator of local-time to "/":
(local-time:parse-timestring "2019/11/13" :date-separator #\/) ;; OK
There is also the time and datetime separators.
Now a format like ""Wed Nov 13 18:13:15 2019" will fail. We'll use the
cl-date-time-parser library:
(cl-date-time-parser:parse-date-time "Wed Nov 13 18:13:15 2019")
;; 3782657595
;; 0
It returns the universal time which, in turn, we can ingest with the
local-time library:
(local-time:universal-to-timestamp *)
;; #2019-11-13T19:13:15.000000+01:00

Resources