ulf.schroeter Posted March 16, 2013 Share Posted March 16, 2013 problem When working with real-world data visualization time and date plays an important role. It is quite common to represent date/time values as compact UNIX timestamp int values. UNIGINE time() function allows gathering current timestamp value and date() function allows string formating of current date/time. Unfortunately there are no functions for conversion of UNIX timestamp int values to/from corresponding date/time strings. The makes both data date/time parsing and formating very complicated proposal Addition of simple date/time string conversion to/from UNIX int timestamp e.g. by extending existing date/time functions for user-provisioning of explicit date string / timestamp int. Usage of default parameters should allow full code compatibility with existing script code. string date( string format, int timestamp = -1 ); int time( string datestring = 0 ); Required UNIGINE code change should be trivial, but would greatly simplify date/time handling via UNIGINE sript Link to comment
ulf.schroeter Posted March 16, 2013 Author Share Posted March 16, 2013 Just for illustration, some ugly temporary work-around code for timestamp conversion/date parsing. No solution for required conversion from timestamp to date/time string yet... #ifndef __TIMESTAMP_H__ #define __TIMESTAMP_H__ /////////////////////////////////////////////////////////////////////////////// #define TIMESTAMP_MAX 2147483647 /** * Timestamp utility */ namespace Timestamp { int days_start_of_month[12] = ( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ) ; /** * Calculate timestamp value from datetime string. * * @param datetime datetime string in format * * "dd/.mm/.yyyy hh:mm:ss" or * "yyyy/.mm/.dd hh:mm:ss" * "yy-mm-dd hh:mm:ss" * * @return seconds since 01.01.1970 (-1: error) */ int parseDate( string datetime ) { string components[0]; if( strsplit( datetime, "/.- :", components ) != 6 ) { log.error( "Timestamp::parseDate() invalid date format '%s'\n", datetime ); return -1; } long year; long days; if( strlen( components[0] ) == 4 ) { // yyyy/.mm/.dd hh:mm:ss year = long(components[0]); days = long(components[2]); } else if( strlen( components[2] ) == 4 ) { // dd/.mm/.yyyy hh:mm:ss year = long(components[2]); days = long(components[0]); } else { // yy-mm-dd hh:mm:ss year = long(components[0]) + 2000; days = long(components[2]); } long month = long( components[1] ); long hours = long( components[3] ); long minutes = long( components[4] ); long seconds = long( components[5] ); return getTimestamp( year, month, days, hours, minutes, seconds ); } /** * Calculate timestamp value from date components. * * @return seconds since 01.01.1970 (-1: error) */ int getTimestamp( long year, long month, long days, long hours, long minutes, long seconds ) { long timestamp; if( year < 1970 || year > 2038 ) { log.error("Timestamp::getTimestamp() invalid year %d\n", year ); return -1; } if( month < 1 || month > 12 ) { log.error("Timestamp::getTimestamp() invalid month %d\n", month ); return -1; } if( days < 1 || days > 31 ) { log.error("Timestamp::getTimestamp() invalid days %d\n", days ); return -1; } if( hours < 0 || hours > 23 ) { log.error("Timestamp::getTimestamp() invalid hours %d\n", hours ); return -1; } if( minutes < 0 || minutes > 59 ) { log.error("Timestamp::getTimestamp() invalid minutes %d\n", minutes ); return -1; } if( seconds < 0 || seconds > 59 ) { log.error("Timestamp::getTimestamp() invalid seconds %d\n", seconds ); return -1; } long years = year - 1970L; long leapyears = ((year-1L)-1968L)/4L - ((year-1L)-1900L)/100L + ((year-1L)-1600L)/400L; timestamp = seconds + 60L * minutes + 60L * 60L * hours + (days_start_of_month[month-1L]+days-1L) * 60L * 60L * 24L + (years * 365L + leapyears ) * 60L * 60L * 24L; if( (month > 2L) && ( year%4L == 0L && (year%100L != 0L || year%400L == 0L)) ) { timestamp += 60L * 60L * 24L; /* +one day if leap year */ } return int(timestamp); } } #endif /* __TIMESTAMP_H__ */ Link to comment
frustum Posted March 17, 2013 Share Posted March 17, 2013 date() function will receive an optional time argument. Link to comment
Recommended Posts