I have a few places in the code where I need to use the TimeZone. I can get the timezone name using DateTime::TimeZone. Is it reasonable to put the timezone name in a constant? Or should it be in a variable?
我在代码中有几个地方需要使用TimeZone。我可以使用DateTime :: TimeZone获取时区名称。将时区名称放在常量中是否合理?或者它应该在变量中?
3 个解决方案
#1
5
If the value can change, use a variable, of it is guaranteed to stay the same, use a constant.
如果值可以更改,则使用变量,保证保持不变,使用常量。
For a timezone, the chance it will change is not big, but it is also not 0. The user can change the timezone and it would be nice if the program behaves accordingly.
对于一个时区,它改变的机会并不大,但它也不是0.用户可以改变时区,如果程序的行为相应会很好。
#2
3
I vote for variable. You could end up with different users in different timezones working with your application, even if you doubt that could ever happen. It happened to me a couple of years ago.
我投票给变量。即使您怀疑可能发生这种情况,您最终可能会在使用您的应用程序的不同时区中与不同的用户结束。几年前它发生在我身上。
#3
2
Look at where the timezone is coming from. Is it the timezone of a city? Make it constant. Is it the timezone of the user? Make it variable - we users travel all the time, and we reset the timezone on our laptops so that Outlook scheduling will work correctly at the new location.
看看时区的来源。它是一个城市的时区吗?让它保持不变。是用户的时区吗?让它变化 - 我们的用户一直在旅行,我们在笔记本电脑上重置时区,以便Outlook调度在新位置正常工作。
#1
5
If the value can change, use a variable, of it is guaranteed to stay the same, use a constant.
如果值可以更改,则使用变量,保证保持不变,使用常量。
For a timezone, the chance it will change is not big, but it is also not 0. The user can change the timezone and it would be nice if the program behaves accordingly.
对于一个时区,它改变的机会并不大,但它也不是0.用户可以改变时区,如果程序的行为相应会很好。
#2
3
I vote for variable. You could end up with different users in different timezones working with your application, even if you doubt that could ever happen. It happened to me a couple of years ago.
我投票给变量。即使您怀疑可能发生这种情况,您最终可能会在使用您的应用程序的不同时区中与不同的用户结束。几年前它发生在我身上。
#3
2
Look at where the timezone is coming from. Is it the timezone of a city? Make it constant. Is it the timezone of the user? Make it variable - we users travel all the time, and we reset the timezone on our laptops so that Outlook scheduling will work correctly at the new location.
看看时区的来源。它是一个城市的时区吗?让它保持不变。是用户的时区吗?让它变化 - 我们的用户一直在旅行,我们在笔记本电脑上重置时区,以便Outlook调度在新位置正常工作。