Dygraphs timestamp with timezone - timezone

I know there was a lot of discussion about datetime parsing in dygraphs and problems with Javascript. But still, I have a problem how to correctly pass datetime.
I'm using dygraph to show data (points with 5 seconds interval) and it's crucial to have correct datetime. But if I pass datetime as timestamp (1401580800) or as ISO 8601 (2014-06-01T00:00:00+00:00), I always get datetime modified to my local time.
My question is, how to correctly pass the datetime to dygraph so datetime doesn't change?

Dygraphs use Javascript Dates, which display according to your local timezone (unfortunately, there's no way to display them using an arbitrary timezone).
One solution is to modify the data - I've encountered a similar problem in my own work, and I made a small helper function to make fake Javascript UTC dates (basically ending up with a Javascript date that's actually the wrong time but shows up looking correct when displayed in local time). This worked for my application, which used moments (http://momentjs.com/) to represent dates everywhere unless required by some other library to use Javascript's Dates.
Another (probably more correct) solution is to modify Dygraph's functions to display the right things, as demonstrated in the answer to this post: Dygraph showing dates in an arbitrary Timezone

The JavaScript Date object will always use the local time of the computer it's running on. If you don't want that behavior, you'll need to use something else. Consider building a pre-formatted string using something like moment.js and then just pass the string to dygraphs instead of the date.

Related

How best to store and convert dates and times in .net core booking system?

I'm writing a room booking system in .net Core 2.0
I have StartTime and EndTime stored as DateTime variables, but the built-in HTML datetime field is clunky and rather user-unfriendly, so I want the values to display on the page as a date field and a time field.
But I'm wondering what the best way of implementing this is. Should I:
create some readonly variables within the model (can I do this without creating database fields?)
create the separate form fields (with jQuery datepicker) and then use JavaScript to write the values of the inputs to hidden fields which store the full DateTime?
(NB I haven't chosen to change the database fields because it is conceivable that a booking might run past midnight. The JavaScript would assume that an early hours endtime is the next day. )
First and foremost, use DateTimeOffset instead of DateTime for your database-backed date/time properties. Whether you end up storing the values in UTC or local time, as long as you have the timezone, you can get to whatever other locale you need. Trust me, this will be invaluable in the long run.
As far as editing goes, make your inputs datetime-local types. This isn't actually supported by any browser, but it at least carries more semantic value and who knows, maybe one day browsers will finally get off their collective hind-quarters and do something with it. Breaking it out into separate date and time fields is tempting, so that you can take advantage of built-in browser controls, but it's clunky and prone to error. Use a JavaScript library that provides a good date and time control to progressively enhance the input instead. My favorite at the moment is air-datepicker. It's lightweight, supports all sorts of scenarios and configuration options, and it's localized.

Ion DateTime also sends DateTime as UTC (Z)

My Ionic 3 app uses the ion DateTime field where the user inputs a date.
<ion-datetime displayFormat="DD/MM/YYYY HH:mm" [(ngModel)]="scheduled_at"></ion-datetime>
This gets then send to my Rails 5 API. The string that arrives at my API endpoint looks like this:
"scheduled_at"=>"2018-06-14T23:33:42.939Z"
I read that the Z indicates UTC (https://robots.thoughtbot.com/its-about-time-zones). The issue is that the timezone the user enters is not in ZTC but in 'America/Sao_Paulo', so when I parse the string to a DateTime object in Rails and store it to the DB it will always be wrong.
Is there any way to tell the ion DateTime the correct timezone?
Maybe, there is no way to do that. moment.js is an alternative.
In ionic datatime documentation,
Advanced Datetime Validation and Manipulation
The datetime picker provides the simplicity of selecting an exact
format, and persists the datetime values as a string using the
standardized ISO 8601 datetime format. However, it's important to note
that ion-datetime does not attempt to solve all situtations when
validating and manipulating datetime values. If datetime values need
to be parsed from a certain format, or manipulated (such as adding 5
days to a date, subtracting 30 minutes, etc.), or even formatting data
to a specific locale, then we highly recommend using moment.js to
"Parse, validate, manipulate, and display dates in JavaScript".
Moment.js has quickly become our goto standard when dealing with
datetimes within JavaScript, but Ionic does not prepackage this
dependency since most apps will not require it, and its locale
configuration should be decided by the end-developer.
Try to use toLocaleString() string function of Date
myDate: String = new Date().toLocaleString();

Selecting date and time with a datepicker for the date but with time immediately visible in Rails

I'm using Rails 4, and also Twitter Bootstrap.
I'd like to select the date with a date picker, but have a separately visible component for selecting the hour and minutes.
I've had a look at smalot bootstrap-datetimepicker https://github.com/smalot/bootstrap-datetimepicker , but looking at the Demo Page, they don't demonstrate any ability to show date and time separately. You have to choose date, and then later on choose time, which doesn't feel very intuitive.
I've also looked at Eonasdan bootstrap-datetimepicker https://github.com/Eonasdan/bootstrap-datetimepicker , but the time picking for it, even in inline mode, is not intuitive - will people know they can just click on the hour value to change it?
I'm thinking of just using a date picker for picking the date, and selecting the hour and minute myself, but it kind of feels wrong handing off part of a datetime to a gem/library and handling the rest of it myself.
I came across Separate date and time form fields in Rails , which is asking about this kind of problem, but it's a question from September 2010.
How do I select the date with a date picker, but have simple and immediately visible selection of time?
First, unless you find a plugin that does what you want off the rack, then yes, it's up to you to handle it, and yes, it feels kinda wrong - depending on how you do it.
Not sure what you had in mind, but the way it feels "the most wrong" is if your form has a single "date time" field under the hood, and you use javascript to botch together the date from the plugin and the time from your own setup, and store them in your datetime field. The nice thing about this is your rails app just gets a single datetime field and knows exactly what to do with it.
Here's how I'd approach it:
Keeping in mind that forms don't necessarily have to map 1-to-1 with your models, I'd split it in the controller layer, and conceptually think of "a form with two fields: date, and time", and then in your controller (or a form object, which is probably better for this situation) you'd stitch the date and time together, before saving them to your model. This approach means you can have separate validation on each field, which is probably also what you want (because I'd assume it's possible for users to input a valid date, but an invalid time or vica versa).
In terms of handling the date with a plugin and the time yourself, that's now fine - they're two completely separate fields from the perspective of your form, so there's nothing dirty about it. It just means you need the extra logic in your controller layer to split the datetime when you display the form, and merge the date and time back into one when you save the form.
Edit: if you haven't heard of form objects, check out https://github.com/apotonick/reform and http://railscasts.com/episodes/416-form-objects

Is there a site that allows timezone conversion on a given date easily by URL

I am looking for a timezone converter site which accepts arguments of a time, city, and date on the command line and opens up a page showing the equivalent time/date in my current locale. (Needing to specify my current locale would be acceptable.) I do need it to be a little smart about searching for the cities - I don't want to have to enter an exact timezone syntax but rather a substring which results in a unique search.
Unfortunately spending money is not an option - I've got to find a free service.
More detail:
I use DTRT extensively and would love to be able to do an instant time zone conversion straight from my address bar.
I would like to type this:
3pm Dallas 2015-03-20
and get a page telling me what time that will be in my current location (and perhaps in certain other locations I would set up).
The DTRT is designed to simply convert the query string in the URL to a different URL so I'm looking for a site that might accept GET parameters like this in the URL. I've done quite a bit of googling, but so far everything I have found either (1) requires me to go through their interface to convert the cities to codes or (2) doesn't do GET parameters at all.
Any time zone converters you know that do this? The closest I've found is this and it uses those codes...
Here is another option which works just by naming the city in the URL, but it has no capability of doing it for another date. When I am setting up meetings when one city changes to DST one week and another city 3 weeks later I absolutely have to have the date capability.
Have you tried wolfram alpha?
https://www.wolframalpha.com/input/?i=3pm+Dallas+2015-03-20

Rails times comparison

I need to compare two dates:
One date is taken form a table, created_at column and the other one is taken from (i don't know, server time, or client time).
So is it ok to compare them like this?
t.strftime("%Y-%d-%m") == Time.now.strftime("%Y-%d-%m")
Where t is the table row.create_at.
And now about, the time that my t will be compared with, from where is Time.now taken from my computer, or from the server?
If is taken from my computer, how can I use the server time?
Since Rails know that your time information is stored as UTC in the database it will convert any time you give it to UTC.
I think we should use
t.strftime("%Y-%d-%m") == Time.zone.now.strftime("%Y-%d-%m")
at the place of
t.strftime("%Y-%d-%m") == Time.now.strftime("%Y-%d-%m")
Just be sure to never construct the query string by hand and always use Time.zone.now as the base and you should be safe.
For more info, go through working with time zones
This comparison will work as expected because you are converting time to string and then comparing strings. I usually convert to integer for comparison but since you are comparing dates you would need to convert time objects to date object first.
Ruby is executed on the server so Time.now is always server time. If you want to compare with utc time use Time.current

Resources