XPages: create current Datetime in ssjs and save to a Datetime Notes form field - datetime

The only way i have found to store date in a Datetime field in a notes form is this:
theDoc2.replaceItemValue("lastAccess",session.createDateTime("Today"));
But this only creates a Date, not DateTime. Also, i dont want to create a static time like "Today 12" but i want the current datetime dynamicly.
Using this i get an error (Exception occurred calling method NotesDocument.replaceItemValue(string, Date) null):
theDoc2.replaceItemValue("lastAccess",#Now());
and using this, the form field changes from Date/Time to Text data type and i want to keep Date/Time type:
theDoc2.replaceItemValue("lastAccess",#Now().toLocaleString);
Any ideas?

Just gave it a try:
as you wrote, .replaceItemValue("fieldName", #Now()) throws an error.
However, I got it to work with
.replaceItemValue("fieldName", session.createDateTime(#Now()))
In that case the value is stored in the Notes field as Time/Date with all necessary components as in
17.01.2014 12:45:51 CET
From what I can see, difference between the two is that #Now() returns a Date data type, whereas session.createDateTime() returns a NotesDateTime object
On the other hand, for me it's also working with your original method:
session.createDateTime("Today")
Don't know what's causing the prob on your side; do you have an editable represantion of the field on you xpage? If so, does it have some kind of converter enabled which could do some filtering during submit?

i will answer my own question as i found a way. Please comment if you think it is not correct or best practice...
theDoc2.replaceItemValue("lastAccess",session.createDateTime("Today"+#Now().toLocaleTimeString()));

A little late, but I had the same problem, but this method resolved it:
DateTime datumtijd = session.createDateTime("Today");
datumtijd.setNow(); //!!!!!!
System.out.println((datumtijd).toString());
Hope it helps :)

Related

Why does AliceBundle Fixture DateTime give me an unexpected value?

I'm currently working on a project in which I create fixtures with Alice-bundle to run tests to make sure my API-endpoints work properly. Everything works fine, except for the DateTime properties.
No matter what string I pass it, eg: <dateTime('2019-09-23 14:00:00')>, it always gives me the wrong date and time, usually something like: 1998-10-25T14:29:45+01:00.
Even using <dateTime('now')> does not work -- it gives me some pre-2000s date & time as well, while that's exactly what some examples I'd found do use.
A fixture could look something like this:
Path\To\Task\Entity:
my_task:
title: 'My tasks'
description: 'These are all the tasks just for me!!!'
startsAt: <dateTime('now')>
endsAt: <dateTime('now')>
createdBy: '#some_higher_user'
Ideally I just want to pass it a string so I can define both a Date and Time and make sure it works properly, in the right format.
And help would be appreciated.
Looking here https://github.com/nelmio/alice/blob/master/doc/advanced-guide.md#functions we read:
function can be a Faker or a PHP native (or registered in the global scope) function.
So I would recommend trying a PHP native function that creates a \DateTime object
<date_create_from_format ( 'Y-m-d H:i:s' , '2019-09-23 14:00:00')>
// or
<date_create('now')>
That's how it works. The <dateTime()> function takes an argument called $max. So what it does is create a date between a starting date (not sure which one, probably something in the 1900 range or so) and that $max argument.
I guess you will want to use <dateTimeBetween()> which takes a startDate and an endDate to create a fake date between them. So I suppose if startDate = endDate, then you'll get the desired fixed date.
Take a look at fzaninotto/Faker library documentation. It's the library used by AliceBundle to actually fake data. There you can see what DateTime related functions you can use.

Binding char colmn for SQL Server

ALL,
My search with Google didn't produce anything and the example in MSDN does not cover it so....
In my table I have a column with a type char(1). I am trying to retrieve it with the following binding:
SQLWCHAR dataFontItalic[2];
SQLLEN cbDataFontItalic = SQL_NTS;
ret = SQLBindCol( stmt_tableProp, 6, SQL_C_BIT, &dataFontItalic, 2, &cbDataFontItalic );
Unfortunately this code does not work. I don't see the value under the debugger. And unfortunately SQLGetDiagRec() does not say which field in the query is troublesome but I presume it is the first one and the error I'm getting is
Invalid character value for cast specification
The value in the column is 'N'.
Does anybody can spot the issue?
EDIT:
Defining dataFontItalic as SQLCHAR didn't help.
Answering my own question...
Apparently I need to bind to SQL_C_CHAR and not to SQL_C_BIT.
Hopefully this will save someone else's time.

Typo3 6.2.15 DateTime off by 1 hour

I have a model with a DateTime column named "fromDate":
/**
* fromDate
*
* #var \DateTime
*/
protected $fromDate;
now I need this date to do some calculations, but when I try outputting it, it seems to be off exactly 1 hour.
echo $model->getFromDate()->format('Y-m-d H:i:s');
This returns 2016-02-11 01:00:00 instead of 2016-02-11 00:00:00
I checked my server timezone, date_default_timezone_get() returns "Europe/Berlin" (which is correct). I tried to change the typo3 serverTimeZone (I tried 0 and 1, but doesn't change anything).
If I look at the database with phpmyadmin, the entry is "2016-02-11 00:00:00"
What am I missing, why is this happening? Any hints? Because I feel like I'm going insane, a huge calculation script is based on the correctness of $fromDate ... Any help would be greatly appreciated. Thank you!
Try something like
$myDate = $model->getFromDate();
$myDate->setTimezone(new \DateTimeZone('UTC'));
or whatever time zone you use in your raw database table value, if not UTC.
In TYPO3 6.2, at least, doing a findAll() on your repository begins a series of calls -- to \TYPO3\CMS\Extbase\Persistence\Repository->createQuery(), and afterward to \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult->rewind(). rewind() calls initialize(), which calls \TYPO3\CMS\Extbase\Persistence\Generic\Mapper->map(). map() calls mapSingleRow(), which then calls thawProperties() in the same class.
TYPO3\CMS\Extbase\Persistence\Generic\Mapper->thawProperties() has a "switch case" code section based on each property data type. If the type is DateTime, thawProperties() calls mapDateTime() in the same class.
mapDateTime() assumes "native date/datetime values are stored in UTC", so it applies the 'UTC' DateTimeZone to the raw value. But then, mapDateTime() applies the DateTimeZone from the PHP date_default_timezone_get() function, and returns the altered date and timezone property values to thawProperties() and eventually to your Extbase extension.
You can find details in the TYPO3 6.2 API reference for mapDateTime() and its definition in DataMapper.php.
My answer is related to this StackOverflow answer, this TYPO3 Forum thread, and this TYPO3 Forum thread.
This is a problem in the DateTime handling of Extbase, which was recently fixed for TYPO3 7.6 and 8 (dates were not interpreted as UTC). We won’t backport the fix for 6.2 officially, but you can of course apply it yourself.
If you don’t want to patch the core, you can also create an extension class for the relevant part (DataMapper) and override the method (mapDateTime()).

Date format upon retrieval

I am trying to retrieve dates from database in a recordset and print dates in mm/dd/yy mm:ss format in the view. Is it possible to intercept data in the model while retrieval and do formatting there instead of looping over recordset in the controller and reformatting dates in the controller before sending it to the view.
Is there a way in handlebars to format dates in the view without writing a helper function ?
Please help.
Thank you.
To format all my data, I created a service "utils" and pass it formatting libraries like moment.js. Your question asked if the handlebars could do this, and I don't know, but this method gives you the freedom to utilize any library. Reference http://sailsjs.org/#/documentation/concepts/Services
this "utilService" has a few libraries that help with formatting.
// utilService.js - in api/services
var changeCase = require("change-case"),
moment = require("moment"),
numeral = require("numeral");
module.exports = {
numeral : numeral,
moment : moment,
changeCase : changeCase
}
then I can use it in my view pages as utilService.moment(DATE).format('YYYY-MM-DD')

Dexterity Content Type DateTime Issue & How to modify Content Type Values

First of all: I am running a Plonce Plone 4.3 (4305) instance with Dexterity Content Types 2.0.7
My approach is to write a Python Script (added via ZMI) which creates my dexterity content type using the methods invokeFactory(...) or typestool.constructContent(..) described here: http://plone.org/documentation/kb/add-content-programmatically (I've written two scripts which do the same task but use different methods - for learning purposes)
Everything is working fine, except when i try to add a DateTime Object to the constructor of both methods above to create my Content Type. The Date field strangly only updates the day and year values. Because of the restrictions in importing libraries inside python i am stuck (with my current knowledge) with this code:
d = DateTime('12/12/2013')
My script returns the date object after completion which looks like this:
2013/12/12 00:00:00 GMT+1
I've written another small script which outputs the Date value after construction and it gives me the same result (which seems to be correct). The resulting Content Type has its day and year field updated correctly but the month value stays on January and raises the following TypeError upon viewing:
TypeError: int() argument must be a string or a number, not 'instancemethod'
I can fix this by editing the month value manually which is not exactly what i want. I guess it its a minor problem with my DateTime object but i am running out of ideas at this point (overall documentation seems to be a bit scattered). I've tried various date formats inside the DateTime constructor but without luck.
I am also not sure how to modifiy my objects custom field values. Plone seems only to provide setTitle() and setDescription() methods. Maybe someone has a good hint.
Thanky you all in advance,
regards
not sure about the dateTime issue, but check out indexes, indexing and custom index to figure out setting titles and description.
to set your title for instance
#indexer(IFormName)
def titleIndexer(obj):
return obj.valueFromForm
grok.global_adapter(titleIndexer, name="Title")
AFAIK, the problem is that DateTime field for dexterity types needs a datetime object, not a DateTime object.
In the invokeFactory, you should pass a datetime object instead of a DateTime object
>>>date=datetime.datetime(2011,1,1)
>>>myobj=target.invokeFactory(type_name="content_type_name", id=id, date=date)
>>from DateTime import DateTime
>>DateTime().month()
7
>>from datetime import datetime
>>datetime.now().month
7
>>datetime.now().month()
TypeError: 'int' object is not callable
>>> myobj.date
datetime.datetime(2013, 7, 26, 0, 0)
>>> myobj.date.month
7
>>> myobj.date.month()
TypeError: 'int' object is not callable

Resources