Skip to content

Commit eea4cf5

Browse files
committed
added comment from copilot
1 parent 0cd2966 commit eea4cf5

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

R/utils.R

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,45 @@ has_star <- function(x) {
5757
#' `format(as.POSIXct(x, tz=""), format="%Y-%m-%d")` to get a string with format
5858
#' YYYY-MM-DD.
5959
#'
60-
# TODO: check the previous statements
60+
# TODO: check the previous and following statements
61+
#
62+
# ACCORDING TO Copilot:
63+
# ### **Analysis**:
64+
# 1. **Excel Date Storage**:
65+
# - Excel does **not** store dates as the number of days since January 1, 1970. Instead:
66+
# - Excel stores dates as the number of days since **January 1, 1900** (for Windows systems) or **January 1, 1904** (for macOS systems).
67+
# - Excel also incorrectly assumes that 1900 was a leap year, which introduces an offset of 1 day for dates before March 1, 1900.
68+
#
69+
# 2. **`read_excel` Behavior**:
70+
# - When using `readxl::read_excel`, Excel dates are typically read as numeric values representing the number of days since Excel's epoch (e.g., 1900 or 1904). These values are **not automatically converted to POSIXct** by `read_excel`. The user must manually convert them.
71+
#
72+
# 3. **POSIXct Conversion**:
73+
# - The description mentions converting the number to a date using `as.POSIXct(as.integer(x))`. However:
74+
# - This assumes that the numeric value `x` is already in seconds since January 1, 1970, which is not the case for Excel dates.
75+
# - To convert Excel dates to R's `POSIXct`, you need to account for Excel's epoch (e.g., subtract the appropriate offset for 1900 or 1904).
76+
#
77+
# 4. **Formatting**:
78+
# - The description correctly states that `format(as.POSIXct(x, tz=""), format="%Y-%m-%d")` can be used to format a `POSIXct` object as a string in the `YYYY-MM-DD` format.
79+
#
80+
# ### **Corrected Description**:
81+
# Here’s a revised and accurate version of the description:
82+
#
83+
# Excel stores dates as numeric values representing the number of days since
84+
# January 1, 1900 (Windows) or January 1, 1904 (macOS). Note that Excel's 1900
85+
# date system incorrectly assumes 1900 was a leap year, which introduces a
86+
# 1-day offset for dates before March 1, 1900.
87+
#
88+
# When read using `readxl::read_excel`, Excel dates are imported as numeric
89+
# values. To convert these to R's `POSIXct` class, you must account for Excel's
90+
# epoch. For example, subtract 25569 days (the number of days between January 1,
91+
# 1900, and January 1, 1970) and convert to seconds by multiplying by 86400.
92+
#
93+
# Example conversion:
94+
# `as.POSIXct((x - 25569) * 86400, origin = "1970-01-01", tz = "")`
95+
#
96+
# To format the date as a string in `YYYY-MM-DD` format, use:
97+
# `format(as.POSIXct(...), format = "%Y-%m-%d")`.
98+
#
6199
#' @return A vector of the specified atomic class
62100
#' @noRd
63101
coerce <- function(x, atomicclass) {

0 commit comments

Comments
 (0)