-1

I am getting user info from facebook after using oauth2 access_token

{
 ...
 "timezone": 5.5,
}

now I need to convert this to Zone Name to fill the openid connect zoneinfo filed.

ex : "Asia/Calcutta"

I am requesting from India , in timezone map I am seeing +05:30 is correct one. but facebook is issuing 5.5 I am not able solve this issue. please help me on this.

vimal prakash
  • 1,503
  • 1
  • 22
  • 38
  • 2
    First of all, there is no 1:1 relation between offsets and time zones. There are multiple time zones using the same offset, so you can not get “the” time zone for a given offset. Second, what exactly is the problem relating `+05:30` to `5.5`? That `.5` corresponds to .5 * 60 minutes = `30` minutes should be pretty obvious, no? – CBroe Feb 12 '18 at 08:02
  • hi @CBroe yes, I will take your suggestion, I just need to know, is that ok to do the .5*60 or not. now I will use that way. thank you – vimal prakash Feb 12 '18 at 08:53
  • 1
    It depends on your programming language and your date-time API. The safe bet is if you can convert it into an offset from UTC rather than a time zone. – Ole V.V. Feb 12 '18 at 11:03
  • ok @OleV.V. sure I will try that as well. thank you – vimal prakash Feb 13 '18 at 07:39
  • Would this question be helpful? [Convert UTC offset to timezone or date](https://stackoverflow.com/questions/11820718/convert-utc-offset-to-timezone-or-date) – Ole V.V. Feb 13 '18 at 08:47

1 Answers1

0

I still don’t know which language you’re after (if any particular). Here’s a Java solution:

    long secondsPerHour = TimeUnit.HOURS.toSeconds(1);
    long offsetSeconds = Math.round(5.5 * secondsPerHour);
    if (offsetSeconds < ZoneOffset.MIN.getTotalSeconds()
            || offsetSeconds > ZoneOffset.MAX.getTotalSeconds()) {
        System.out.println("Not a valid UTC offset, is out of range");
    } else {
        ZoneOffset offset = ZoneOffset.ofTotalSeconds((int) offsetSeconds);
        System.out.println(offset);
    }

It prints

+05:30

You can use the ZoneOffset from this snippet anywhere a ZoneId is expected, since ZoneOffset is a subclass of ZoneId. ZoneId is the modern Java class to represent a time zone.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161