how to get LOCAL TIME, NOT server time? - javascript

I use these lines of code to find out the local time in Gujarat - India, regardless server time set correct or not. But i noticed that this is not working. This line produce SERVER TIME, but not LOCAL TIME. It cause problem if server time change accidentally.
What is the exact solution to to get LOCAL TIME in any country in the world?
Dim zoneId As String = "Indian Standard Time"
Dim tzi As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(zoneId)
Dim result As DateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi)

You may want to take a look at Noda, a .net port of Joda an open source project for working with dates. This has been ported by Stack Overflows, Jon Skeet.
A couple Link that might be helpful.
http://www.codeproject.com/Articles/78213/Noda-DateTime-Extensions-for-NET
enter link description here

What you want is the correct local time, regardless of the server defined time.
First of all, the server should be configured with the correct date/time. If you can't trust it, I think you have to rely on a service provided by a third party provider to tell you the correct time.
This other question suggests services you can connect to: Web service - current time zone for a city?
If what you want is the client's time, you have to get it through Javascript and send it to the server side.
Good luck!

Related

Getting actual timestemp OS independent

Ok, guys, my problem is simple I need a pop on my website to be shown at certain times and I don't want the user to be able to change time on OS thus tricking the popup and one last thing I would prefer not to query my server for the timestamp. Is there some global API for such thing something like GET currenttimestamp.org?
you could you the site https://worldtimeapi.org/ to get the current time (timestamp or other formats).
You could get it for the current IP
https://worldtimeapi.org/api/ip
Or for a specific timezone.
https://worldtimeapi.org/api/timezone/Europe/London
Check out the documentation for more alternatives: https://worldtimeapi.org/api

moment().fromNow() depends on user's PC time

I have an issue with fromNow function in moment js working with user timezones.
It works correctly only when clocks on the user's PC set absolutely correctly.
For example, If I set my clock to -15 minutes from the real time and post a new comment on a website it will show "comment posted in 15 minutes" instead of "comment posted a few seconds ago".
I tried
moment(dateInUTCFromServer).local().fromNow() // in 15 minutes.
and
moment.utc(dateInUTCFromServer).fromNow() // in 15 minutes.
They do now work correctly.
dateInUTCFromServer is a regular timestamp date object from a database. I want to show this date for users in "from now" format. console.log(dateInUTCFromServer) // 2020-01-29 12:25:48
It many cases it gives me "in a second" instead of "a few seconds ago" even when I try to set the correct time on my PC.
In some cases, such issues may be critical. For example, showing deadlines.
Is it possible to use "from now" format ignoring user's PC time settings?
Since you said your dates are in UTC, and in ISO format but do not include a Z or other offset indicator, then the correct code is your second example:
moment.utc(dateInUTCFromServer).fromNow()
The clock on an end user's PC is not something you can control, but it's generally acceptable for one to assume that the end user is synchronizing time. Especially in applications that use third-party authentication (such as a Google or Facebook login) and/or use SSL certificates - both require accurate timestamp validation.
If however you find it critical for the time on the client to be ignored, then you'll need to pass the server's current time to your application.
moment.utc(dateInUTCFromServer).from(currentTimeInUTCFromServer)
Alternatively, if your back end is Node.js, you could just run the original code on the server and pass the string result down to the client.

Make server run function once per day

I have an object on my database containing rows with different dates + emails. I need the server to automatically check once every day or week if any of the listed dates are the current date, and if so send an email to that person. (Image of the object in backand below).
I have made an email "on demand action" at the server side logic and operations in backand, which works, but i have to trigger it manually. Instead i need the server to trigger it on a specific time.
Is this possible to do, and if so how?
A solution i was thinking of, is having a function which is looping through the object, checking dates and sending the emails. And then somehow make the server run this function once per day/week or something.
Cron solution is something we plan to add in the next few weeks, but in the meanwhile, we have a good FREE solution that let you do it.
The service is https://www.easycron.com.
You just need to specify the URL of the action, which you can get from the test action panel (after executing it in test mode).
To gain access to the action, you need to implement the basic authentication which means to use the masterKeyToken( Security & Auth--> Configuration) and an adminKey (Security & Auth--> Team--> click on the key icon near one of the Admins) like that:
https://masterKeyToken:adminKey#api.backand.com/1/objects/action/ObjectName1?name=YourActionName
To read more on Basic auth click here: http://docs.backand.com/en/latest/apidocs/security/#basic-authentication
Cronjobs are the way to go.
If your hoster does not support cronjobs you have no chance to do it well.
A quick google shows me that one.com MAY not support cronjobs. But I'm not sure.
Maybe ask the support.
If they does not I would choose a different hoster which is not shitty.
(Only a shitty hoster does not support crons. I'm not saying one.com is such hoster because I don't know).
If you don't want to and they don't offer crons you could use "Poor Mens Cron". It's a crappy hack from ancient times of the Internet.
You can google that because I wouldn't recommend.

