Skip to content

RealyUniqueName/DateTime

Repository files navigation

DateTime

API docs

Custom date-time implementation for Haxe. Does not store milliseconds information. Contains classes and methods for manipulating intervals and date/time math.

DateTime is an abstract type on top of Float, so it does not create any objects (unlike standard Haxe Date class) and saves you some memory :)

Also DateTime supports dates from 1 a.d. to 16 777 215 a.d. (maybe even more)

DateTime is completely crossplatform, because it's written in plain Haxe.

Timezones

DateTime uses IANA timezone database to deal with timezones: http://www.iana.org/time-zones

Local timezone detection code is a straightforward port of jstimezonedetect library: https://bitbucket.org/pellepim/jstimezonedetect. Notice: Read 'Limitations' of original library readme.

Performance

Depending on platfrom you target and methods of DateTime you use it can be up to 7 times faster than standard Date class or up to 10 times slower.

Timezone database

Unless you reference Timezone class somewhere in your code, it will not be compiled. But you will still be able to get local time by DateTime.local() method.

Full timezone database is ~116Kb. It becomes less then 50Kb when gziped by web server. Timezone database will be compiled into your binary only if you use Timezone class somewhere in your code.

Additionally you have an option to avoid embedding timezone database by providing a -D EXTERNAL_TZ_DB flag to Haxe compiler.

You can load timezone database at runtime from external sources (filesystem, web server, etc.) and pass it to datetime.Timezone.loadData(data:String) (where data is contents of src/datetime/data/tz.dat file)

Since timezones can change several times every year because of various laws in different countries, perhaps you need to update timezones database using haxelib run datetime command (or do it manually)

Timezone database is stored in src/datetime/data/tz.dat file of DateTime library.

Installation

haxelib install datetime

Examples

varutcNow=DateTime.now(); // Current date and time in UTCvarutc=DateTime.fromString('2014-09-19 01:37:45'); //orvarutc:DateTime='2014-09-19 01:37:45'; //orvarutc=DateTime.fromTime(1411090665); //orvarutc:DateTime=1411090665; //orvarutc=DateTime.make(2014, 9, 19, 1, 37, 45); //orvarutc=DateTime.fromDate( newDate(2014, 9, 19, 1, 37, 45) ); //orvarutc:DateTime=newDate(2014, 9, 19, 1, 37, 45); trace( utc.format('%F %T') ); // 2014-09-19 01:37:45trace( utc.getYear() ); // 2014trace( utc.isLeapYear() ); // falsetrace( utc.getTime() ); // 1411090665trace( utc.getMonth() ); // 9trace( utc.getDay() ); // 19trace( utc.getHour() ); // 1trace( utc.getMinute() ); // 37trace( utc.getSecond() ); // 45trace( utc.getWeekDay() ); // 5//find last Sunday of current monthtrace( utc.getWeekDayNum(Sunday, -1) ); // 2014-09-28 00:00:00//find DateTime of May in current yearvarmay:DateTime=utc.getMonthStart(May); trace( may ); // 2014-05-01 00:00:00//snap to the beginning of current monthutc.snap( Month(Down) ); // 2014-10-01 00:00:00//snap to next yearutc.snap( Year(Up) ); // 2015-01-01 00:00:00//find next Mondayutc.snap( Week(Up, Monday) ); //find nearest Wednesdayutc.snap( Week(Nearest, Wednesday) ); trace( utc.add(Year(1)) ); // 2014-09-19 -> 2015-09-19trace( utc+Year(1) ); // 2014-09-19 -> 2015-09-19trace( utc.add(Day(4)) ); // 2014-09-19 -> 2014-09-23trace( utc+=Day(4) ); // 2014-09-19 -> 2014-09-23trace( utc.add(Minute(10)) ); // 01:37:45 -> 01:47:45trace( utc+Minute(10) ); // 01:37:45 -> 01:47:45trace( utc.add(Second(-40)) ); // 01:37:45 -> 01:37:05trace( utc-Second(40) ); // 01:37:45 -> 01:37:05trace( utc.add(Week(3)) ); // 2014-09-19 -> 2014-10-10trace( utc+Week(3) ); // 2014-09-19 -> 2014-10-10trace( utc.snap(Year(Down)) ); // 2014-01-01 00:00:00trace( utc.snap(Year(Up)) ); // 2015-01-01 00:00:00trace( utc.snap(Year(Nearest)) ); // 2015-01-01 00:00:00trace( utc.snap(Week(Up, Wednesday)) ); // 2014-09-24 00:00:00varutc2:DateTime='2015-11-19 01:37:45'; vardti:DateTimeInterval=utc2-utc; //this interval now contains 1 year and 2 monthstrace( dti.toString() ); // (1y, 2m)trace( utc+dti ); // 2015-11-19 01:37:45//assuming your timezone has +4:00 offsettrace (utc.local()); // 2014-09-19 05:37:45//If timezones database is not embedded or you need to load an updated databasevardata:String=...//load from external sourceTimezone.loadData(data); vartz=Timezone.local(); trace( tz.getName() ); // Europe/Moscowtrace( tz.at(utc) ); // 2014-09-19 05:37:45trace( tz.format(utc, '%F %T %z %Z') ); // 2014-09-19 05:37:45 +0400 MSK

And much more: API docs

About

Date/time (+math, +intervals) implementation for Haxe

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 9