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

Days of the Week evaluated as UTC and not the configured Time Zone #20

Open
aclarkot opened this issue Mar 16, 2023 · 7 comments
Open

Comments

@aclarkot
Copy link

aclarkot commented Mar 16, 2023

Describe the bug
When you specify a tag that includes days of the week E.g., '18:00->07:00,Saturday,Sunday'. The time-range (18:00->-7:00) is correctly evaluated using the time zone configured in the TZ variable. However, the days of the week (Saturday, Sunday) ignore the time zone and are evaluated as UTC. In my case TZ is set to 'New Zealand Standard Time' (UTC+13) so this is a major issue, the VMs don't shutdown until 1PM on Saturday.

To Reproduce
Steps to reproduce the behavior:

  1. Enable the autoshutdownschedule runbook
  2. Set a custom time zone using the TZ parameter
  3. Add a tag to a VM including days of the week

Expected behavior
The VM should shutdown at 00:00 according to the time zone in TZ on the specified day.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
The problem code block is below. Notice that 'Get-Date' will always return UTC unless it is modified with Get-Date.AddHours(X).

# If specified as day of week, check if today
if ([System.DayOfWeek].GetEnumValues() -contains $TimeRange) {
    if ($TimeRange -eq (Get-Date).DayOfWeek) {
        $parsedDay = Get-Date "00:00"
   }

I have hacked it to work for my scenario as below by adding 13 hours for NZST.

# If specified as day of week, check if today
if ([System.DayOfWeek].GetEnumValues() -contains $TimeRange) {
    if ($TimeRange -eq ((Get-Date).AddHours(13)).DayOfWeek) {
        $parsedDay = (Get-Date "00:00").Addhours(13)
    }

A proper solution is needed to always adjust Get-Date output according to the UTC offset required by the configured time zone.

@aclarkot aclarkot changed the title Days of the Week only evaluated as UTC and not the configured Time Zone Days of the Week evaluated as UTC and not the configured Time Zone Mar 16, 2023
@tomasrudh
Copy link
Owner

tomasrudh commented Mar 16, 2023

Hello aclarkot,

Thanks for reporting, what version of the script are you running? Make sure you test with the latest, 3.9.0.

Regards,
Tomas

@aclarkot
Copy link
Author

aclarkot commented Mar 20, 2023

I've tested with 3.9.0 and 3.9.2, the behavior is the same with both.

The workaround I implemented above is resolving the issue in my case. So, the script doesn't evaluate 'day of the week' with the correct time zone, it only uses UTC.

@tomasrudh
Copy link
Owner

tomasrudh commented Mar 23, 2023 via email

@aclarkot
Copy link
Author

Schedule Configuration:
image
Automation Account Variables:
image
Example Job Output from testing (VM should shutdown on Thursday):
image

@tomasrudh
Copy link
Owner

tomasrudh commented Mar 24, 2023 via email

@sivagithub25
Copy link

Hi Tom,
I was also going to report this error. Can I know if there is a fix for the same.
Issue: When the tag contains days of the week, the logic validate the day ( Sunday or Monday etc) in UTC only, it is not using the Timezone present in the variable.
Cheers,
Siva

@sivagithub25
Copy link

Hi @aclarkot and @tomasrudh,

I believe the code segment

if ($TimeRange -eq (Get-Date).DayOfWeek) {
$parsedDay = Get-Date "00:00"
}

Should be changed to

if ($TimeRange -eq [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([DateTime]::Now,$tz).dayofweek) {
$parsedDay = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([DateTime]::Now,$tz).date
}

Where ever the logic written by using Get-Date in the code, I think we need to consider using ConvertTimeBySystemTimeZoneId by passing the $tx variable.

@aclarkot , in your workaround, I believe $parsedDay = (Get-Date "00:00").Addhours(13) might have an issue. Because parseday variable is using to get the range for 24 hours of the day which needs to be start at 12:00 AM .

Cheers,
Siva

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

3 participants