realyuniquename / datetime Goto Github PK
View Code? Open in Web Editor NEWDate/time (+math, +intervals) implementation for Haxe
Home Page: http://doc.stablex.ru/datetime/index.html
License: Other
Date/time (+math, +intervals) implementation for Haxe
Home Page: http://doc.stablex.ru/datetime/index.html
License: Other
date.snapToWeekday( Monday, Up ):DateTime;
date.isInLeapYear():Bool;
date.numDaysInThisMonth():Int;
DateTime.isLeapYear( year:Int ):Bool;
DateTime.numDaysInMonth( month:MonthInt ):Int;
If you are happy to see some or all of these, let me know which ones and I will attempt a pull request, based on the code I have here:
https://github.com/ufront/ufront-mvc/blob/master/src/Dates.hx
The DateTime.fromString()
method only allows ISO 8601 dates that have the following format:
YYYY-MM-DDThh:mm:ss[.SSS]Z
But it is very common to have dates that use a timezone offset, like 2020-04-24T16:20:07+02:00
.
So it would be cool if the DateTime.fromString()
method supported dates using a timezone offset.
Currently, it's very hard to deal with ISO 8601 dates in Haxe!
Consider interval of 2021-06-26, 2022-06-25. interval.getTotalMonths returns -1 instead of 11.
Here's my code fix it outside of datetime package.
@:access(datetime)
class IntervalFix {
public static function getTotalMonthsFix(interval:DateTimeIntervalCore) {
final begin = interval.begin;
final end = interval.end;
var months = (end.getYear() - begin.getYear()) * 12 + (end.getMonth() - begin.getMonth());
var d1 = begin.getDay();
var d2 = end.getDay();
if (d2 < d1) {
months --;
} else if (d1 == d2) {
var h1 = begin.getHour();
var h2 = end.getHour();
if (h2 < h1) {
months --;
} else if (h2 == h1) {
var m1 = begin.getMinute();
var m2 = end.getMinute();
if (m2 < m1) {
months --;
} else if (m2 == m1 && end.getSecond() < begin.getSecond()) {
months --;
}
}
}
return months;
}
}
Doing a simple clock for football matches, counts up to 45 minutes from zero, or to 90 minutes from 45.
Noticed this: if I compile to flash all goes well, but if I compile to windows every time it goes up to sometime:59 it displays sometime:00 before doing sometime+1:00. It is like it is clearing the seconds before updating the minutes, but only in c++.
Attaching .zip with project to illustrate this better. It runs perfect on flash but the same code displays the extra time position when compiled to windows. You can use the keypad's "*" to increase seconds up to the minute change, around sometime:59, and watch what it does. Just press button "45" to start it.
Seriously, that package made my day.
ISO 8601 format standard for strings.
Examples: 2014-10-04T19:42:56+00:00 , 2014-10-04T19:42:56Z (see: http://en.wikipedia.org/wiki/ISO_8601
I feel that removing the ms is a pity and for more general adaptation of the lib and language it would be very beneficial. That would be another clear selling point with respect to the default implementation.
An ISO date like 2000-13-48T00:00:00Z
or 2000-01-01T29:73:82Z
is invalid: the DateTime.fromString()
method should throw an exception in this case.
Instead, it returns a date where the timestamp is adjusted according to the "overflow": 2001-01-17 00:00:00
for 2000-13-48T00:00:00Z
, 2000-01-02 06:14:22
for 2000-01-01T29:73:82Z
.
I has installed datetime on haxe (haxelib install datetime) to get timezone offset. It run ok on Neko. But when I run on Android (lime test android), it runtime error. What's wrong?
Please help me.
Thanks.
import datetime.DateTime;
import datetime.Timezone;
class DateHelper
{
public function getTimeZone():Void{
trace(Timezone.local().getOffset(DateTime.local()));
}
}
Addition / subtraction:
DateTime.now() += Week(1);
DateTime.now() -= Month(1);
Comparison
dateTime1 != dateTime2
dateTime1 == dateTime2
dateTime1 > dateTime2
dateTime1 < dateTime2
Again, just throwing out ideas because this is something I wanted to do but haven't gotten around to, so excited to see where your library might head :)
Add documentation on how to update tz database
I found it useful to create a simple DayCounter for my Covid19 project. I convert dates from a string in the csv data using your library and use my DayCounter to easily control days I render.
Perhaps something like day counter would be useful directly in datetime if not then left here incase useful for others, I expect you could create a date and then increment it by one day but this seemed clearer to work with a cut down format, perhaps you have similar feature already that I missed.
import datetime.DateTime;
@:structInit
class InternalDayCounter {
public var day: Int;
public var month: Int;
public var year: Int;
function new( day: Int, month: Int, year: Int ){
this.day = day;
this.month = month;
this.year = year;
}
}
@:forward
abstract DayCounter( InternalDayCounter ) from InternalDayCounter to InternalDayCounter {
public inline
function new( v: InternalDayCounter ){ this = v; }
public inline
function hasNext(){
return true;
}
public inline
function next(){
var isLeap = DateTime.isLeap( this.year );
var dayTot = DateTime.daysInMonth( this.month, isLeap );
this.day++;
if( this.day > dayTot ) {
this.day = 1;
this.month++;
if( this.month > 12 ) {
this.month = 1;
this.year++;
}
}
}
public inline
function matchDate( date: DateTime ){
return date.getDay() == this.day && date.getMonth() == this.month && date.getYear() == this.year;
}
}
typical use
var dayCounter = new DayCounter({day:5,month:3,year:2020});
function renderDay(){ // every so many frames
var renderData = new Array<MyData>(); // MyData would contain date and some data values.
var j = 0;
for( d in myData ) {
if( dayCounter.matchDate( d.date ) ) renderData[ j++ ] = d;
}
plot( renderData ); // plot data to screen
dayCounter.next();
}
var nov = DateTime.fromString("2020-11-01 00:00:00");
trace(nov); // 2020-11-01 00:00:00
var dec = nov.add(Month(1));
trace(dec); // 2021-12-01 00:00:00
Should happen for any addMonth
ending in december.
The @:op(A += B)
exists twice for DTPeriod operations, instead a @:op(A -= B)
operator is what we expect.
DateTime/src/datetime/DateTime.hx
Line 683 in 0be6fba
Why i did not just compare total amount of seconds???
https://github.com/RealyUniqueName/DateTime/blob/master/src/datetime/DateTimeInterval.hx#L153
I would love to see a "snap()" function similar to your "add()" function.
You could define direction as an abstract enum:
@:enum abstract SnapDirection {
var Up = 1;
var Down = -1;
var Round = 0;
}
I have a function similar to this in my fork of Franco's THX library and it's date class:
https://github.com/ufront/ufront-mvc/blob/master/src/Dates.hx#L145
I want to abandon that class anyway though for a clean abstract approach like this, so I'll request the missing features if you're interested :)
Something like that:
public function toLocal() : DateTime {
return getTime() + getLocalOffset();
}//function toLocal()
static public inline function fromUtcTime (time:Float) : DateTime {
return new DateTime(time + getLocalOffset());
}//function fromTime()
static public inline function fromUtcString (str:String) : DateTime {
return DateTimeUtils.fromString(str).toLocal();
}//function fromString()
static public inline function fromUtcDate (date:Date) : DateTime {
return Math.ffloor(date.getTime() / 1000 + getLocalOffset());
}//function fromDate()
// or make it public (why it is private?)
static public function getLocalOffset () : Int {
var now = Date.now();
var local = make(now.getFullYear(), now.getMonth() + 1, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());
return Std.int(local.getTime() - Std.int(now.getTime() / 1000));
}//function getLocalOffset()
var d = DateTime.now();
Sys.println(d.getDate());
var a = d.snap(Month(Down)).add(Month(-6));
Sys.println(a.getDate());
outputs
2017-06-06 14:31:36
2017-12-01 01:00:00
Why the second date is not 2016-12-01 01:00:00
Why the hour is not 00:00:00 ? ( because of day light saving ?? )
( I'm using haxe 3.2.1 with neko )
Another strange case
var d = DateTime.now();
Sys.println(d.getDate());
var a = d.snap(Month(Down)).add(Month(-2));
Sys.println(a.getDate());
outputs
2017-06-06 14:36:47
2017-04-01 02:00:00
Why the hour is not 00:00:00 ?
...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.