Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto detect user's timezone using his ip in rails

I want to find a users time zone from his ip address. If the user is travelling the application should be able to find out the current time zone in which he is.

like image 561
Mahesh Avatar asked Dec 31 '25 13:12

Mahesh


2 Answers

You can't, at least reliably: think of proxies, ISP NAT or VPNs, even if a user seems in a country it doesn't mean he's really there.

I would suggest to let the user choose his preferred time zone, and let him alter it if he needs to.

Otherwise, have a look at this: Determine a User's Timezone

like image 140
Albireo Avatar answered Jan 03 '26 13:01

Albireo


Yes, you can get the timezone in rails based on the user's IP address but it won't be accurate always because of proxies or VPNs.

Why?

  • Your application will get only the latest IP(think ISP provider and VPN) from the request header and you can find timezone based on it.

However, if you have a chance to overlook this shortcoming you can use check this gem with the IP lookup service. To test, you can use the ipapi_com service(it is free up to 45/minute). For the implementation hints, you can have a look here.

like image 35
Tamal Chakroborty Avatar answered Jan 03 '26 13:01

Tamal Chakroborty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!