Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timezone in formal string conversion #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

l0drex
Copy link

@l0drex l0drex commented Aug 14, 2023

Whenever the offset minutes are 0, the timezone was set to "Z" due to using or instead of and.

Whenever the offset minutes are 0, the timezone was set to "Z" due to using or instead of and.
@l0drex
Copy link
Author

l0drex commented Aug 14, 2023

Here is a workaround in typescript:

function toFormalString(simple: Simple): string {
  let formalString = simple.toFormalString();
  
  if (formalString.endsWith("Z")){
    // remove the z, will be added in the next step if applicable
    formalString = formalString.substring(0, formalString.length - 1);

    if (simple.getHours() && (simple.getTZHours() !== 0 || simple.getTZMinutes() !== 0)) {
      formalString += getOffset(simple);
    }
  }

  return formalString;
}

function getOffset(date: Simple): string {
  let hours = date.getTZHours() ?? 0;
  let minutes = date.getTZMinutes();

  if (date.getTZHours() === 0 && date.getTZMinutes() === 0) return "Z"

  let offset = (hours >= 0 ? "+" : "-") + Math.abs(hours).toString().padStart(2, "0");

  if (minutes != null) {
    offset += ':' + Math.abs(minutes).toString().padStart(2, "0");
  }

  return offset;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant