PeteBaser
PeteBaser

Reputation: 313

Is there a way to set a date and time (timezone) in Azure DevOps CI/CD build pipeline

I have automated test running in my CI/CD build pipeline, but the time in DevOps is UTC and my assertions tests check the local time. Is there a way to set a time zone in my build pipeline?

Upvotes: 12

Views: 7734

Answers (5)

wei
wei

Reputation: 4767

If ubuntu agent is used, the following commands can be used.

  • List timezones: timedatectl list-timezones
  • Set timezone: sudo timedatectl set-timezone '<your timezone>'

Upvotes: 1

Happy Mittal
Happy Mittal

Reputation: 129

Asia/Kolkata and "sudo timedatectl set-timezone" is not working on mac OS vmimage. Use below task for mac.

 - task: CmdLine@2
    displayName: Set system Timezone to IST
    inputs:
      script: |
        sudo systemsetup -gettimezone
        sudo systemsetup -settimezone Asia/Calcutta
        sudo systemsetup -gettimezone

Also you can use this command to get the list of all valid timezones

sudo systemsetup -listtimezones

Upvotes: 2

flywhc
flywhc

Reputation: 51

Additional to other replies for Windows Powershell and Linux Bash, Mac OSX agent such xcode or Xamarin.iOS should use the script below to set timezone:

- task: Bash@3
    displayName: Set Mac OS X Timezone
    inputs:
        targetType: 'inline'
        script: sudo systemsetup -settimezone Asia/Shanghai

Upvotes: 0

June Lau
June Lau

Reputation: 191

using Powershell, you can do:

Get-TimeZone
Set-TimeZone "India Standard Time"
Get-TimeZone

Upvotes: 11

Glue Ops
Glue Ops

Reputation: 698

Yes. For example this simple BASH script run using a Microsoft Hosted Agent:

echo "checking date"
date
echo "setting date to Asia/Kolkata"
sudo timedatectl set-timezone "Asia/Kolkata"
date

The results as seen in the log:

2019-07-05T20:26:48.5992486Z checking date
2019-07-05T20:26:48.5992954Z Fri Jul  5 20:26:48 UTC 2019
2019-07-05T20:26:48.5993264Z setting date to Asia/Kolkata
2019-07-05T20:26:48.9107025Z Sat Jul  6 01:56:48 IST 2019

As you can see, you can manipulate the local time on the agent. I do not agree with the other poster that this is necessarily a bad thing to do in the context of running tests.

You put some extra code in your tests to account for the local / target time or you could add 1 line into your build agent and achieve the same thing.

It just depends, the devil is in the details. Be careful with how you handle time.

Upvotes: 7

Related Questions