What does moment(testDate, "x") do? - momentjs

I have the following piece of code before me:
var testDate = 1481103000000;
var enterTime = moment(testDate, "x");
console.log(enterTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
The variable enterTime results in a momentjs object with an additional _f property set to "x" and a _pf property of type object (see the console log) compared to a normal moment(testDate) object.
I couldn't find information on the _f or _pf properties anywhere.
Can anyone tell me what the "x" stands for and for what reason it is being used?
Thanks in advance.

With moment(testDate, "x"); you are creating a moment object using moment(String, String); function specifying x as format (Unix ms timestamp).
When you do moment(testDate), you are creating a moment object using moment(Number);.
All moment properties starting with _ are for internal use, _f stands for format, while _pf stand for Parsing Flags.
You can have a look to moment code to get more details about _f and _pf.

x denotes Unix ms timestamp
Please note that this parameter is case-sensitive:
X Output: 1410715640.579 Unix timestamp
x Output: 1410715640579 Unix ms timestamp
Refer here for all the options.

Related

Parsing a datetime string to local time in rust chrono

Having trouble with a simple problem. I have a string that does not include timezone information that I need to parse into a DateTime struct. I can get it as UTC, but not local:
let from = NaiveDateTime::parse_from_str(&start_date, "%Y-%m-%dT%H:%M:%S")?;
let from_utc = DateTime::<Utc>::from_utc(from, Utc);
You need Local.from_local_datetime() to convert a NaiveDateTime into a DateTime<Local>:
let from: NaiveDateTime = start_date.parse().unwrap();
let date_time = Local.from_local_datetime(&from).unwrap();
Admittedly, this isn't really easy to find in the documentation.
The first line you have works fine as well. For this particular format (RFC3339), it's easier to use str::parse(), though.

Querying dates in Realm React Native

[EDITED]
I'd like to query and filter by dates in realm. Here's my code:
let factories = realm.objects('Factory')
for(factory of factories) {
let toys = realm.objects('Toy').filtered('factory_id == factory.id')
let lowerBound = new Date(year + '-1-1T00:00:00Z')
let uppperBound = new Date(year + '-2-1T00:00:00Z')
let janToys = toys.filtered('created_at > lowerBound AND created_at < uppperBound')
}
year is a variable declared before the code snippet above.
This doesn't work and I'm pretty sure it's because the date format is not correct.
Here is the date format when I log toys:
Fri Mar 24 2017 16:01:59 GMT+0800 (Malay Peninsula Standard Time)
I'd like to know how to query realm dates. I can't find it in the documentation and other posts here. I'd appreciate any help. If this is not a date format issue, please tell me.
Thank you!
EDIT: I added the outside loop. This may not be a date format issue. Here is the error message:
Error: Predicate expressions must compare a keypath and another keypath or a constant value`
It seems that Realm does not support dot operators inside its queries. Forcing Realm to be relational is not allowed. As a solution, I transformed my usage to follow NoSQL convention. Passing in objects instead of declaring relationships.

Save an Excel sheet as PDF programatically through powerbuilder

There is a requirement to save an excel sheet as a pdf file programmatically through powerbuilder (Powerbuilder 12.5.1).
I run the code below; however, I am not getting the right results. Please let me know if I should do something different.
OLEObject ole_excel;
ole_excel = create OLEObject;
IF ( ole_excel.ConnectToObject(ls_DocPath) = 0 ) THEN
ole_excel.application.activeworkbook.SaveAs(ls_DocPath,17);
ole_excel.application.activeworkbook.ExportAsFixedFormat(0,ls_DocPath);
END IF;
....... (Parsing values from excel)
DESTROY ole_excel;
I have searched through this community and others for a solution but no luck so far. I tried using two different commands that I found during this search. Both of them return a null object reference error. It would be great if someone can point me in the right direction.
It looks to me like you need to have a reference to the 'activeworkbook'. This would be of type OLEobject so the declaration would be similar to: OLEobject lole_workbook.
Then you need to set this to the active work book. Look for the VBA code on Excel (should be in the Excel help) for something like a 'getactiveworkbook' method. You would then (in PB) need to do something like
lole_workbook = ole_excel.application.activeworkbook
This gets the reference for PB to the activeworkbook. Then do you saveas and etc. like this lole_workbook.SaveAs(ls_DocPath,17)
workBook.saveAs() documentation says that saveAs() has the following parameters:
SaveAs(Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout, Local)
we need the two first params:
FileName - full path with filename and extension, for instance: c:\myfolder\file.pdf
FileFormat - predefined constant, that represents the target file format.
According to google (MS does not list pdf format constant for XLFileFormat), FileFormat for pdf is equal to 57
so, try to use the following call:
ole_excel.application.activeworkbook.SaveAs(ls_DocPath, 57);

Why "error: invalid escape sequence?"

According to the valadoc
var now = new DateTime.now(new TimeZone.local());
var timestamp = now.format("\%F.\%T");
should set timestamp to "2012-08-28.09:51:06." Why "error: invalid escape sequence" on "F" and "T?" Other formats from the valadoc cause the same error and now.to_string() is in fact "2012-08-28T09:51:06+0000"
Edit: Perhaps embedded-linux target is missing something?
Edit: The test code here prints "(null)" in this project which uses glib 2.26.1.
As NullUserException mentioned, you shouldn't be including the backslashes--that is what is causing the invalid escape sequence error.
The reason it still doesn't work after removing the backslashes is that the %T format specifier wasn't added until the 2.30 cycle. The relevant commit is 414c8ce532c19fe65deb8dfb80222d0164be5cbe
You can work around it by doing something like this instead:
var timestamp = now.format ("%F.%H:%M:%S");

Use of console.time

I'm creating an application with Google Closure Library and its Compiler. To debug values I use console.log(). Compiling this will throw the following exception JSC_UNDEFINED_VARIABLE. variable console is undeclared at .... To solve this error, I just had to use window.console.log() instead.
I also want to measure the time that a function takes. Firebug has two nice functions console.time(name) and console.timeEnd(name) to do that very easily. Unfortunately the Closure Compiler does not support these functions throwing the following warning JSC_INEXISTENT_PROPERTY. Property time never defined on Window.prototype.console at .... Unfortunately you cannot solve this warning with prepending window.
I also had a look at the library, but goog.debug.Console has not the function that I need.
Another solution I have used before was something like the following
var start = new Date();
// do something
var end = new Date();
// do some calculation to get the ms for both start and end
var startMS = ....;
var endMS = .....;
// get the difference and print it
var difference = (endMS - startMS) / 1000;
console.log('Time taken for something: ' + difference);
This is a little bit too much code, if you use it very often, and the version with the two functions would be great:
window.console.time("timeTaken");
// do something
window.console.timeEnd("timeTaken");
This prints out the MS between start and end. But as mentioned above, this doesn't work with the closure compiler. Does anyone have a solution for this, how I can use these two functions window.console.time() and window.console.timeEnd()? Or maybe another solution that goog.closure provides, but I haven't found?
You just need to added them to the externs you are using.
If you don't want to/can't use externs, you can easily reference "undeclared" objects with the string-based properties:
window['console']['log']('Hello Console!');
window['console']['time']('timeTaken');
...
But you have to be careful, because the second line might throw an error if the time property does not exist or it's not a function.

Resources