diff options
| author | Kristian Evers <kristianevers@gmail.com> | 2022-03-15 08:52:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-15 08:52:43 +0100 |
| commit | 8f614bb9e4b7628f5363ccb48a33ba771704b988 (patch) | |
| tree | d069716e496413149adc13e5f9e709cfce4837b9 /src | |
| parent | fb4f815a09f98b5581f6cac0f232035101fcce23 (diff) | |
| parent | ac069da7de745fb231309c7a5c2c42706b49ad8d (diff) | |
| download | PROJ-8f614bb9e4b7628f5363ccb48a33ba771704b988.tar.gz PROJ-8f614bb9e4b7628f5363ccb48a33ba771704b988.zip | |
Merge pull request #3117 from OSGeo/backport-3111-to-9.0
[Backport 9.0] unitconvert: round to nearest date when converting to yyyymmdd.
Diffstat (limited to 'src')
| -rw-r--r-- | src/conversions/unitconvert.cpp | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/src/conversions/unitconvert.cpp b/src/conversions/unitconvert.cpp index d584d93f..991eb2e0 100644 --- a/src/conversions/unitconvert.cpp +++ b/src/conversions/unitconvert.cpp @@ -252,21 +252,22 @@ static double yyyymmdd_to_mjd(double yyyymmdd) { /***********************************************************************/ static double mjd_to_yyyymmdd(double mjd) { /************************************************************************ - Date given in YYYY-MM-DD format. + Date returned in YYYY-MM-DD format. ************************************************************************/ - double mjd_iter = 14 + 31; - int year = 1859, month=0, day=0; + unsigned int date_iter = 14 + 31; + unsigned int year = 1859, month = 0, day = 0; + unsigned int date = (int) lround(mjd); - for (; mjd >= mjd_iter; year++) { - mjd_iter += days_in_year(year); + for (; date >= date_iter; year++) { + date_iter += days_in_year(year); } year--; - mjd_iter -= days_in_year(year); + date_iter -= days_in_year(year); - for (month=1; mjd_iter + days_in_month(year, month) <= mjd; month++) - mjd_iter += days_in_month(year, month); + for (month=1; date_iter + days_in_month(year, month) <= date; month++) + date_iter += days_in_month(year, month); - day = (int)(mjd - mjd_iter + 1); + day = date - date_iter + 1; return year*10000.0 + month*100.0 + day; } |