Get the users local time accurately

I am working on a project which primarily uses javascript, css, html5. I need to get the local time accurately no mater where the user is located to allow user to access a module on a particular date. Assume 1 September.
How do i get the users local time accurately?
Options:
1: Use JavaScript to get the users local time and use it.
problem: The user can manually change the date time settings of his system to change the date and access the module prematurely.
2: Use server date time to enable a module on a particular date.
problem: The server could be located anywhere eg: in U.S. and people in Australia will not be able to access the module unless the date in U.S is 1 September.
Is there any other option.
Is using client IP address a option?
well, the user's time/date info is not included in the http request header, so php will not automatically have that information. You can, as you said, use javascript to get the user's time similar to what was posted here: Determine a User's Timezone -- this is with pure javascript, if you use jquery or something similar to it, you can do it very easily.
if these are registered users however, you can allow them to set a timezone in their profile/settings, and then just use THAT setting, so even if they are traveling, they will always be set to the "home" timezone.
does that help?
First of all you should always assume user may fake any data calculated on his side. Therfore using server time is more reliable.
Using IP is an option - you can find services and databases that allow you to resolve IP to country its located in. Example: http://php.net/manual/en/book.geoip.php
Lastly - why do you want efectively differend release date for various countries? They can always use someone in other country to access module in their name.
If the user gives permission, and is using a supported browser, etc, you can get their location using navigator.geolocation.getCurrentPosition().
See developer.mozilla.org/en-US/docs/Web/API/Geolocation.getCurrentPosition for parameters and more info.
You can then use a service such as provided by geonames.org. Eg, http://api.geonames.org/timezone?lat=47.01&lng=10.2&username=demo . This returns the time at the given coords.
Update as per first comment: Of course you can never trust any data coming in from outside. But you can do things to raise levels of confidence. This wasn't meant to be a full stand-alone solution.

Rails: How to get the current user's time zone when using Heroku

I set the time zone of our site on Heroku to Pacific Standard Time (PST) using:
heroku config:add TZ=America/Los_Angeles
Times for users are now always in PST--whether or not they are in the PST time zone.
What's the best way to get the user's actual time zone (i.e. the time zone of where they are physically located)?
I'm guessing that this can be solved using Rails (or Javascript?), as opposed to Heroku.
There are two ways to do this.
Indeed, you can use javascript to fetch their current time/timezone. There is the possibility that the user's computer time is not set correctly, in which case the time zone you display will not be correct.
Because you are using Rails, a recommended way is to get javascript already bundled as a gem, like detect_timezone_rails. This makes it easy to install (because it is all bundled automatically in the asset pipeline.
You can use the IP address to infer their country and time zone. The danger in this case is that a user may be using a proxy. Also, while the IP address generally has city-level resolution, it may be more or less accurate, which may in rare cases give the wrong time zone.
Using the IP address, you can get their approximate city and latitude/longitude. There are many gems that can do this on Ruby Toolbox, eg. geocoder. With the latitude/longitude, you can get the time zone using a gem like timezone.
You can also use one of the above, and allow the user to manually change their time zone on your website (either storing this setting in a database if registered, or as a cookie on their browser). This is useful in case you got the timezone wrong.
There is a couple of ways you could do this depending on how you app is set up. None of which are unique to the Heroku environment.
If your app allows users to sign up then you most probably have a User model, and you may be using the Devise gem for authentication/signup. Add a field to your db (:time_zone) and store the users time zone in this field when they sign up.
>> rails generate migration add_time_zone_to_users time_zone:string
>> rake db:migrate
Rails gives you a handy time_zone_select form helper which gives you a select list with all the Time zones in it which you can display to your user. Add it to the user sign up form and allow the user to set their time zone when signing up.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/time_zone_select
In your Application Controller you can then do something like this;
before_filter :set_time_zone
def set_time_zone
#current user is a devise method see https://github.com/plataformatec/devise
Time.zone = current_user.time_zone if current_user
end
Then when you display a date in your app call .in_time_zone on the time instance which will display the time in the users time zone.
<%= Time.now.in_time_zone %> or <%= #your_model.created_at.in_time_zone %>
If you don't have user authentication then you could fall back to javascript. To do this you could use the native javascript getTimezoneOffset() on the date object, or even better use the following jsTimezoneDetect plugin:
http://www.pageloom.com/automatic-timezone-detection-with-javascript
Finally you could use a hybrid of both and firstly detect their time zone offset using javascript and then store this value in a rails session/cookie and then use a before_filter to set the Time.zone as above but based on the session time_zone value previously calculated in javascript.
Or you can use this awesome gem
https://github.com/kbaum/browser-timezone-rails

Categories

Resources