SugarCRM Get an effective TimeDate from a SugarBean field - datetime

I must be missing something obvious, but it seems that I'm unable to find a way to get the TimeDate object from the value of a SugarBean field.
Let's say I get a specific Lead with this kind of call:
$lead = BeanFactory::retrieveBean('Leads', "18bfc69e-8cd4-11e7-ad08-000c29b1a36e");
then any call to this:
$lead->date_entered
will return a string value: "2017-08-29 16:05" (note the absence of seconds).
So then, for example, if I try to use such value to create a SugarTimeDate:
$TimeDate = new TimeDate();
$SugarTimeDate = $TimeDate->fromDb($lead->date_entered);
it will return false, since the value provided to fromDb() is not in the proper format (the seconds are missing).
When looking at the SQL table with Toad, I can see that the information is effectively stored in the database as a DateTime, with the value 08/29/2017 16:05:56. But the SugarBean object provides it as a text with a format that is incomplete.
So how can you get the effective SugarTimeDate, TimeDate or DateTime from a Field in a given SugarBean, ideally as an object?
I searched, and all the example I found was about creating a new date object from Now to set to a field in a SugarBean, but none to set a datetime field from an existing datetime field.
Any hint would be highly appreciated.

By playing around, and with some help from Patrick McQueen, it appears there 2 ways to get the effective date value of a field.
First solution I found was to do a SugarQuery with a select on the needed fields, which then returns the full date information, so "2017-08-29 16:05:56". A bit overkill, but it does the job.
The other solution brought up by Patrick is to use the fetcher_row array from the bean object, which will return the full date information also. So:
$lead->fetched_row['date_entered']
will returns also "2017-08-29 16:05:56".
So in any case an effective date is required ("round-trip" with a get then a set, or some sync requirement), the fetched_row[] is the solution, and the "direct" call to the field $bean->field is to be definitely avoided.

I wasn't 100% clear what you were trying to accomplish (see my comments), but I'm guessing that you want the fromUser() function instead, i.e.
$SugarTimeDate = $TimeDate->fromUser($lead->date_entered);
The reason why, is that Sugar prepares the data for the GUI (including formatting the date as per user preferences) at the point your code is being called. This includes stripping out the seconds. Doing the above fromUser() function will return a SugarDateTime object based on the current user's configured date format with a full date string as a "date" property. This, in turn, could be dealt with elsewhere by using this standard format.

Related

Specify only Time in DateTime field (ISO-8601) in Hyperledger

I'm working on Hyperledger Composer Playground and want to specify only time in DateTime field skipping the Date completely. But whenever I try to create the asset, everytime I'm getting RangeError: Invalid time value error.
I tried giving default value to the variable in model file in some varieties of the following example but doesn't work either.
o DateTime dispatchTime default = "T06:18:42.716Z"
I must be missing some obvious thing in this as it is a ISO-8601 standard format but couldn't find exact solution to get it working specifically in Hyperledger Composer. Thanks in advance.
No, you can't store only time to DateTime variable.
As described in the docs, it will only accept an ISO-8601 compatible time instance, with optional time zone and UTZ offset.
You can manually convert it to only time format at the time of response.
For more details of Hyperledger Primitive types, check out the link below:
https://hyperledger.github.io/composer/latest/reference/cto_language
You are getting invalid time error because you can not store just time in your dispatchTime variable as it is an ISO-8601 Date-Time format. You can check it
But then also, if you want to store only time, you can convert it in one of the transaction processor functions in your logic.js file as follows:
tx.assetObject.dispatchTime = new Date().toISOString().split(/T/)[1];
Hope it helps!

Get Short Date Format/Style/Structure

Suppose my system date is 2014-02-10 & I have published my app on Internet Information System. Now Using DateTime.Now.ToShortDateString method I can get date like 2014-02-10, but how can get pattern/style/structure (e.g yyyy-MM-dd) for date? It should be irrespective of current culture.
IF I change date to 10-Feb-2014, then I should get dd-MMM-yyyy.
What's the solution? Thanks in adv..
Ah, I see. I thought you were saying that you wanted to format a date value but it seems that you're saying that you actually want the format string. In that case, you need to use get an appropriate DateTimeFormatInfo object and get its ShortDatePattern property. You can use System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat or System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat to get that DateTimeFormatInfo.

