clemhoff Posted November 16, 2011 Posted November 16, 2011 (edited) ... feel free to report bugs & errors // USAGE : LocalTimeStampToUTC ( fm_TimeStamp ; fm_TimeStampDateFormat ; fm_UtcOutputPattern ) // 20111115 clem // version 1.2 // // fm_TimeStampDateFormat => 1 = dd/MM/yyyy; 2 = MM/dd/yyyy; 3 = yyyy/MM/dd // fm_UtcOutputPattern => see http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html // // EXAMPLE = LocalTimeStampToUTC ( 01/01/2011 00:07:20 ; 1 ; "dd/MM/yyyy HH:mm:ss" ) [ my machine is located in Time Zone GMT + 1 ] // RESULT => 31/12/2010 23:07:20 import java.text.SimpleDateFormat import static java.util.Calendar.* if (fm_TimeStamp && fm_UtcOutputPattern ){ arrTxt = fm_TimeStamp.split(/[^0-9]/) def arrInt = [] arrTxt.each{ arrInt.add(it as Integer)} def dateMap = [:] tsdf = ! fm_TimeStampDateFormat ? 1 : fm_TimeStampDateFormat as Integer switch ( tsdf ) { case 2: dateMap = [y:arrInt[2], m:arrInt[0], d:arrInt[1]]; break; case 3: dateMap = [y:arrInt[0], m:arrInt[1], d:arrInt[2]]; break; default: dateMap = [y:arrInt[2], m:arrInt[1], d:arrInt[0]] } def timeMap = [h: arrInt[3], mn: arrInt[4], s: arrInt[5]] def tsMap = dateMap += timeMap // Month value is 0-based. e.g., 0 for January. def localCal = new GregorianCalendar() localCal.with{ set YEAR, tsMap["y"] set MONTH, tsMap["m"] - 1 set DAY_OF_MONTH, tsMap["d"] set HOUR_OF_DAY, tsMap["h"] set MINUTE, tsMap["mn"] set SECOND, tsMap["s"] } def utcCal = new GregorianCalendar(TimeZone.getTimeZone("GMT")) utcCal.timeInMillis = localCal.timeInMillis year = utcCal.get(YEAR) month = utcCal.get(MONTH) day = utcCal.get(DAY_OF_MONTH) h = utcCal.get(HOUR) h = ! utcCal.get(AM_PM) && ! fm_TimeStamp.toUpperCase().contains('PM') ? h : h + 12 m = utcCal.get(MINUTE) s = utcCal.get(SECOND) def sdf = new SimpleDateFormat( fm_UtcOutputPattern ) def cal = new GregorianCalendar(year, month, day, h, m, s) return sdf.format(cal.time) } see "Date & Time patterns" from the SimpleDateFormat class Edited November 18, 2011 by clemhoff
john renfrew Posted November 17, 2011 Posted November 17, 2011 Clem If I feed it this 11/17/2011 3:10:42 PM which is Get(CurrentTimeStamp) I get an answer of 11/05/2012 02:10:42 Friday AD I am sat in GMT btw. Also 21/06/2011 00:07:20 gives me 20/06/2011 23:07:20 Monday AD British Summer Time, which is correct
clemhoff Posted November 18, 2011 Author Posted November 18, 2011 Hi John, If I feed it this 11/17/2011 3:10:42 PM which is Get(CurrentTimeStamp) I get an answer of 11/05/2012 02:10:42 Friday AD Because you certainly didn't enter the value "2" for parameter "fm_LocalDateFormat". fm_LocalDateFormat => 1 = dd/MM/yyyy; 2 = MM/dd/yyyy; 3 = yyyy/MM/dd Also 21/06/2011 00:07:20 gives me 20/06/2011 23:07:20 Monday AD British Summer Time, which is correct ... because 21/06/2011 is fm_LocalDateFormat = 1 and the string TimeStamp too.
john renfrew Posted November 18, 2011 Posted November 18, 2011 That is indeed my mistake, but the issue is actually with not recognising the PM part correctly. 03:00:00 PM should be 15:00:00 Needs a test if the timestamp string contains PM
clemhoff Posted November 18, 2011 Author Posted November 18, 2011 John; thank you for reporting ! I added the test and updated the function.
Recommended Posts
This topic is 4762 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now