UTC/Timezones in Breeze - breeze

I am working with Breeze and running into some date/time issues.
I have a field in a form, with a date time picker, that is returning a value of 07/20/2018 14:00. For this example, assume I am in CST timezone (GMT -0500). What I would like to do is pass that value to my Breeze entity manager and have it saved in my database correctly. I get the date into a variable:
dateVariable = ctx.ChosenTime;
This works and puts a value of 07/20/2018 14:00 into the variable dateVariable.
I create a new entity:
var newEntity = entityManager.createEntity('Test Entity', {Date: dateVariable};
And when I debug and check the value of newEntity, it has a property called Date with the proper value. However, once I call entityManager.SaveChanges(), and then get the returned value back, it is displayed as 07/20/2018 19:00. Since Breeze is handling the display value (via data binding), I am not sure why this is happening. Any advice would be appreciated. Thanks

When the JSON response comes from the server, if the date string does not have a timezone specifier, Breeze assumes it is in UTC and puts a "Z" on the end before parsing. So it converts your local time to UTC after the round-trip to the server. The solutions are:
Change your server-side property to DateTimeOffset or similar data type that preserves the timezone of a date. This way the returned date will have a time zone.
Tell Breeze not to add the "Z" and just interpret the date in local time. See this SO answer for more information.

Related

Save only time in rails

I have two time fields in the table i.e. start_time and end_time .
When I execute MyModel.save(start_time: '12:34'), it gets saved with appending a date(Sat, 01 Jan 2000 07:25:00 UTC +00:00).
I want to save time only. I am using Rails5
When we execute, a = MyModel.save(start_time: '12:34'), it saves only time in the database. But when we display the value in the console(a.start_time), we gets time with date. So we have to retrieve time from it by the following:
a.start_time.strftime("%H:%M")
Data gets correctly saved in the database
Rails will always append date with time even its type is "time".
You can achieve storing only time using sql query.
But i would suggest you to use one of the following options:
1)store time in column of type fixnum or decimal in 24hrs format like "22.44"
2) Store number of seconds in column of type integer & then write a helper method to confirm it into time.
3)Use column type :time and ignore the date whole querying.

Date in free format into db