How to calculate time difference between 2 DateTime fields in SSRS

I'm making a report that involves Tours lets say. I have one column populated by the tour_start_time datafield and another populated by tour_end_time. Then i have a column called Total Time. The Total Time column needs to tell me in hh:mm format what the difference between tour_start_time and tour_end_time is. So if tour_start_time returned 5:00 and tour_end_time returned 5:06 then Total Time should be 0:06. I can't seem to get this and the DateDiff stuff I've read all reference Parameters which isn't what I'm using. I need the data in a table. I tried this expression:
=DateDiff(minute,Fields!tour_start_time,Fields!tour_end_time)
But that didn't work and neither did any of the variants of that which I tried. I know im missing something as im new to this so sorry if it's a dumb question but i need someone that can explain it to me or at least give me another function to try.
Assuming your columns are actually DateTime data types, your DateDiff expression should look like this:
=DateDiff(DateInterval.Minute, Fields!tour_start_time.Value, Fields!tour_end_time.Value)
or
=DateDiff("n", Fields!tour_start_time.Value, Fields!tour_end_time.Value)
VB.NET DateDiff requires a DateInterval parameter; you can either specify the enumerator member (DateInterval.Minute) or its string representation (i.e. "n" for minute).
This will only return an integer value for the difference in minutes; it sounds like you need to then change this to a HH:mm string. Off the top of my head the easiest way to do this would be with some custom code in the report which takes this integer value as a parameter and construct the string based on this. Let me know if you need more information for this part.

Data Binding DateTime to a label->text converts the value to local time

I am working in C++ .net. I have a custom control that shows the contents of a dataBase with a bindingsource in between. I use labels in the control to bind the text property to the data in the dataset through a bindingsource with the following code:
mycontrol->Controls[i]->DataBindings->Add((gcnew System::Windows::Forms::Binding(L"Text",
this->bindingSourceRelRev_Data, mycolumn,true, System::Windows::Forms::DataSourceUpdateMode::OnValidation,
nullptr, L"t")));
Please, note that the code is an excerpt of a loop that iterates on all the controls, therefore the control[i], which would be the label control, and the variable mycolumn, which would be the column of the dataset to be binded to. It works fine apart of an unwanted behaviour that I haven't yet found the way to change.
The sofware runs in different parts of the world but the records must be in local time. The database stores the time in localtime as a DateTime value but the dataBinding interprets it as UTC and shows the time in the label with the timezone difference applied. For example, if the data is 24/06/2012 16:40, it will show 24/06/2012 22:40 in a 4 hours difference time zone.
I need it to show just the value as it was stored, without changes.
I can think of different ways to go arround, but non elegant:
-capture the text update event of the label and un-do the change...
-Store the date as String in the DB...
-fill manually the labels...
-...
If I bind without formating, it will not change the value, but then I have the raw datetime string on the label...
Please, any magic I am missing in the binding so that it will not assume it has to change the time zone?
Thank you very much in advance!
Adan
well, it seems that the problem was not actually on the binding but in the dataset itself. The columns' property DateTimeMode was set as undefinedlocal and this was serializing the dataimput and converting it to the local time of the machine viewing the data. Just changed the property to undefined and it works now as I wanted, just taking the date from the data unmodified.
I was confused by the documentation on the format provider for DateTime, which states that it serializes the date. Perhaps I missunderstood it... It actually doesn't.
Thank you very much anyway!

Control input of SQLite attribute to date format only.

I have been reading all about converting TEXT fields into date formats and ways to use Python to create date objects but my question remains.
My table has a dateTime column that is specified as TEXT, I would like to build a constraint that forces input to be in dateTime format, but as SQLite doesn't do dates (as I would like) I haven't worked out how to do it.
My current ideas: 1. Limit number of characters
2. Set separate attributes for day, month and year and constrain their domains
3. It is silly to do this on the database side just do it in the user interface
I would appreciate your opinions on these or other options.
Thanks :)
I've been trying to solve the same issue and the method I came up with was to use the date/time functions to parse the value and check that it hasn't changed. Essentially the following:
CREATE TABLE dts_test (
dts TEXT CHECK (dts IS datetime(dts))
);
If the default format is not what you want you can use the strftime or similar to design whatever format you want, but it must be something that the built-in date and time functions can parse.

Resources