Convert epoch to timestamp and vice-versa using in asl
search cancel

Convert epoch to timestamp and vice-versa using in asl

book

Article ID: 442170

calendar_today

Updated On:

Products

VMware Smart Assurance Network Observability

Issue/Introduction

You need to convert a specific time format (e.g., 26/05/2025 06:00:30) into an epoch timestamp (and vice versa) using ASL scripting, as standard numeric functions do not support this specific format.

Environment

All supported Smarts versions

Resolution

The following ASL script provides a method to manually calculate the epoch value from a custom string and then convert that epoch value back into the original string format.

Note: Custom ASL scripting falls beyond the standard scope of support. If you require further assistance with ASL customization, please contact your account manager to engage professional services.

START {
    .. eol
} do {
    // ==========================================
    // PART 1: CUSTOM TIMESTAMP STRING TO EPOCH
    // ==========================================
    custom_date = "26/05/2025 06:00:30";

    day_str   = substring(custom_date, 0, 2);
    month_str = substring(custom_date, 3, 2);
    year_str  = substring(custom_date, 6, 4);
    hour_str  = substring(custom_date, 11, 2);
    min_str   = substring(custom_date, 14, 2);
    sec_str   = substring(custom_date, 17, 2);

    day   = numeric(day_str);
    month = numeric(month_str);
    year  = numeric(year_str);
    hour  = numeric(hour_str);
    min   = numeric(min_str);
    sec   = numeric(sec_str);

    y = 1970;
    total_days = 0;
    leap_counter = 2;

    while (y < year) {
        if (leap_counter == 4) {
            total_days = total_days + 366;
            leap_counter = 0;
        } else {
            total_days = total_days + 365;
        }
        y = y + 1;
        leap_counter = leap_counter + 1;
    }

    month_days = table();
    month_days[1] = 31; month_days[2] = 28; month_days[3] = 31;
    month_days[4] = 30; month_days[5] = 31; month_days[6] = 30;
    month_days[7] = 31; month_days[8] = 31; month_days[9] = 30;
    month_days[10] = 31; month_days[11] = 30; month_days[12] = 31;

    if (leap_counter == 4) { month_days[2] = 29; }

    m = 1;
    while (m < month) {
        total_days = total_days + month_days[m];
        m = m + 1;
    }
    total_days = total_days + (day - 1);

    epoch_val = (total_days * 86400) + (hour * 3600) + (min * 60) + sec;
    print("SUCCESS! Calculated Epoch: " . epoch_val);


    // ==========================================
    // PART 2: EPOCH BACK TO CUSTOM TIMESTAMP STRING
    // ==========================================
    rem_seconds = epoch_val;

    rev_year = 1970;
    rev_leap = 2;
    seconds_in_year = 0;

    // 1. Extract the Year
    while (rem_seconds >= 0) {
        if (rev_leap == 4) {
            seconds_in_year = 31622400; // 366 days * 86400
        } else {
            seconds_in_year = 31536000; // 365 days * 86400
        }

        if (rem_seconds >= seconds_in_year) {
            rem_seconds = rem_seconds - seconds_in_year;
            rev_year = rev_year + 1;
            rev_leap = rev_leap + 1;
            if (rev_leap > 4) { rev_leap = 1; }
        } else {
            break;
        }
    }

    // 2. Adjust months for the identified year
    rev_month_days = table();
    rev_month_days[1] = 31; rev_month_days[2] = 28; rev_month_days[3] = 31;
    rev_month_days[4] = 30; rev_month_days[5] = 31; rev_month_days[6] = 30;
    rev_month_days[7] = 31; rev_month_days[8] = 31; rev_month_days[9] = 30;
    rev_month_days[10] = 31; rev_month_days[11] = 30; rev_month_days[12] = 31;

    if (rev_leap == 4) { rev_month_days[2] = 29; }

    // 3. Extract the Month
    rev_month = 1;
    while (rev_month <= 12) {
        seconds_in_month = rev_month_days[rev_month] * 86400;
        if (rem_seconds >= seconds_in_month) {
            rem_seconds = rem_seconds - seconds_in_month;
            rev_month = rev_month + 1;
        } else {
            break;
        }
    }

    // 4. Extract the Day
    rev_day = 1;
    while (rem_seconds >= 86400) {
        rem_seconds = rem_seconds - 86400;
        rev_day = rev_day + 1;
    }

    // 5. Extract Hours, Minutes, and Seconds
    rev_hour = 0;
    while (rem_seconds >= 3600) {
        rem_seconds = rem_seconds - 3600;
        rev_hour = rev_hour + 1;
    }

    rev_min = 0;
    while (rem_seconds >= 60) {
        rem_seconds = rem_seconds - 60;
        rev_min = rev_min + 1;
    }
    rev_sec = rem_seconds;

    // 6. Pad with leading zeros to maintain formatting rules
    out_day   = string(rev_day);   if (rev_day < 10)   { out_day = "0" . out_day; }
    out_month = string(rev_month); if (rev_month < 10) { out_month = "0" . out_month; }
    out_hour  = string(rev_hour);  if (rev_hour < 10)  { out_hour = "0" . out_hour; }
    out_min   = string(rev_min);   if (rev_min < 10)   { out_min = "0" . out_min; }
    out_sec   = string(rev_sec);   if (rev_sec < 10)   { out_sec = "0" . out_sec; }
    out_year  = string(rev_year);

    // 7. Piece together the target format
    final_timestamp = out_day . "/" . out_month . "/" . out_year . " " . out_hour . ":" . out_min . ":" . out_sec;

    print("REVERSED STRING OUTPUT: " . final_timestamp);
    stop();
}