rudderlabs / community-user-transformations

MIT License
1 stars 1 forks source link

Convert UTC Timestamp to Local Date and Time #23

Open pgiannone33 opened 1 year ago

pgiannone33 commented 1 year ago

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:

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