I hate timezone but i have a query now i have this...
# lucee
g
I hate timezone but i have a query now i have this function public struct function isTimeInRange2( required string timezone, any startDate = "", any endDate = "", string expectedoutput = "PST" ) { try { // Create timezone objects for proper conversion local.inputTZ = createObject("java", "java.util.TimeZone").getTimeZone(arguments.timezone); local.outputTZ = createObject("java", "java.util.TimeZone").getTimeZone(arguments.expectedoutput); local.shortTZ = local.inputTZ.getDisplayName(true, local.inputTZ.SHORT); // Get current time in the expected output timezone local.currentTime = lsParseDateTime( date: NOW(), timezone: arguments.expectedoutput ); // For start and end dates, since they're already in AEST, // we should parse them without timezone conversion if (!isNull(arguments.startDate) && len(trim(arguments.startDate))) { local.startTime = parseDateTime(arguments.startDate); } else { local.startTime = ""; } if (!isNull(arguments.endDate) && len(trim(arguments.endDate))) { local.endTime = parseDateTime(arguments.endDate); } else { local.endTime = ""; } // Calculate if current time is in range local.isInRange = ( (local.startTime == "" || local.currentTime >= local.startTime) && (local.endTime == "" || local.currentTime <= local.endTime) ); // Format times for output local.currentTimeFormatted = lsDateTimeFormat( date: local.currentTime, timezone: arguments.expectedoutput, mask: "mm/dd/yyyy HHnnss z" ); local.startTimeFormatted = (local.startTime != "") ? dateTimeFormat( local.startTime, "mm/dd/yyyy HHnnss" ) & " " & local.shortTZ : ""; local.startTimeFormattedNoZone = (local.startTime != "") ? dateTimeFormat( local.startTime, "mm/dd/yyyy HHnnss" ) : ""; local.endTimeFormatted = (local.endTime != "") ? dateTimeFormat( local.endTime, "mm/dd/yyyy HHnnss" ) & " " & local.shortTZ : ""; return { result: local.isInRange ? "Active" : "Inactive", isInRange: local.isInRange, currentTime: local.currentTimeFormatted, startTime: local.startTimeFormatted, startTimeNoZone: local.startTimeFormattedNoZone, timezonepst: local.shortTZ, endTime: local.endTimeFormatted, timezone: arguments.timezone }; } catch (any e) { return { result: "Inactive", message: "Error processing times: #e.message#", currentTime: "", startTime: "", endTime: "", timezone: arguments.timezone }; } } and its usage is: <cfset currentTmz = isTimeInRange2( timezone = '#getEvents.abbrFormat#', startDate = "#getEvents.regStartZoneDateTime#", endDate = "#getEvents.regEndZoneDateTime#", expectedoutput='PST')> Australia/sydney - getEvents.abbrFormat regStartZoneDateTime - 2024-12-14 094800 regEndZoneDateTime - 2024-12-15 175000 when i dump i get data like this to me it seems wrong, because database have values in AEST and the now should be between the start and end time because it is expected to in there as i am doing in athiments.timezone which specifiees me to AEDT, i wonder what i am doing wrong here any clues
r
this is a small issue, but is setTimezone() and getTimeZone() supported in Lucee like it is in CF 2021? I would imagine they are just cf references to the java libraries you are already hitting but I wonder if using those functions would make a difference.
t
The issue is your:
Copy code
local.currentTime = lsParseDateTime(
                date: NOW(),
                timezone: arguments.expectedoutput
);
https://trycf.com/gist/21b9438b7ed3bdce2b87c056825d07d1/lucee5?theme=monokai
When you use now() as the value for lsParseDateTime, the timezone you specify won't have any effect. But you supply a string to lsParseDateTime then the timezone argument will work
Also you need to understand how the timezone argument in lsParseDateTime work lsParseDateTime("2024-12-15 015015", "Australia/Sydney") means: You ask Lucee take the date/time 2024 Dec 15 015015am as it is a Sydney date/time. Lucee will convert it to your Lucee server's timezone (it is UTC for trycf.com), so the result is subtracting 11hrs (becauase Sydney now is +11hr, and UTC is +0hr) from the datetime value ✅ So lsParseDateTime is: convert the given date/time to Lucee's server timezone as the date/time is in xxx timezone| ❌ Don't think lsParseDateTime is: convert the given date/time value to xxx timezone
g
so you are saying in this case i do not need to use lsparse , and just use parse datetime,
t
Copy code
// Get current time in the expected output timezone
local.currentTime = lsParseDateTime(
                date: NOW(),
                timezone: arguments.expectedoutput
);
The comment says "Get the current time in the output timezone", so if arguments.expectedOutput is "PST", the codes you write there should do this job: convert the value
now()
generated by Lucee server to a date/time in PST timezone The easiest way to do that without using any 3rd party library is use DateTimeFormat() + the timezone argument:
local.currentTime = dateTimeFormat( now(), "yyyy-mm-dd HH:nn:ss", arguments.expectedoutput );
example: https://trycf.com/gist/fe9e3f3eb247f0218faac9b39d55d89d/lucee5?theme=monokai