Hello, I have a question regarding the date objects and timezones. My current understanding is, that a Date object does only save the days from the origin and no more information about timezones or other information (please correct me if I am wrong). But if I use d = as.Date("2024-11-11") or d = as.Date("2024-11-11", tz="America/New_York") the date object is displayed in my RStudio environment as "2024-11-11 UTC". If I try to read the timezone information from the date object in the ways possible for POSIX, I will get NULL as return: > attr(d, "tzone") NULL Also unclassing the object does not reveal any information about the timezone, unlike for POSIX objects > unclass(d) [1] 20038 In contrast, getting the timezone information with base::format will yield the timezone information > base::format(d, format="%Z") [1] "UTC" As for my current understanding, setting the tz Argument in as.Date will only have an effect if the input is a date-time such as POSIX and else it has no effect and it is not possible to set a timezone for an Date object. But is this the case if there is some timezone information present when using base::format or in the display in the RStudio environment? Is there any way to add timezone information to an Date object (other than UTC) and keeping it a Date object? And if not how is the Date object internally structured, so that it will return "UTC" when using base::format or in the RStudio environment? Is this due to some default behavior of R? Best regards, Luca My Session Info: R version 4.3.2 (2023-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 11 x64 (build 22631) Matrix products: default locale: [1] LC_COLLATE=German_Germany.utf8 LC_CTYPE=German_Germany.utf8??? LC_MONETARY=German_Germany.utf8 [4] LC_NUMERIC=C??????????????????? LC_TIME=German_Germany.utf8 time zone: Europe/Berlin tzcode source: internal
Hi, I might have misunderstood your point, but why should a Date object store a timezone, since timezone is an attribute of time, not date? Your tz-examples force R to present a timezone, resulting - this is my assumption - to default (=UTC) as there is no other information available. Best, Kimmo Luca Brinkmann via R-help kirjoitti 24.9.2024 klo 17.10:> Hello, > > I have a question regarding the date objects and timezones. My current > understanding is, that a Date object does only save the days from the > origin and no more information about timezones or other information > (please correct me if I am wrong). > But if I use > d = as.Date("2024-11-11") > or > d = as.Date("2024-11-11", tz="America/New_York") > the date object is displayed in my RStudio environment as "2024-11-11 > UTC". If I try to read the timezone information from the date object in > the ways possible for POSIX, I will get NULL as return: > > > attr(d, "tzone") > NULL > > Also unclassing the object does not reveal any information about the > timezone, unlike for POSIX objects > > unclass(d) > [1] 20038 > > In contrast, getting the timezone information with base::format will > yield the timezone information > > base::format(d, format="%Z") > [1] "UTC" > > As for my current understanding, setting the tz Argument in as.Date will > only have an effect if the input is a date-time such as POSIX and else > it has no effect and it is not possible to set a timezone for an Date > object. But is this the case if there is some timezone information > present when using base::format or in the display in the RStudio > environment? Is there any way to add timezone information to an Date > object (other than UTC) and keeping it a Date object? And if not how is > the Date object internally structured, so that it will return "UTC" when > using base::format or in the RStudio environment? Is this due to some > default behavior of R? > > Best regards, > > Luca > > > My Session Info: > R version 4.3.2 (2023-10-31 ucrt) > Platform: x86_64-w64-mingw32/x64 (64-bit) > Running under: Windows 11 x64 (build 22631) > > Matrix products: default > > > locale: > [1] LC_COLLATE=German_Germany.utf8 LC_CTYPE=German_Germany.utf8 > LC_MONETARY=German_Germany.utf8 > [4] LC_NUMERIC=C??????????????????? LC_TIME=German_Germany.utf8 > > time zone: Europe/Berlin > tzcode source: internal >
24 ???????? 2024 ?. 17:10:13 GMT+03:00, Luca Brinkmann via R-help <r-help at r-project.org> ?????:> My current > understanding is, that a Date object does only save the days from the > origin and no more information about timezones or other information > (please correct me if I am wrong).You are correct.> the date object is displayed in my RStudio environment as "2024-11-11 > UTC".This looks like a result of as.POSIXlt(<Date object>).> In contrast, getting the timezone information with base::format will > yield the timezone information > > base::format(d, format="%Z") > [1] "UTC"That's an implementation detail: the format() method for Date objects conjures a temporary POSIXlt object and formats the result, and POSIXlt objects can contain time zone information.> Is there any way to add timezone information to an Date > object (other than UTC) and keeping it a Date object?Not with a plain 'Date'. What should a date in a timezone mean? Start of day? End of day? The whole 24-hour interval (except when it lasts 23 or 25 hours due to DST)? Perhaps you could store one (or two for an interval) POSIXt object(s) if you need timezone information for your dates. -- Best regards, Ivan