1

Twitter's user timeline API is giving me timezone information in its response payload in two different ways - utc_offset (integer) and time_zone (string)

e.g. "utc_offset": -18000, "time_zone": "Eastern Time (US & Canada)",

I intend to store timezone string field in PostgreSQL via my Spring application and want to be able to derive the correct timestamp later using the same.

That would mean I should be able to parse the time_zone field to obtain the timezone information but its format doesn't adhere to what java.util.TimeZone expects. I tried it and because its unable to parse the above format, it falls back to UTC.

Is there a java library that understands the format given by twitter? Its hard to imagine that Twitter will use a non-standard timezone format in its feed.

NOTE:- I am looking for pure Java based solution and not Rails

comiventor
  • 3,922
  • 5
  • 50
  • 77
  • According to the [unicode-file](http://unicode.org/cldr/trac/browser/trunk/common/supplemental/windowsZones.xml), the entry "" indicates that this "timezone" has no DST-information, too (like the offset). – Meno Hochschild Nov 16 '17 at 10:43
  • Can't you parse `created_at` field and extract timezone from there? – Andremoniy Nov 16 '17 at 11:52
  • @Andremoniy`created_at` field is in UTC format – comiventor Nov 16 '17 at 13:07
  • @MenoHochschild my bad there. I spent some time and figured out that what I need is a parser in Java which understands the format of the `time_zone` field. With the timestamp in UTC and timezone string, I should then be able to recover actual time of the tweet creation in user's native timezone. – comiventor Nov 16 '17 at 13:13
  • 1
    They are Rails time zone names. See the dup link for details. – Matt Johnson-Pint Nov 16 '17 at 19:24
  • @MattJohnson Thanks but not sure why you marked my question as duplicate because I am not looking for a Rails based solution :( – comiventor Nov 17 '17 at 01:38
  • The dup answer doesn't provide a Rails based solution. It explains that Twitter itself is using Rails to produce these particular time zone names. They *only* exist in Rails, so one has to use the data from the Rails source code to map them back to standard identifiers. You'd have to do this regardless of language. – Matt Johnson-Pint Nov 17 '17 at 02:33

1 Answers1

0
    DateTimeFormatter parser = DateTimeFormatter.ofPattern("zzzz");
    String timeZoneFromTwitter = "Eastern Time (US & Canada)";
    ParsePosition position = new ParsePosition(0);
    ZoneId zid = ZoneId.from(parser.parse(timeZoneFromTwitter, position));
    System.out.println(zid);

This prints (at least on my computer)

America/New_York

I honestly don’t know how far it will help you, and I admit it’s on the edge of cheating. What really happens is the formatter parses “Eastern Time” only and stops. This is enough that it can recognize the time zone as America/New_York. The ParsePosition is there to specify that we do not require it to parse all of the string.

You mentioned java.util.TimeZone. I encourage you to drop that class and its friends, they are long outdated. java.time, the modern Java date and time API also known as JSR-310 is generally much nicer to work with, so why don’t you make this your habit?

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