Dates...
-
Well this is my "in HISE" stuff that I wanted for the dates....its a mess right now as its all just exploring - but it seems to function as I wanted:
reg leapYears = [1992,1996,2000,2004,2008,2012,2016,2020,2024,2028,2032]; //add more if you need em function getYearNumber(aDateString) { reg yr = Math.floor(aDateString.substring(0,4)); return yr; }; function getMonthNumber(aDateString) { reg mth = Math.floor(aDateString.substring(5,7)); return mth; }; function getDayNumber(aDateString) { reg day = Math.floor(aDateString.substring(8,11)); return day; }; function getDayOfDate(aDateString) { reg leap = 0; reg monthAmounts = [31,28,31,30,31,30,31,31,30,31,30,31]; reg dayCount = getDayNumber(aDateString); reg mnth = getMonthNumber(aDateString); reg sumDays; if(leapYears.indexOf(getYearNumber(aDateString)) != -1) { if(mnth > 2) leap = 1; }; sumDays = dayCount + leap; for(i = 0; i< mnth-1; i++) { sumDays = sumDays + monthAmounts[i]; }; return sumDays; }; function getDiffDates(systemDate,expireDate) { reg systemYear = getYearNumber(systemDate); reg expireYear = getYearNumber(expireDate); reg cLeap = 0; reg diff = 0; if(systemYear < expireYear) { // how long to the end of the year if(leapYears.indexOf(systemYear) != -1) { cLeap = 1; } diff = ((365+cLeap) - getDayOfDate(systemDate)) + getDayOfDate(expireDate); } if(expireYear < systemYear) { // how long to the end of last year reg allLastYear = getDayOfDate(expireDate.substring(0,4) + "-12-31"); // days in the expire year diff = (alllastYear - getDayOfDate(expireDate)) + getDayOfDate(systemDate); } if (expireYear == systemYear) { diff = getDayOfDate(expireDate) - getDayOfDate(systemDate); } return diff; }; reg mySDate = "2023-01-07T05:25:43.511Z"; reg myEDate = "2023-01-08T05:25:43.511Z"; Console.print(getDiffDates(mySDate,myEDate)); reg diffResult= getDiffDates(mySDate,myEDate); if(diffResult >0 && diffResult < 30) { Console.print("valid date"); }else{ Console.print("******INVALID**** date"); }
-
@Lindon That seems convoluted because of the leap years of course. Now having a time in ms will allow you to do the same thing in two lines...
-
@Lindon That seems convoluted because of the leap years of course. Now having a time in ms will allow you to do the same thing in two lines...
yeah - ish.... not really a problem - the diffDates was the worst offender - I dont think time in milliseconds is going to help with that - in fact I can see it being worse.. We really need a function that tells me how long to the end of the year for a given year...
as well as something sensible like
myDate = aGivenDate + 30
-
@Lindon Time in milliseconds take into account all leaps from 01-01-1970. So if you subtract two dates you'll get the exact number of milliseconds between the two, that you can convert up to the scale you want -> msDiff/1000/60/60/24 for days for instance.
-
@ustk yeah I get that - and Im not obsessed about leap years - its not that hard to do...I'd just like a nice way to say
reg myDate = Time(Engine.getSystemTime);
and save all that text parsing...which would mean implementing Time::getCurrentTime()
or Time::currentTimeMillis()..and whilst we are there:
Time::fromISO8601(StringRef iso8601) would be really helpful too...
So I guess I can already see a few more methods we could add right off the bat to make this waay more useful.
-
@Lindon Just adding a parameter to the actual function (or make a separate one...) and it's a one liner:
-
@ustk good plan. I look forward to it. :-)
-
So I have finally created a separated class as Dave suggested that I weirdly called
Timing
...
Time
couldn't be used as a class name because of a million conflicts, but other ideas are welcomeFor now I have added 4 functions, let me know if you need more
Since it is just for a few conversion jobs, I don't think we actually need a Time object, but who knows... (well, except Christoph obviously :) )
-
Hmm,
Timing
is a pretty bad name for this as I would expect it to measure some benchmark stuff or handle MIDI delays etc.What's the problem with
Time
? I'm not aware of any existing classes with this name and even if, I would prefer ´Date` as an API class (there's also a popular JS library with this name so it's a bit more close to our mother language. -
reg leapYears = [1992,1996,2000,2004,2008,2012,2016,2020,2024,2028,2032]; //add more if you need em
Lol
inline function isLeapYear(year) { return year % 4 == 0; }
-
@Christoph-Hart Simply because this was as far as my english brought me today
I tried Time but I encountered some conflicts with Juce Time, or I'm just idiot in the way I did it...
Date sounds good :thumbs_up:
-
@ustk ah your class doesn‘t need to be called Time in C++ the scripting name is determined by getObjectName().
-
@Christoph-Hart If you say so :man_shrugging: Could you say it again in french please?
EDIT: Oh ok I see :thumbs_up:
-
Yeah I think date is a good choice since that's familiar to us Javascript people :)
-
@Christoph-Hart @d-healey @Lindon Pull request made (along with a few others)
-
@Christoph-Hart said in Dates...:
reg leapYears = [1992,1996,2000,2004,2008,2012,2016,2020,2024,2028,2032]; //add more if you need em
Lol
inline function isLeapYear(year) { return year % 4 == 0; }
except of course this is wrong.... you will need
year % 100
and
year % 400
-
@Lindon huh? This would mean that theres a leap year every 400 years so please tell everyone who has their birthday on the 29th of February that they won‘t live to see another birthday party…
-
@Christoph-Hart - er you should go read up on what makes a leap year, here's the code (which I couldnt be bothered writing and testing last time out - naturally lazy...):
inline function isLeapYear(year) { if(year % 400 == 0 ) { return true; }else{ if(year % 100 == 0) { return false; }else{ return year % 4 == 0; } } }
So 2000 is a leap year - but 1900 isnt.
-
@Lindon here's a little script online:
https://www.programiz.com/javascript/examples/check-leap-yearinline function isLeapYear(year) { if ((0 == year % 4) && (0 != year % 100) || (0 == year % 400)) { return true; } else { return false; } }
-
@Dan-Korneff Why not?
inline function isLeapYear(year) { return (0 == year % 4) && (0 != year % 100) || (0 == year % 400)); }