Giter Site home page Giter Site logo

lua-icu-date-ffi's Introduction

lua-icu-date-ffi

LuaJIT FFI bindings to ICU (International Components for Unicode). ICU provides a robust date and time library that correctly and efficiently handles complexities of dealing with dates and times:

  • Date and time formatting
  • Date and time parsing
  • Date and time arithmetic (adding and subtracting)
  • Time zones
  • Daylight saving time
  • Leap years
  • ISO 8601 formatting and parsing

Usage

local icu_date = require "icu-date-ffi"

-- Create a new date object.
local date = icu_date:new()

-- You can get and set the date's timestamp.
date:get_millis() -- Defaults to current time.
date:set_millis(1507836727123)

-- You can generate an ISO 8601 formatted string.
local format_iso8601 = icu_date.formats.iso8601()
date:format(format_iso8601) -- "2017-10-12T19:32:07.123Z"

-- You can generate a custom formatted string.
local format_custom = icu_date.formats.pattern("EEE, MMM d, yyyy h:mma zzz")
date:format(format_custom) -- "Thu, Oct 12, 2017 7:32PM GMT"

-- You can parse a string using various formats.
local format_date = icu_date.formats.pattern("yyyy-MM-dd")
date:parse(format_date, "2016-09-18")
date:format(format_iso8601) -- "2016-09-18T00:00:00.000Z"

-- You can extract specific date or time fields.
date:get(icu_date.fields.YEAR) -- 2016
date:get(icu_date.fields.WEEK_OF_YEAR) -- 39

-- You can set specific date or time fields.
date:set(icu_date.fields.YEAR, 2019)
date:format(format_iso8601) -- "2019-09-18T00:00:00.000Z"

-- You can perform date or time arithmetic,
date:add(icu_date.fields.MONTH, 4)
date:format(format_iso8601) -- "2020-01-18T00:00:00.000Z"
date:add(icu_date.fields.HOUR_OF_DAY, -2)
date:format(format_iso8601) -- "2020-01-17T22:00:00.000Z"

-- Timezones are fully supported.
date:get_time_zone_id() -- "UTC"
date:set_time_zone_id("America/Denver")
date:format(format_iso8601) -- "2020-01-17T15:00:00.000-07:00"

-- Daylight saving time is also fully supported.
date:set_millis(1509862770000)
date:format(format_iso8601) -- "2017-11-05T00:19:30.000-06:00"
date:add(icu_date.fields.HOUR_OF_DAY, 5)
date:format(format_iso8601) -- "2017-11-05T04:19:30.000-07:00"

Performance

API

new

syntax: date = icu_date.new(options)

Create and return a new date object.

The options table accepts the following fields:

  • zone_id: (default: UTC)
  • locale: (default: en_US)
  • calendar_type: (default: calendar_types.GREGORIAN)

calendar_types

syntax: fields = icu_date.calendar_types

fields

syntax: fields = icu_date.fields

attributes

syntax: fields = icu_date.attributes

formats.pattern

syntax: format = icu_date.formats.pattern(pattern)

formats.iso8601

syntax: format = icu_date.formats.iso8601()

A shortcut for icu_date.formats.pattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ").

date:get

syntax: date:get(field)

date:set

syntax: date:set(field, value)

date:add

syntax: date:add(field, amount)

date:clear

syntax: date:clear()

date:clear_field

syntax: date:clear_field(field)

date:get_millis

syntax: date:get_millis()

date:set_millis

syntax: date:set_millis(value)

date:get_attribute

syntax: date:get_attribute(attribute)

date:set_attribute

syntax: date:set_attribute(attribute, value)

date:format

syntax: date:format(format)

date:parse

syntax: date:parse(format, text, options)

The options table accepts the following fields:

  • clear: (default: true)

lua-icu-date-ffi's People

Contributors

gui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tarantool

lua-icu-date-ffi's Issues

Allocated char size in function "uchar_to_string" in icu-date.lua not enough for non-ASCII characters

Possibly, allocated char size in function "uchar_to_string" in icu-date.lua not enough for non-ASCII characters.

...
local function uchar_to_string(uchar)
  local length = call_fn("u_strlen", uchar) + 1
  local str = ffi.new('char[?]', length)
  call_fn("u_austrcpy", str, uchar)
  return ffi.string(str)
end
...

For example:

local icu_date = require("icu-date")
local format_date = icu_date.formats.pattern("yyyy-MM-dd")
local format_custom = icu_date.formats.pattern('cccc', "ru_RU")
local date = icu_date.new({locale="ru_RU"})
for i = 1,7 do
	date:parse(format_date, '2000-01-0' .. tostring(i))
	print(date:format(format_custom))
end

So, before changes I've been getting this result:
суббота
воскресе#
понедель#
втор#
сред#
четв#
пятн#

But after changing
local length = call_fn("u_strlen", uchar) + 1 to local length = call_fn("u_strlen", uchar) + 6
everything is fine:
суббота
воскресенье
понедельник
вторник
среда
четверг
пятница

Maybe, I'm wrong and problem in another place..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.