0
0

Azure Functionsでdate-fnsのformat結果がローカル実行とAzure上で実行したときに差が出ていたメモ

Last updated at Posted at 2024-05-14

概要

前回でローカル実行環境を構築したが、日付をDBから取得してフォーマットしたところで問題が起きた。

環境 TimeZone format(d,'yyyy-MM-dd HH:mm:ss')
Azure UTC 2024-05-14 00:00:00
ローカル JST 2024-05-14 09:00:00

結論からいうと、TimeZoneのlocaleの問題であった。
ローカル実行時の環境変数TZUTCを設定することでローカルもAzure上と同じ挙動にできることを確認した。

date-fns formatより

Return the formatted date string in the given format. The result may vary by locale.
指定された形式で書式設定された日付文字列を返します。結果はロケールによって異なる場合があります。

Node.js TZ より

The TZ environment variable is used to specify the timezone configuration.
TZ 環境変数は、タイムゾーン構成を指定するために使用されます。

設定

ローカル設定ファイルに環境変数を追加する

local.settings.json
{
  "IsEncrypted": false,
  "Values": {
+   "TZ": "UTC",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsStorage": "<connection-string>",
    "MyBindingConnection": "<binding-connection-string>",
    "AzureWebJobs.HttpExample.Disabled": "true"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "*",
    "CORSCredentials": false
  },
  "ConnectionStrings": {
    "SQLConnectionString": "<sqlclient-connection-string>"
  }
}

試したがダメだったもの

ローカル実行時の環境変数にWEBSITE_TIME_ZONEを設定しても、date-fnsのformatの挙動は変わらなかった。

参考

Jestによるテスト実行時のタイムゾーンを固定する
vscode-jest

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0