I have a model with :birth_date of type date.
I've tried to put a string like 3 janvier 1968 (French language) into that field and somehow in database I saw that PostgreSQL or someone else converted it into a date!
I also tried some other dates like 3 février 1968 or like 3 fevrier 1968 which didn't work and turned out to be NULL in db.
I can't find information about this feature anywhere. How does this work?
Rails knows that attribute is a Date from the database definition, so it converts the string you give it to a Date. If you create a new instance of your model in the Rails console and assign to birth_date, you can show that it's already a Date even before you save it to the database:
m = Model.new # Use your model name
m.birth_date = "3 février 1968"
m.birth_date.class
The console should tell you that m.birth_date is a Date.
So the conversion to Date is done before you save the model to the database. Rails defines a String::to_date method that calls the Ruby ::Date.parse method, which converts various human-readable date strings into a Date (https://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/Date.html#method-c-parse). In the Rails source, you'll see that whatever you assign to a Date attribute is converted to a Date with the to_date method. So when you assign a String, it happens via String::to_date which calls Date.parse.
As you mentioned in your comment, Date.parse seems to take a fairly loose approach to the months when they're spelled out. I tried a variety of English, French, and Spanish months in Date.parse, and as long as the first three letters of the non-English month are the same as the English month, Date.parse will convert them. But if the first three letters are different, then Date.parse throws an error.
if you have a column in the database as type 'date', it will only save as a date. Rails does it's best to convert a string into a recognized date if possible. You should always pass the 'birth_date' data as a date (i.e. use a date_field). Otherwise, if you REALLY want to store it as a string, the birth_date column must be of type string in the database

DOORS / DXL object attribute "Last Modified On" including time?

I understand how to access an object's modification date using the Last Modified On attribute. Is there any hidden / undocumented way to access the modification time with DOORS 9.5? In my case, I want to identify changes since a certain daytime, thus the date is not precise enough.
According to this post at the IBM forum, the attribute was supposed to return date and time. However, the output of this statement:
Date lastModified = obj."Last Modified On"
print "dateAndTime = " (dateAndTime(lastModified)) "\tlastModified = " lastModified "\tdateOnly = " dateOnly(lastModified) "\n"
is in my case
dateAndTime = 08/04/14 00:00:00 lastModified = 04 August 2014 dateOnly = 04 August 2014
and I guess that this means that the change time was not provided (correctly).
Section "History" of the DXL manual describes the function Date lastModifiedTime({Module|Object|Link}) which provides the desired time.
Unfortunately the Last Modified On attribute is only storing the date without a time. In order to get the time of the last modification, you would need to step through the history records on the object and get the time from the last record. This will also be an issue because any object that hasn't changed since the baseline will have no history records against it.

Timezone Offset in Angular JS and Rails

Background: I'm building an app with Angular JS as web interface and Rails API. The problem I am having is passing a date from Angular to Rails.
Issue: I have a form with a Date of Birth date field, when a user inputs his DOB say March 1st, 1985, Angular interprets it as 1985-03-01 00:00 +0800 (if you're in Hong Kong or Singapore) and sends a request to Rails. The first thing Rails does with it is to convert it to UTC, which means the datetime is now 1985-02-28 16:00 UTC. Therefore, when the date is saved to the database date column, it becomes Feb 28, 1985.
Solution for now: What I'm doing now is on Angular side, I get the Timezone offset hours and add it to the date, so instead of 1985-03-01 00:00 +0800, it is now 1985-03-01 08:00 +0800. When Rails get it, it converts to 1985-03-01 00:00 UTC and so saves the correct date to db. However, I believe this is a better alternative to tackle this issue.
Thinking about parsing just the date in Rails, yet the params[:dob] I see is already UTC by the time I get it. Would love to know if there is a better practice than my current solution. Thank you for any comment and feedback.
This problem is actually quite common, and stems from two separate but related issues:
The JavaScript Date object is misnamed. It's really a date + time object.
The JavaScript Date object always takes on the characteristics of the time zone for the environment in which it is running in.
For a date-only value like date-of-birth, the best solution to this problem is to not send a full timestamp to your server. Send just the date portion instead.
First, add 12 hours to the time, to use noon instead of midnight. This is to avoid issues with daylight saving time in time zones like Brazil, where the transition occurs right at midnight. (Otherwise, you may run into edge cases where the DOB comes out a day early.)
Then output the date portion of the value, as a string in ISO format (YYYY-MM-DD).
Example:
var dt = // whatever Date object you get from the control
dt.setHours(dt.getHours() + 12); // adjust to noon
var pad = function(n) { return (n < 10 ? '0' : '') + n; }
var dob = dt.getFullYear() + '-' + pad(dt.getMonth()+1) + '-' + pad(dt.getDate());
Another common way to do this is:
var dt = // whatever Date object you get from the control
dt.setHours(dt.getHours() + 12); // adjust to noon
dt.setMinutes(dt.getMinutes() - dt.getTimezoneOffset()); // adjust for the time zone
var dob = dt.toISOString().substring(0,10); // just get the date portion
On the Rails side of things, use a Date object instead of a DateTime. Unlike JavaScript, the Rails Date object is a date-only object - which is perfect for a date-of-birth.

rails 3: how create a time from hash, like Time.local, except interpret the hash as being in a different time zone?

Given a hash containing time and day, how do I create a time object that is UTC when the hash is some other time zone?
For example, my User is EST and specifies "15:04 8/25/2012" (via a select_datetime in a view) so the time is implicitly EST.
When they submit the form, time_hash = {"day"=>"25", "month"=>"8", "year"=>"2012", "hour"=>"15", "minute"=>"04"}
If I set
the_time = Time.local params[:date][:year], params[:date][:month], params[:date][:day], params[:date][:hour], params[:date][:minute]
it creates a time object that is 15:04 8/25/2012 -- BUT in the SERVER's local time (Pacific), not the User's local time (Eastern).
Note: I DO have Time.zone set to Eastern earlier in the same method, so I thought Time.local would interpret the hash as Eastern time, but it doesn't. I also tried Time.zone.local but that throws an error.
Time.zone.local
should work fine. It's a little picker than Time.local and will only accept integers as its arguments, whereas Time.local doesn't mind strings. Sprinkle some calls to to_i in there and you should be ok.
One trick seems to be: use Time.zone.parse and which also means manually converting the hash to the "YYYY-MM-DD HH:MM:00" format.
the_time = Time.zone.parse("#{params[:date][:year]}-#{params[:date][:month]}-#{params[:date][:day]} #{params[:date][:hour]}:#{params[:date][:minute]}:00")

Resources