Hi, I have some issue with this function ```public...
# lucee
g
Hi, I have some issue with this function
Copy code
public struct function checkTimeExpiration3(dateExpired, timezoneOffset, dstFlag) {
        // Get current time in UTC
        var currentTime = dateConvert("local2UTC", now());

        dump(currentTime);
        
        // Convert expiration date to UTC considering the timezone offset and DST
        var offsetHours = val(left(timezoneOffset, 3));
        var offsetMinutes = val(right(timezoneOffset, 2));
        var totalOffsetMinutes = (offsetHours * 60) + offsetMinutes;
        
        // Add DST hour if applicable
        if (dstFlag == "1") {
            totalOffsetMinutes += 60;
        }
        
        // Convert expiration date to UTC by subtracting the offset
        var expirationUTC = dateAdd("n", -totalOffsetMinutes, dateExpired);

        dump(expirationUTC);
        
        // Calculate time difference in seconds instead of minutes
        var timeDiffSeconds = dateDiff("s", currentTime, expirationUTC);

        dump(timeDiffSeconds);
        
        // If expired
        if (timeDiffSeconds <= 0) {
            return {
                result: "Expired",
                formattedTime: '<span style="color:red; font-weight:bold;">This invitation has expired</span>'
            };
        }
        // Calculate remaining time components
        var days = fix(timeDiffSeconds / (24 * 60 * 60));
        var remainingSeconds = timeDiffSeconds mod (24 * 60 * 60);
        var hours = fix(remainingSeconds / 3600);
        remainingSeconds = remainingSeconds mod 3600;
        var minutes = fix(remainingSeconds / 60);
        var seconds = remainingSeconds mod 60;
        
        // Format the time remaining
        var components = [];
        if (days > 0) components.append("#days# days");
        if (hours > 0) components.append("#hours# hours");
        if (minutes > 0) components.append("#minutes# minutes");
        if (seconds > 0 || arrayLen(components) == 0) components.append("#seconds# seconds");
        
        return {
            result: "Not expired",
            formattedTime: arrayLen(components) ? arrayToList(components, ", ") & " remaining" : "0 seconds remaining"
        };
    }
but i tried to convert the time to PST time zone which has the value from database -8:00,1 but its always giving me 1 hour ahead
t
Give us an example, what value is dateExpired? and what you get from your dump (currentTime, expireationUTC, and timeDiffSeconds) Try to guess from your question, you are giving the function timezoneOffset="-8:00" and dstFlag=1 With dstFlag=1, it will adjust your timezone/offset forward by 1 hour, so effectively -8:00 become -7:00 ; Is that what you mean "always giving 1 hour ahead"?
g
image.png
r
Don't do dates manually unless it is a learning task. There are many date functions to use, if not in CF - then in Java