choptastic / qdate

Erlang date, time, and timezone management: formatting, conversion, and date arithmetic
http://sigma-star.com/blog/post/qdate
MIT License
247 stars 82 forks source link

qdate:set_timezone("UTC+8")'s error #19

Open dcy opened 9 years ago

dcy commented 9 years ago
qdate:set_timezone("UTC+8"),
qdate:to_unixtime(calendar:local_time())

will raise error:

(sanguo_1@192.168.0.5)32> qdate:to_unixtime(calendar:local_time()).
** exception error: no function clause matching calendar:date_to_gregorian_days(error) (calendar.erl, line 124)
     in function  calendar:datetime_to_gregorian_seconds/1 (calendar.erl, line 137)
     in call from qdate:to_unixtime/2 (src/qdate.erl, line 294)

qdate:to_date("UTC+8", util:unixtime()). will be "{error,unknown_tz}"

"GMT" works

dcy commented 9 years ago

And another error:

Eshell V6.3  (abort with ^G)
1> qdate:start().
ok
2> calendar:local_time().
{{2015,11,19},{17,27,37}}
3> qdate:to_date(qdate:unixtime()).
{{2015,11,19},{9,28,1}}

it will be 8 hours difference.

choptastic commented 9 years ago

Hi,

Thanks for the report.

That it crashes with "UTC+6" is definitely an error, and I will get that fixed.

Apparently it doesn't like "UTC+6", but "GMT+6" is fine, but does reveal a bug in erlang_localtime with respect to "GMT+-X" format in that it seems to increment and decrement the minutes rather than hours. That's another problem.

As for the calendar:local_time() thing, that is not actually an error. Unless you've set a default timezone using the config key default_timezone (or at runtime with application:set_env(qdate, default_timezone, XYZ)), or set for the specific process using qdate:set_timezone(XYZ), then you will get that discrepancy.

qdate assumes GMT unless you've set one of the above settings to change the timezone. Conversely, calendar:local_time() returns the current machine's timezone local time, while qdate:to_date() will convert any times to the currently set timezone (in the above example, GMT).

For now, I recommend using the canonical names for timezones, either the abbreviations (like "CST" for "Central Standard Time"), or the location names, like "Asia/Brunei".

choptastic commented 9 years ago

The GMT+8 bug has been filed here: https://github.com/dmitryme/erlang_localtime/issues/26