I know we can configure session timeout, but how can we get the configured session timeout value in Apex code?
String sessionLength = Auth.SessionManagement.getCurrentSession().get('NumSecondsValid');
Does Salesforce provide helper function to calculate the remaining session time for current logged in user?
For example, the session timeout is set to be 2 hours, now the remaining session time should be 0.5 hour for a user logged in 1.5 hours ago
No, that is not correct. Because the code was called after halfway through the session time, the timer is reset back to two hours. As long as you're using your session, it does not time out.
To calculate the remaining session time in Apex class, I can query LastLoginDatetime for current user, then calculate expireDate = currentDatetime + (the remaining session time - LastLoginDatetime)
That's not correct (as per above). The session may have been created days ago, so long as it was in use the whole time, it's still active.
As it says in the documentation:
LastModifiedDate ... A session expires when the current date and time equals LastModifiedDate + NumSecondsValid. This field is a standard system field.
Conceptually, you could get the LastModifiedDate and add the NumSecondsValid from the
getCurrentSession() map from above. Note that the date format isn't a typical format you'd see in Salesforce, so you'll have to "manually" parse it.