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

Convert UTC Timestamp to Local Date and Time #23

Open
1 task done
pgiannone33 opened this issue Apr 7, 2023 · 0 comments
Open
1 task done

Convert UTC Timestamp to Local Date and Time #23

pgiannone33 opened this issue Apr 7, 2023 · 0 comments

Comments

@pgiannone33
Copy link

Contact Details

p.giannone36@gmail.com

Language

Javascript

Category

Data Processing and Enrichment

Description

The ability to accurately convert timestamps to the user's local time zone can greatly improve the usability and accessibility of many applications.
This transformation can be used to convert an ISO 8601 timestamp to a user's local date and time based on their time zone.

Purpose

Suppose you are building a social media platform that allows users to schedule posts to be published at a specific date and time. You want to display the scheduled post times in the user's local timezone so they can better manage their content. By using this transformation, you can convert the UTC timestamp of each scheduled post to the local date and time of the user, based on their timezone and locale settings. This makes it easier for users to schedule and manage their posts, and saves time and effort by avoiding having to manually convert the timestamp from Zulu time to the client's local time at the destination.

Example

George is a small business owner who uses Rudderstack to manage his social media accounts. Since his audience is spread across different time zones, George wants to publish posts at the optimal time for each audience. He plans to schedule two posts to be published at 9am local time in Italy (8 am UTC) and America (2 pm UTC).
Using this transformation, he can easily identify the correct time at which to publish his posts without performing any conversion on the destination and automating the process.

Approach

The function first extracts the event's timezone, original timestamp, and locale:

    const timeZone = event.context?.timezone;
    const originalTimestamp = event.originalTimestamp;
    const locale = event.context?.locale || "en-US";

Then, it converts the original timestamp to a string in the user's local date and time format, based on their time zone and locale. The resulting local date and time string is then added to the event object as a new property, localTimestamp.

Besides, it offers great flexibility in representing the local date and time format:

  • The format of the produced date will depend on the locale value passed: for example, if "it-IT" is passed instead of "en-US", the format of the produced date will be different, as the Italian language has different names for months and days of the week than English. Therefore, if the function is called with event.context.locale set to it-IT, for example, the month name "January" will be translated to "Gennaio", while the day of the week name "Tuesday" will be translated to "Martedì"

  • The options constant can be modified to meet specific needs, allowing for a more personalized and accurate representation of the date and time for each user based on their specific time zone.
    For example, considering the following input data:

originalTimestamp: 2020-05-25T20:37:10.917Z
timezone: US/Pacific
locale: en-US

Options Value Local Date format
const options = { weekday: "long", year: "numeric", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" } Monday, May 25, 2020, 01:37:10 PM
const options = { year: "2-digit", month: "2-digit", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric" } 05/25/20, 1:37:10 PM
const options = { hour12: false, year: "2-digit", month: "2-digit", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", timeZoneName: "long" } 05/25/20, 13:37:10 Pacific Daylight Time
const options = { hour12: true, dateStyle: "full", timeStyle: "full"} Monday, May 25, 2020 at 1:37:10 PM Pacific Daylight Time
const options = { hour12: true, year: "numeric", month: "long", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", timeZoneName: "short" } May 25, 2020, 1:37:10 PM PDT
const options = { year: "numeric", month: "2-digit", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric" } 05/25/2020, 1:37:10 PM

Code Block

export function transformEvent(event) {

    const timeZone = event.context?.timezone;
    const originalTimestamp = event.originalTimestamp;
    const locale = event.context?.locale || "en-US";

    if (timeZone && originalTimestamp) {
        const date = new Date(originalTimestamp);
        const localDateTime = date.toLocaleString(locale, {
            ...options,
            timeZone
        });
        event.localTimestamp = localDateTime;
    }

    return event;
}

const options = {
    year: "numeric",
    month: "2-digit",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric"
}

Input Payload for testing

[
  {
    "anonymousId": "8d872292709c6fbe",
    "channel": "mobile",
    "context": {
      "locale": "en-US",
      "timezone": "Asia/Kolkata",
      "traits": {
        "address": {
          "city": "Kolkata",
          "country": "India",
          "postalcode": "700096",
          "state": "West bengal",
          "street": "Park Street"
        },
        "age": "30",
        "anonymousId": "8d872292709c6fbe",
        "birthday": "2020-05-26",
        "createdat": "18th March 2020",
        "description": "Premium User for 3 years",
        "email": "identify@test.com",
        "firstname": "John",
        "userId": "sample_user_id",
        "lastname": "Sparrow",
        "name": "John Sparrow",
        "id": "sample_user_id",
        "phone": "9876543210",
        "username": "john_sparrow",
        "quantity": "5",
        "price": "56.0"
      }
    },
    "event": "identify",
    "messageId": "1590431830865-3be680d6-7dcd-4b05-8460-f3acc30046d9",
    "originalTimestamp": "2020-05-25T18:37:10.865Z",
    "type": "identify",
    "userId": "sample_user_id"
  }
]

License

  • I understand, that my code will be licensed under MIT license (copy of license is available in this repo)
@pgiannone33 pgiannone33 changed the title Convert Timestamp to Local Date and Time Convert UTC Timestamp to Local Date and Time Apr 7, 2023
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

1 participant