Current Time as a Fraction of Current Year

I had the need to represent time as a fraction of a year, so I came up with this little program to do the conversion for me.  (using in Redlion Crimson 3)

//returns current time in terms of fraction 
//of this year, 0.0 to 1.0
//each year consists of 525600 minutes, 
//or 31536000 seconds (common years)

float year;
int sec, thisYear, secondsThisYear, now;

//get current date/time in seconds since 1 jan 1997
now = GetNow();

//what year is it? 
thisYear = GetYear(now);

//get current second of the year as an integer by 
//subtracting 1 jan of this year
sec = now - Date(thisYear,1,1);

//check for leap year (works until 2100) 
if (thisYear % 4 == 0)
 secondsThisYear = 31622400;
 else
 secondsThisYear = 31536000;

//scale to year as a float 
// convert 0 to 31536000(or 31622400) to 0.0 to 1.0
year = sec * 1.0 / secondsThisYear;
return year;

Remember the formula to scale values:

Screenshot from 2015-04-24 12:50:02

So 2:26PM on April 24, 2015 comes out as 0.31123823, perfect!

 

This entry was posted in Industrial Automation and tagged , , , , , . Bookmark the permalink.