###
# Warranty & Liability
# To the extent permitted by applicable law and unless explicitly
# otherwise agreed upon, XLOG Technologies AG makes no warranties
# regarding the provided information. XLOG Technologies AG assumes
# no liability that any problems might be solved with the information
# provided by XLOG Technologies AG.
#
# Rights & License
# All industrial property rights regarding the information - copyright
# and patent rights in particular - are the sole property of XLOG
# Technologies AG. If the company was not the originator of some
# excerpts, XLOG Technologies AG has at least obtained the right to
# reproduce, change and translate the information.
#
# Reproduction is restricted to the whole unaltered document. Reproduction
# of the information is only allowed for non-commercial uses. Selling,
# giving away or letting of the execution of the library is prohibited.
# The library can be distributed as part of your applications and libraries
# for execution provided this comment remains unchanged.
#
# Restrictions
# Only to be distributed with programs that add significant and primary
# functionality to the library. Not to be distributed with additional
# software intended to replace any components of the library.
#
# Trademarks
# Jekejeke is a registered trademark of XLOG Technologies AG.
##

from nova.core import (exec_unify, exec_build,
   check_integer, check_atom, exec_deref, is_pending,
   is_variable, make_error, set, make_check,
   Compound, sys_time_parse)
import time
import locale

################################################################
# os_atom_time/4                                              #
################################################################

###
# os_atom_time(A, F, O, T): internal only
# If A is a variable, the built-in succeeds in A with the millisecond
# time T formatted by the pattern F and the flag O. Otherwise the
# built-in succeeds in T with the millisecond time parsed from A
# by the pattern F and the flag O.
##
def test_os_atom_time(args):
    fmt = exec_build(args[1])
    check_atom(fmt)
    flag = exec_build(args[2])
    check_integer(flag)
    text = exec_deref(args[0])
    if is_variable(text) or is_pending(text):
        tms = exec_build(args[3])
        check_integer(tms)
        try:
            res = sys_time_format(fmt, tms, flag)
        except ValueError:
            raise make_error(Compound("domain_error", ["time_format", fmt]))
        return exec_unify(text, res)
    else:
        text = exec_build(text)
        check_atom(text)
        try:
            res = sys_time_parse(text, fmt, flag)
        except ValueError as err:
            if "in format" in err.args[0]:
                raise make_error(Compound("domain_error", ["time_format", fmt]))
            else:
                raise make_error(Compound("syntax_error", ["illegal_date"]))
        return exec_unify(args[3], res)


###
# <p>Format a date time string.</p>
#
# @param pat  The format pattern.
# @param tms The date time in UTC milliseconds.
# @param utc The UTC calendar flag.
# @return The date time as a string.
##
def sys_time_format(fmt, tms, utc):
    if utc != 0:
        date = time.gmtime(tms/1000)
        locale.setlocale(locale.LC_TIME, (None, None))
        return time.strftime(fmt, date)
    else:
        date = time.localtime(tms/1000)
        locale.setlocale(locale.LC_TIME, locale.getlocale())
        return time.strftime(fmt, date)


#######################################################################
# Int Lib Init                                                        #
#######################################################################

def main():
    set("os_atom_time", 4, make_check(test_os_atom_time))