playMaker

Author Topic: Timezone converter  (Read 646 times)

zzap64

  • Playmaker Newbie
  • *
  • Posts: 35
Timezone converter
« on: May 22, 2023, 11:19:49 PM »
Howdy
I'm seeking an action to convert UTC time to my current timezone PST.

I did ask Chat GPT, the code compiled but it ultimately didnt work. :(

using UnityEngine;
using HutongGames.PlayMaker;
using System;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Time)]
    [Tooltip("Converts UTC time to PST time.")]
    public class ConvertUTCToPST : FsmStateAction
    {
        [RequiredField]
        [UIHint(UIHint.Variable)]
        [Tooltip("The UTC time to convert.")]
        public FsmFloat utcTime;

        [RequiredField]
        [UIHint(UIHint.Variable)]
        [Tooltip("The converted PST time.")]
        public FsmFloat pstTime;

        public override void Reset()
        {
            utcTime = null;
            pstTime = null;
        }

        public override void OnEnter()
        {
            // Get the current UTC time
            var utcNow = DateTime.UtcNow;

            // Convert UTC time to PST time
            var pstTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            var pstTimeValue = TimeZoneInfo.ConvertTime(utcNow, pstTimeZone).ToOADate();

            // Store the converted PST time
            pstTime.Value = (float)pstTimeValue;

            Finish();
        }
    }
}

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Timezone converter
« Reply #1 on: May 23, 2023, 07:10:41 AM »
Hi.
Does it give any errors?

zzap64

  • Playmaker Newbie
  • *
  • Posts: 35
Re: Timezone converter
« Reply #2 on: May 23, 2023, 11:50:33 AM »
no errors, it provides two variable options (see attachment) but no converted time is is output in the PST time :(

zzap64

  • Playmaker Newbie
  • *
  • Posts: 35
Re: Timezone converter
« Reply #3 on: May 23, 2023, 09:50:25 PM »
I solved it

The Action gets the UTC time and has a variable to deduct the number of hours
The purpose was to help with obtaining API info when the time data is set to UTC timezones.
This can easily be adjusted to add hours

code:


using UnityEngine;
using System;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Time)]
    [Tooltip("Gets the current UTC time, deducts a specified number of hours, and stores the result as a string.")]
    public class GetUTCTimeAndDeductHoursAsString : FsmStateAction
    {
        [Tooltip("Number of hours to deduct from the current UTC time.")]
        public FsmInt HoursToDeduct;

        [UIHint(UIHint.Variable)]
        [Tooltip("Store the resulting time as a string.")]
        public FsmString Result;

        [Tooltip("Store the resulting time as local time instead of UTC.")]
        public FsmBool StoreAsLocalTime;

        public override void OnEnter()
        {
            // Get current UTC time
            DateTime currentUtcTime = DateTime.UtcNow;

            // Deduct hours
            DateTime resultTime = currentUtcTime.AddHours(-HoursToDeduct.Value);

            // Convert to local time if requested
            if (StoreAsLocalTime.Value)
            {
                resultTime = resultTime.ToLocalTime();
            }

            // Convert DateTime to string using a desired format
            string resultString = resultTime.ToString("yyyy-MM-dd HH:mm:ss");

            // Store the result as a string
            Result.Value = resultString;

            Finish();
        }
    }
}



Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Timezone converter
« Reply #4 on: May 24, 2023, 01:08:01 AM »
ChatGPT is the goat (and will make us all unemployed rather sooner than later)

zzap64

  • Playmaker Newbie
  • *
  • Posts: 35
Re: Timezone converter
« Reply #5 on: May 24, 2023, 02:24:42 AM »
Amen Brother, for now it helps me move onto my next challenge :)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Timezone converter
« Reply #6 on: May 24, 2023, 07:41:12 AM »
ChatGPT is the goat (and will make us all unemployed rather sooner than later)
Nah, it will speed up many jobs as its easier to search and can help with simple scripts.
But it still gets all its data from the internet, i encountered already several times that it could not find a (correct) solution and i could find myself by searching with the right words.

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Timezone converter
« Reply #7 on: May 24, 2023, 10:17:39 AM »
I’m currently using it for most code tasks for both c# and playmaker actions. It’s not perfect by any means and it makes many mistakes but it always has gotten me to a working solution. And I’m learning a ton since it explains me everything. It’s really fun and immediate. Like having an assistant. For me it’s a game changer for sure.

zzap64

  • Playmaker Newbie
  • *
  • Posts: 35
Re: Timezone converter
« Reply #8 on: May 24, 2023, 12:50:35 PM »
In the process its helping me out a little understanding C# and thats a good positive but I do rely do 100% on Playmaker because its AWESOME!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Timezone converter
« Reply #9 on: May 24, 2023, 01:39:33 PM »
oh yes definitely, Its very useful.