Skip to content

set options timeZoneName is not working #25

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

Open
ebsi-lruiz opened this issue Sep 28, 2020 · 4 comments
Open

set options timeZoneName is not working #25

ebsi-lruiz opened this issue Sep 28, 2020 · 4 comments

Comments

@ebsi-lruiz
Copy link

Hello,
I'm trying to use the options to set the timeZoneName so the string comes formatted using that time zone

I have something like this

const config = {  year: 'numeric',  month: 'short',  day: 'numeric',  timeZoneName: 'UTC', }
const formatter = new DateTimeFormat("en", config)
const result = formatter.format(new Date("2020-01-01")) 

the result should be Jan 1, 2020, but the timeZoneName is being ignored, I'm receiving Dec 31, 2019, instead

Looking at the code, I think I found a solution for that, it could be done in the formatNative function
something like this

// -- ios
...
const timeZone: NSTimeZone = getNativeTimeZone(timeZoneName);
        if (timeZone) {
            dateFormatter.timeZone = timeZone;
        }
...

// -- android
...
const timeZone: java.util.TimeZone = getNativeTimeZone(timeZoneName);
        if (timeZone) {
            sdf.setTimeZone(timeZone);
        }
...

Creating the corresponding getNativeTimeZone methods

If you'd like I can make the pull request

@CatchABus
Copy link

@lruiz-empyrean Does this return correct datetime for any given timezone?

@ebsi-lruiz
Copy link
Author

ebsi-lruiz commented Oct 20, 2020

@lruiz-empyrean Does this return correct datetime for any given timezone?

The current version doesn't, but changing the timezone before formatting it would, as long as you use the time zone IDs, such as: "UTC", "America/Argentina/Buenos_Aires", "America/Aruba", "America/Chicago", etc.

Reference:
https://developer.apple.com/documentation/foundation/nstimezone
https://developer.android.com/reference/java/util/TimeZone

Example:
format: 'YYYY-MM-dd HH:mm'
sourceDate: '2020-01-01'
config: { timeZoneName: 'here goes your target timezone id' }
formatter = new DateTimeFormat("en", config, format)

// 'UTC'
result = '2020-01-01 00:00'

// 'America/Argentina/Buenos_Aires'
result = 2020-12-31 21:00

// 'America/Aruba'
result = '2020-12-31 20:00'

// 'America/Chicago'
result = '2020-12-31 18:00'

@CatchABus
Copy link

CatchABus commented Oct 21, 2020

I see. Could you provide your native example please?
I used the following code on android but no matter what timezone I set, it applies device zone on date.

const isoFormat = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
const date = isoFormat.parse("2010-05-23T09:01:02");
console.log(date);

EDITED: It seems device does not support 'UTC' :D
As long as zone exists on device, it works.

@ebsi-lruiz
Copy link
Author

In the end, I've just inherited from the DatetimeFormat and overrode the current formatNative function in the library

 public formatNative(pattern: string, locale?: string, date?: Date): string {
        let sdf = locale ?
            new java.text.SimpleDateFormat(pattern, getNativeLocale(locale)) :
            new java.text.SimpleDateFormat(pattern);
        const timeZone: java.util.TimeZone = java.util.TimeZone.getTimeZone(this.options.timeZoneName);
        if (timeZone) {
            sdf.setTimeZone(timeZone);
        }
        return sdf.format(date ? new java.util.Date(date.valueOf()) : new java.util.Date()).toString();
    }

and then I used the new class the same as the original.

As long as zone exists on device, it works.

That's a good point to take into account.

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

No branches or pull requests

2 participants