SlideShare a Scribd company logo
Notes on Date & TimePart 1: Date Calculation Shuo Chen Shuo Chen (blog.csdn.net/Solstice) 2010/08
Contents of this serial Strategic date calculation Time keeping (in UTC) Time zone and daylight saving time Choices between local time and UTC time Time in a distributed system How leap seconds affect heartbeat protocol Source Code http://github.com/chenshuo/recipes/tree/master/datetime/ 2010/08 Shuo Chen (blog.csdn.net/Solstice) 2
Part 1: Strategic date calculation Date calculation with Julian Day Number Assume dates after 1900 until we talk about history Illustrations of magic formulas  Design a Date class Alternative designs Unit tests Libraries available Brief history of Gregorian calendar 2010/08 Shuo Chen (blog.csdn.net/Solstice) 3
Common date calc tasks How many days between two dates 2000-01-01 and 2008-08-08 Is 2012/01/01 a Sunday? When is the 1000th day after 2009/12/25? Would be trivial if dates map to consecutive integers and vice versa. Shuo Chen (blog.csdn.net/Solstice) 2010/08 4
Gregorian calendar Days in a month Leap year 97 leap years every 400 years A full cycle is 400 years, 400*365+97 = 146097d How to number each date with an integer given such an irregular pattern? 2010/08 Shuo Chen (blog.csdn.net/Solstice) 5
Convert Dates to Integers Day number of (year, month, day) is sum of Days before Jan 1 of the year,  def daysYear() as daysYear(year+1) = daysYear(year) + 365 + isLeap(year) daysYear(2004) = daysYear(2003) + 365 daysYear(2005) = daysYear(2004) + 366 Days in this year before 1st day of this month daysMonth(1) = 0, daysMonth(2) = 31, daysMonth(3) = 28 or 29, daysMonth(4) = 31, ... Days in this month, ie. day Before going on with rolling our own … 2010/08 Shuo Chen (blog.csdn.net/Solstice) 6
Julian Day Number The standard method of representing dates as consecutive positive integers Some day predates all record history as day 1 4700+ BC or so Integer arithmetic only, without look-up table  See http://www.faqs.org/faqs/calendars/faq/ http://en.wikipedia.org/wiki/Julian_day 2010/08 Shuo Chen (blog.csdn.net/Solstice) 7
Julian Day Number from Date Code in C, divisions are truncation 1 <= month <= 12, 1<= day Tons of magic numbers 2010/08 Shuo Chen (blog.csdn.net/Solstice) 8 inttoJulianDayNumber(int year, int month, int day) { int a = (14 - month) / 12; int y = year + 4800 - a; int m = month + 12 * a - 3;   return day + (153*m + 2) / 5 + y*365          + y/4 - y/100 + y/400 - 32045; }
Given a date (year, month, day), Julian Day Number is sum of Days before the year Days before the month Days in the month Why not use year and month directly? y and m are shifted year and month How does it work? 2010/08 Shuo Chen (blog.csdn.net/Solstice) 9 An offset to make day 0 is Nov 24, 4714 BC return day + (153*m + 2) / 5        + y*365 + y/4 - y/100 + y/400 - 32045;
Shift year by two months New year starts in March ... March is month 0 in shifted year February is the last month (no. 11) of shifted year Leap day (if any) will be the last day of pervious year Length of first 11 months (m=0..10) are fixed 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 2010/08 Shuo Chen (blog.csdn.net/Solstice) 10
Shifting is easy First three lines of toJulianDayNumber() month = [1, 2, 3,  4, 5, 6,  7, 8, 9,  10, 11, 12] After shifting, much easier to calculate Days before the year,  esp. days before the month 2010/08 Shuo Chen (blog.csdn.net/Solstice) 11 int a = (14 - month) / 12;  // = (month < 3) ? 1 : 0 int y = year + 4800 – a; int m = month + 12 * a – 3;
Days before the year The (shifted) year part of our magic formula The last three terms of the expression add in a day for each year that is a leap year. Recall that the leap day counts in pervious year The first term adds a day every 4 years The second subtracts a day back out every 100 years The third adds a day back in every 400 years To verify, calc how many leap years between 1~100 (24), 1~200 (48), 1~300 (72), 1~400 (97) 2010/08 Shuo Chen (blog.csdn.net/Solstice) 12 y*365 + y/4 - y/100 + y/400
Days before the month The most magical part in the formula is about m It gives cumulative sum of days in month daysBeforeMonth(March) = 0   	March is the first month daysBeforeMonth(April) = 31	March has 31 days daysBeforeMonth(May) = 61	plus April’s 30 days daysBeforeMonth(Feb) = 337	total days of March to Jan 2010/08 Shuo Chen (blog.csdn.net/Solstice) 13 daysBeforeMonth(m) := (153*m + 2) / 5
Days before the month, cont’d By shifting 2-month, first 11 months have fixed length, so it’s much easier to calc cumulative sum No matter the last month (Feb) has 28 or 29 days, it will be addressed in the days in the month part It can be done either with look-up table, built with std::partial_sum(), or a simple function We will see how the coefficients come from Algorithm 199, Comm. of the ACM, Aug 1963 2010/08 Shuo Chen (blog.csdn.net/Solstice) 14
Examples Calc Julian day number 2007-02-01	2454133 2007-02-28	2454160 2007-02-29*	2454161 2007-03-01	2454161 2008-02-01	2454498 2008-02-29	2454526 2008-03-01	2454527 isLeap(year) := (julianDay(year, 2, 29) != julianDay(year, 3, 1)) 2010/08 Shuo Chen (blog.csdn.net/Solstice) 15 28 365 337 366 29
Function for days before month Find a function  f(m), so that That is, find a line drills 	through each pair of dots 2010/08 Shuo Chen (blog.csdn.net/Solstice) 16
Find a straight line that fits Since the number of days of first 11 months are either 30 or 31, we try a straight line first, that is, a linear function 	y=ax+b To find out a and b, we finda line which is close to themiddle points of every pairs That is, find a line that fits xi = {0, 1, 2, 3, …, 11} yi = {0.5, 31.5, 61.5, 92.5, …, 337.5} 2010/08 Shuo Chen (blog.csdn.net/Solstice) 17
Simple linear regression The estimation of a and b are http://en.wikipedia.org/wiki/Simple_linear_regression Result  a = 30.601, b = 0.52564	(regress.m in code) To get integer coefficients, we could round a and b a = 30.6 and b = 0.5 	⇒ 	(306*x+5)/10 a = 30.6 and b = 0.4 	⇒ 	(153*x+2)/5 Both happen to give correct answers, we’re done. 2010/08 Shuo Chen (blog.csdn.net/Solstice) 18
Break down Julian Day Number Inverse function of  toJulianDayNumber() Could it possibly be correct? 2010/08 Shuo Chen (blog.csdn.net/Solstice) 19 YearMonthDaytoYearMonthDay(intjulianDayNumber) { int a = julianDayNumber + 32044; int b = (4*a + 3) / 146097; int c = a - ((b * 146097) / 4); int d = (4*c + 3) / 1461; int e = c - ((1461 * d) / 4); int m = (5*e + 2) / 153; YearMonthDayymd; ymd.day = e - ((153*m + 2) / 5) + 1; ymd.month = m + 3 - 12 * (m / 10); ymd.year = b*100 + d - 4800 + m/10;   return ymd; } structYearMonthDay { int year; int month; // [1..12] int day; };
Two nested cycles of calendar A cycle consists four phases, first three phases are in the same length, last phase has one more day Outter cycle, 4 centuries, 146097 days Phase 0, year 1~100, 24 leap years, 36524 days Phase 1 and 2, same length (one century) Phase 3, year 301~400, 25 leap years, 36525 days Inner cycle, 4 years, 1461 days Phase 0, 1st year, 365 days Phase 1 and 2, same length (one year) Phase 3, 4th year, leap year, 366 days 2010/08 Shuo Chen (blog.csdn.net/Solstice) 20
Examples of cycles Outer cycle, 2000-03-01~2400-02-29  (146097d) Phase 0, 	2000-03-01 ~ 2100-02-28	36524d Phase 1, 	2100-03-01 ~ 2200-02-28	36524d Phase 2, 	2200-03-01 ~ 2300-02-28	36524d Phase 3, 	2300-03-01 ~ 2400-02-29	36525d Inner cycle, 2000-03-01~2004-02-29  (1461d) Phase 0, 	2000-03-01 ~ 2001-02-28	365d Phase 1, 	2001-03-01 ~ 2002-02-28	365d Phase 2, 	2002-03-01 ~ 2003-02-28	365d Phase 3, 	2003-03-01 ~ 2004-02-29	366d 2010/08 Shuo Chen (blog.csdn.net/Solstice) 21
Determine phases in cycles Formulas to break a day x into phases and offsets n is length of phase (365 or 36524), 4n+1 is cycle len. p is phase, 0~3 for cycle 1, 4~7 for cycle 2, and so on b is start day of phase p d is offset of x in phase p Eg. Day 2000 is Phase 5 of inner cycle 174th day of that year 2010/08 Shuo Chen (blog.csdn.net/Solstice) 22
Put them together Review the code of toYearMonthDay()  Are you convinced? 2010/08 Shuo Chen (blog.csdn.net/Solstice) 23 int a = julianDayNumber + 32044; // 0-th day is Mar 1, epoch year int b = (4*a + 3) / 146097;      // century (phase of outer cycle) int c = a - ((b * 146097) / 4);  // days in the century int d = (4*c + 3) / 1461;        // year in the century (inner phase) int e = c - ((1461 * d) / 4);    // days in the year int m = (5*e + 2) / 153;         // shifted month in the year YearMonthDayymd; ymd.day = e - ((153*m + 2) / 5) + 1;  // day of the month ymd.month = m + 3 - 12 * (m / 10);    // normal month ymd.year = b*100 + d - 4800 + m/10;   // normal year
Final note Last inner cycle may be incomplete, but doesn’t matter. Eg. 2100 is not leap year Inner cycle 2096-03-01~2100-02-28 has 1460 days 2100-03-01 belongs to next (inner and outer) cycle Moving leap day to the end of year, simplified a lot The ancient Roman calendar started its year on 1st March Integer arithmetic can be tricky and useful 2010/08 Shuo Chen (blog.csdn.net/Solstice) 24
What do we have A function maps dates in Gregorian calendar to consecutive positive integers (known as Julian Day Number)  An inverse function breaks down Julian Day Number to year, month and day Be convinced those mappings work (I hope so.) Let’s put it into real program, design a Date class 2010/08 Shuo Chen (blog.csdn.net/Solstice) 25
Design a Date class For civil or business usage Not for historian (dates before 1900) Not for rocket science or astronomy Assuming the current Gregorian calendar always was, and always will be, in effect. See the history of adoption of Gregorian calendar at the end of this slides 2010/08 Shuo Chen (blog.csdn.net/Solstice) 26
Define data member(s) The text book way The professional way 2010/08 Shuo Chen (blog.csdn.net/Solstice) 27 class Date {  // ...  private: int year_; int month_; int day_; }; class Date {  // ...  private: intjulianDayNumber_; }; Full code: http://github.com/chenshuo/recipes/tree/master/datetime/
Define member functions Date() Date(int y, int m, int d) explicit Date(intjdn) explicit Date(const struct tm&) // default copy-ctor and assignment bool valid() const int year() const int month() const int day() const intweekDay() const structYearMonthDayyeaMonthDay() const string toIsoString() const 2010/08 Shuo Chen (blog.csdn.net/Solstice) 28
Design decisions Make it a value type, and make it immutable Doesn’t provide today() The date should be injected to the system, not simply taken from local time or GMT time.  Make testing simple. You don’t have to wait for years to get a date like 2012-02-29 for testing Month ranges in [1, 12] Let printf(“%4d-%02d-%02d”, date.year(), date.month(), date.day()) prints intuitive result Same as other date libraries except C’s struct tm 2010/08 Shuo Chen (blog.csdn.net/Solstice) 29
Unit Tests Inspired by The Elements of Programming Style 2/e, p.54 2010/08 Shuo Chen (blog.csdn.net/Solstice) 30 intjulianDayNumber = 2415021; // 1900-01-01 intweekDay = 1; // Monday int days[2][12+1] =  {     { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },     { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }}; for (int year = 1900; year < 2500; ++year) {   for (int month = 1; month <= 12; ++month) {     for (int day = 1; day <= days[isLeap(year)][month]; ++day) {   // check Date d(year, month, day) and d2(julianDayNumber)   ++julianDayNumber;   weekDay= (weekDay+1) % 7;     }   } }
Alternative designs Strong type Year, Month and Day The Most Important Design Guideline?, by Scott Meyers, IEEE Software, July/August 2004. Move toIsoString() outside of class How Non-Member Functions Improve Encapsulation, by Scott Meyers, CUJ February 2000. Item 18 and 23 of Effective C++ 3rd ed. Provides more features parseIsoString() 	// yyyy-mm-dd getNextMonday(), etc. 2010/08 Shuo Chen (blog.csdn.net/Solstice) 31
Available libraries Boost::date_time Use it if you need “customization and extension” Python datetime http://docs.python.org/library/datetime.html Ruby date http://ruby-doc.org/stdlib/libdoc/date/rdoc/ Java Joda Time http://joda-time.sourceforge.net/ 2010/08 Shuo Chen (blog.csdn.net/Solstice) 32
POSIX.1 2003 Doesn’t provide date function Date time calc based on Unix time, ie. Seconds since 1970-01-01 00:00:00 UTC Exposed to year 2038 problem on 32-bit platform http://code.google.com/p/y2038/ Will talk more in coming parts, when we talk UTC time and local time 2010/08 Shuo Chen (blog.csdn.net/Solstice) 33
A bit of history Calendars are notoriously idiosyncratic. http://en.wikipedia.org/wiki/Gregorian_calendar#History Gregorian calendar begins in 1582. Not all countries adopted at same time England, United States (ex. Alaska), 1752 $ cal Sep 1752  # try it on Linux 1700 was a leap year in England history Russia, 1918 October Revolution happened on 1917-11-07 Gregorian 1900 was a leap year in Russia history 2010/08 Shuo Chen (blog.csdn.net/Solstice) 34
To be continued Time keeping in programs Always record happened event in UTC time Time zones and daylight saving time Consider keep scheduled event in local time Do not hardcode time zone offset or DST rules Time in a distributed system Two timestamps from two hosts are not comparable, unless you know the clock skew of those two hosts NTP isn’t good enough for monitoring latency in LAN 2010/08 Shuo Chen (blog.csdn.net/Solstice) 35

More Related Content

What's hot

Tratado de-ifa-introducao
Tratado de-ifa-introducaoTratado de-ifa-introducao
Tratado de-ifa-introducao
Sérgio Mascarenhas
 
wgrib2
wgrib2wgrib2
wgrib2
Arulalan T
 
908 odu
908 odu908 odu
guia-practica-para-profesionales-de-ifa-chief-fama
guia-practica-para-profesionales-de-ifa-chief-famaguia-practica-para-profesionales-de-ifa-chief-fama
guia-practica-para-profesionales-de-ifa-chief-famamarmartinez555
 
67765852 orunmila-ifa-para-reza-os-256-odus
67765852 orunmila-ifa-para-reza-os-256-odus67765852 orunmila-ifa-para-reza-os-256-odus
67765852 orunmila-ifa-para-reza-os-256-odus
naldo stuart
 
51955795 9615963-odu-apostila-de-jogo-de-buzios-120819193203-phpapp02
51955795 9615963-odu-apostila-de-jogo-de-buzios-120819193203-phpapp0251955795 9615963-odu-apostila-de-jogo-de-buzios-120819193203-phpapp02
51955795 9615963-odu-apostila-de-jogo-de-buzios-120819193203-phpapp02
Wellington Duarte de Oliveira
 
Secretos de la santeria
Secretos de la santeriaSecretos de la santeria
Secretos de la santeriaMase Lobe
 
Analysis on The Presentation of Evidence Between the Adversary System in Mala...
Analysis on The Presentation of Evidence Between the Adversary System in Mala...Analysis on The Presentation of Evidence Between the Adversary System in Mala...
Analysis on The Presentation of Evidence Between the Adversary System in Mala...
 Selvakumar Balakrishnan
 
Patipembas de lucero
Patipembas de luceroPatipembas de lucero
Patipembas de lucero
Deadman La Nueva Orden
 
El libro de palo
El libro de paloEl libro de palo
El libro de palo
Victor Hugo
 
Livro Estudando o Oráculo
Livro Estudando o Oráculo Livro Estudando o Oráculo
Livro Estudando o Oráculo
Babalawo Bergson
 
Babalu aye-
Babalu aye-Babalu aye-
Babalu aye-
Sango_Leke
 
Oraculos yoruba
Oraculos yorubaOraculos yoruba
Oraculos yoruba
SangoLeke
 
El camino de los orishas
El camino de los orishasEl camino de los orishas
El camino de los orishas
Robert Rodriguez
 
236 obras-con-oshun
236 obras-con-oshun236 obras-con-oshun
236 obras-con-oshunOshun16
 
Similar facts evidence-Civil cases
Similar facts evidence-Civil casesSimilar facts evidence-Civil cases
Similar facts evidence-Civil cases
INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA
 
41074514 as-caidas-dos-buzios
41074514 as-caidas-dos-buzios41074514 as-caidas-dos-buzios
41074514 as-caidas-dos-buziosRodrigo Botelho
 

What's hot (20)

Tratado de-ifa-introducao
Tratado de-ifa-introducaoTratado de-ifa-introducao
Tratado de-ifa-introducao
 
wgrib2
wgrib2wgrib2
wgrib2
 
908 odu
908 odu908 odu
908 odu
 
Guillermo castro
Guillermo castroGuillermo castro
Guillermo castro
 
guia-practica-para-profesionales-de-ifa-chief-fama
guia-practica-para-profesionales-de-ifa-chief-famaguia-practica-para-profesionales-de-ifa-chief-fama
guia-practica-para-profesionales-de-ifa-chief-fama
 
67765852 orunmila-ifa-para-reza-os-256-odus
67765852 orunmila-ifa-para-reza-os-256-odus67765852 orunmila-ifa-para-reza-os-256-odus
67765852 orunmila-ifa-para-reza-os-256-odus
 
51955795 9615963-odu-apostila-de-jogo-de-buzios-120819193203-phpapp02
51955795 9615963-odu-apostila-de-jogo-de-buzios-120819193203-phpapp0251955795 9615963-odu-apostila-de-jogo-de-buzios-120819193203-phpapp02
51955795 9615963-odu-apostila-de-jogo-de-buzios-120819193203-phpapp02
 
Manual del oriate
Manual del oriateManual del oriate
Manual del oriate
 
Secretos de la santeria
Secretos de la santeriaSecretos de la santeria
Secretos de la santeria
 
Analysis on The Presentation of Evidence Between the Adversary System in Mala...
Analysis on The Presentation of Evidence Between the Adversary System in Mala...Analysis on The Presentation of Evidence Between the Adversary System in Mala...
Analysis on The Presentation of Evidence Between the Adversary System in Mala...
 
Patipembas de lucero
Patipembas de luceroPatipembas de lucero
Patipembas de lucero
 
El libro de palo
El libro de paloEl libro de palo
El libro de palo
 
Livro Estudando o Oráculo
Livro Estudando o Oráculo Livro Estudando o Oráculo
Livro Estudando o Oráculo
 
Babalu aye-
Babalu aye-Babalu aye-
Babalu aye-
 
Oraculos yoruba
Oraculos yorubaOraculos yoruba
Oraculos yoruba
 
908
908908
908
 
El camino de los orishas
El camino de los orishasEl camino de los orishas
El camino de los orishas
 
236 obras-con-oshun
236 obras-con-oshun236 obras-con-oshun
236 obras-con-oshun
 
Similar facts evidence-Civil cases
Similar facts evidence-Civil casesSimilar facts evidence-Civil cases
Similar facts evidence-Civil cases
 
41074514 as-caidas-dos-buzios
41074514 as-caidas-dos-buzios41074514 as-caidas-dos-buzios
41074514 as-caidas-dos-buzios
 

Viewers also liked

Where destructors meet threads
Where destructors meet threadsWhere destructors meet threads
Where destructors meet threads
Shuo Chen
 
Efficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverEfficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverShuo Chen
 
Muduo network library
Muduo network libraryMuduo network library
Muduo network library
Shuo Chen
 
4 rules-of-fractions1640-2
4 rules-of-fractions1640-24 rules-of-fractions1640-2
4 rules-of-fractions1640-2Dean Oros
 
Fraction to-decimal
Fraction to-decimalFraction to-decimal
Fraction to-decimal
sohailsun46
 
2 1 units_of_measurement
2 1 units_of_measurement2 1 units_of_measurement
2 1 units_of_measurementPaul Cuaresma
 
Units of measurement
Units of measurementUnits of measurement
Units of measurementBrenda Obando
 
The Area and Perimeter Pack
The Area and Perimeter PackThe Area and Perimeter Pack
The Area and Perimeter Pack
Teaching Ideas
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Shuo Chen
 
Fraction, Decimals and Percents - Math Review Jeopardy
Fraction, Decimals and Percents - Math Review JeopardyFraction, Decimals and Percents - Math Review Jeopardy
Fraction, Decimals and Percents - Math Review Jeopardy
Mr. Godin
 
Dates and ordinal numbers
Dates and ordinal numbersDates and ordinal numbers
Dates and ordinal numbers
MaiteSaenz
 
Fraction To Decimal
Fraction To DecimalFraction To Decimal
Fraction To DecimalDonna Furrey
 
Fractions and decimals made easy
Fractions and decimals made easyFractions and decimals made easy
Fractions and decimals made easy
santosh Mr dexter
 
Fractions - Add, Subtract, Multiply and Divide
Fractions - Add, Subtract, Multiply and DivideFractions - Add, Subtract, Multiply and Divide
Fractions - Add, Subtract, Multiply and Divide
sondrateer
 
Fractions
FractionsFractions
Fractions
Snehal Bhargava
 
4 Rules of Fractions
4 Rules of Fractions4 Rules of Fractions
4 Rules of Fractions
Steve Bishop
 

Viewers also liked (17)

Zurg part 1
Zurg part 1Zurg part 1
Zurg part 1
 
Where destructors meet threads
Where destructors meet threadsWhere destructors meet threads
Where destructors meet threads
 
Efficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverEfficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ server
 
Muduo network library
Muduo network libraryMuduo network library
Muduo network library
 
4 rules-of-fractions1640-2
4 rules-of-fractions1640-24 rules-of-fractions1640-2
4 rules-of-fractions1640-2
 
Fraction to-decimal
Fraction to-decimalFraction to-decimal
Fraction to-decimal
 
2 1 units_of_measurement
2 1 units_of_measurement2 1 units_of_measurement
2 1 units_of_measurement
 
Units of measurement
Units of measurementUnits of measurement
Units of measurement
 
The Area and Perimeter Pack
The Area and Perimeter PackThe Area and Perimeter Pack
The Area and Perimeter Pack
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
 
Fraction, Decimals and Percents - Math Review Jeopardy
Fraction, Decimals and Percents - Math Review JeopardyFraction, Decimals and Percents - Math Review Jeopardy
Fraction, Decimals and Percents - Math Review Jeopardy
 
Dates and ordinal numbers
Dates and ordinal numbersDates and ordinal numbers
Dates and ordinal numbers
 
Fraction To Decimal
Fraction To DecimalFraction To Decimal
Fraction To Decimal
 
Fractions and decimals made easy
Fractions and decimals made easyFractions and decimals made easy
Fractions and decimals made easy
 
Fractions - Add, Subtract, Multiply and Divide
Fractions - Add, Subtract, Multiply and DivideFractions - Add, Subtract, Multiply and Divide
Fractions - Add, Subtract, Multiply and Divide
 
Fractions
FractionsFractions
Fractions
 
4 Rules of Fractions
4 Rules of Fractions4 Rules of Fractions
4 Rules of Fractions
 

Similar to Datetime - Julian Date

Recreational mathematics magazine number 3 march 2015
Recreational mathematics magazine  number 3 march  2015Recreational mathematics magazine  number 3 march  2015
Recreational mathematics magazine number 3 march 2015
Θανάσης Δρούγας
 
This a PPT about The Calanders and more info about it
This a PPT about The Calanders and more info about itThis a PPT about The Calanders and more info about it
This a PPT about The Calanders and more info about it
PrincePayeng
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
allanh0526
 
Calendar
CalendarCalendar
Calendar
avinashshinde56
 
Date and time manipulation
Date and time manipulationDate and time manipulation
Date and time manipulationShahjahan Samoon
 
Dates calculators
Dates calculatorsDates calculators
Dates calculators
Mohammed Ali
 
7th pre alg -l1
7th pre alg -l17th pre alg -l1
7th pre alg -l1jdurst65
 
Hi,I have implemented increment() method. Please find the below up.pdf
Hi,I have implemented increment() method. Please find the below up.pdfHi,I have implemented increment() method. Please find the below up.pdf
Hi,I have implemented increment() method. Please find the below up.pdf
Ankitchhabra28
 
Ijetcas14 567
Ijetcas14 567Ijetcas14 567
Ijetcas14 567
Iasir Journals
 
Ch 34 calender and time
Ch   34 calender and timeCh   34 calender and time
Ch 34 calender and time
MehrFath
 
Whole Procedure of Equations of motion.
Whole Procedure of Equations of motion.Whole Procedure of Equations of motion.
Whole Procedure of Equations of motion.Nafria_duky
 
Counting-the time between dates in mathematics of Investments .pptx
Counting-the time between dates in mathematics of Investments .pptxCounting-the time between dates in mathematics of Investments .pptx
Counting-the time between dates in mathematics of Investments .pptx
marialuisarada
 
Unit 2 Algebra of Vectors.pptx
Unit 2 Algebra of Vectors.pptxUnit 2 Algebra of Vectors.pptx
Unit 2 Algebra of Vectors.pptx
erickojojuniorwurah
 
Brand new Date and Time API
Brand new Date and Time APIBrand new Date and Time API
Brand new Date and Time API
Kenji HASUNUMA
 
Brand New Date and Time API
Brand New Date and Time APIBrand New Date and Time API
Brand New Date and Time API
Kenji HASUNUMA
 
Excel DAYS360 Function
Excel DAYS360 FunctionExcel DAYS360 Function
Excel DAYS360 Function
Excel
 
Notes
NotesNotes
icse-10-march13-maths-question-paper-with-solution-2023.pdf
icse-10-march13-maths-question-paper-with-solution-2023.pdficse-10-march13-maths-question-paper-with-solution-2023.pdf
icse-10-march13-maths-question-paper-with-solution-2023.pdf
vani311954
 
8th pre alg -l1
8th pre alg -l18th pre alg -l1
8th pre alg -l1jdurst65
 

Similar to Datetime - Julian Date (20)

Recreational mathematics magazine number 3 march 2015
Recreational mathematics magazine  number 3 march  2015Recreational mathematics magazine  number 3 march  2015
Recreational mathematics magazine number 3 march 2015
 
This a PPT about The Calanders and more info about it
This a PPT about The Calanders and more info about itThis a PPT about The Calanders and more info about it
This a PPT about The Calanders and more info about it
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
 
Calendar
CalendarCalendar
Calendar
 
Date and time manipulation
Date and time manipulationDate and time manipulation
Date and time manipulation
 
Dates calculators
Dates calculatorsDates calculators
Dates calculators
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date time
 
7th pre alg -l1
7th pre alg -l17th pre alg -l1
7th pre alg -l1
 
Hi,I have implemented increment() method. Please find the below up.pdf
Hi,I have implemented increment() method. Please find the below up.pdfHi,I have implemented increment() method. Please find the below up.pdf
Hi,I have implemented increment() method. Please find the below up.pdf
 
Ijetcas14 567
Ijetcas14 567Ijetcas14 567
Ijetcas14 567
 
Ch 34 calender and time
Ch   34 calender and timeCh   34 calender and time
Ch 34 calender and time
 
Whole Procedure of Equations of motion.
Whole Procedure of Equations of motion.Whole Procedure of Equations of motion.
Whole Procedure of Equations of motion.
 
Counting-the time between dates in mathematics of Investments .pptx
Counting-the time between dates in mathematics of Investments .pptxCounting-the time between dates in mathematics of Investments .pptx
Counting-the time between dates in mathematics of Investments .pptx
 
Unit 2 Algebra of Vectors.pptx
Unit 2 Algebra of Vectors.pptxUnit 2 Algebra of Vectors.pptx
Unit 2 Algebra of Vectors.pptx
 
Brand new Date and Time API
Brand new Date and Time APIBrand new Date and Time API
Brand new Date and Time API
 
Brand New Date and Time API
Brand New Date and Time APIBrand New Date and Time API
Brand New Date and Time API
 
Excel DAYS360 Function
Excel DAYS360 FunctionExcel DAYS360 Function
Excel DAYS360 Function
 
Notes
NotesNotes
Notes
 
icse-10-march13-maths-question-paper-with-solution-2023.pdf
icse-10-march13-maths-question-paper-with-solution-2023.pdficse-10-march13-maths-question-paper-with-solution-2023.pdf
icse-10-march13-maths-question-paper-with-solution-2023.pdf
 
8th pre alg -l1
8th pre alg -l18th pre alg -l1
8th pre alg -l1
 

Recently uploaded

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 

Datetime - Julian Date

  • 1. Notes on Date & TimePart 1: Date Calculation Shuo Chen Shuo Chen (blog.csdn.net/Solstice) 2010/08
  • 2. Contents of this serial Strategic date calculation Time keeping (in UTC) Time zone and daylight saving time Choices between local time and UTC time Time in a distributed system How leap seconds affect heartbeat protocol Source Code http://github.com/chenshuo/recipes/tree/master/datetime/ 2010/08 Shuo Chen (blog.csdn.net/Solstice) 2
  • 3. Part 1: Strategic date calculation Date calculation with Julian Day Number Assume dates after 1900 until we talk about history Illustrations of magic formulas Design a Date class Alternative designs Unit tests Libraries available Brief history of Gregorian calendar 2010/08 Shuo Chen (blog.csdn.net/Solstice) 3
  • 4. Common date calc tasks How many days between two dates 2000-01-01 and 2008-08-08 Is 2012/01/01 a Sunday? When is the 1000th day after 2009/12/25? Would be trivial if dates map to consecutive integers and vice versa. Shuo Chen (blog.csdn.net/Solstice) 2010/08 4
  • 5. Gregorian calendar Days in a month Leap year 97 leap years every 400 years A full cycle is 400 years, 400*365+97 = 146097d How to number each date with an integer given such an irregular pattern? 2010/08 Shuo Chen (blog.csdn.net/Solstice) 5
  • 6. Convert Dates to Integers Day number of (year, month, day) is sum of Days before Jan 1 of the year, def daysYear() as daysYear(year+1) = daysYear(year) + 365 + isLeap(year) daysYear(2004) = daysYear(2003) + 365 daysYear(2005) = daysYear(2004) + 366 Days in this year before 1st day of this month daysMonth(1) = 0, daysMonth(2) = 31, daysMonth(3) = 28 or 29, daysMonth(4) = 31, ... Days in this month, ie. day Before going on with rolling our own … 2010/08 Shuo Chen (blog.csdn.net/Solstice) 6
  • 7. Julian Day Number The standard method of representing dates as consecutive positive integers Some day predates all record history as day 1 4700+ BC or so Integer arithmetic only, without look-up table See http://www.faqs.org/faqs/calendars/faq/ http://en.wikipedia.org/wiki/Julian_day 2010/08 Shuo Chen (blog.csdn.net/Solstice) 7
  • 8. Julian Day Number from Date Code in C, divisions are truncation 1 <= month <= 12, 1<= day Tons of magic numbers 2010/08 Shuo Chen (blog.csdn.net/Solstice) 8 inttoJulianDayNumber(int year, int month, int day) { int a = (14 - month) / 12; int y = year + 4800 - a; int m = month + 12 * a - 3; return day + (153*m + 2) / 5 + y*365 + y/4 - y/100 + y/400 - 32045; }
  • 9. Given a date (year, month, day), Julian Day Number is sum of Days before the year Days before the month Days in the month Why not use year and month directly? y and m are shifted year and month How does it work? 2010/08 Shuo Chen (blog.csdn.net/Solstice) 9 An offset to make day 0 is Nov 24, 4714 BC return day + (153*m + 2) / 5 + y*365 + y/4 - y/100 + y/400 - 32045;
  • 10. Shift year by two months New year starts in March ... March is month 0 in shifted year February is the last month (no. 11) of shifted year Leap day (if any) will be the last day of pervious year Length of first 11 months (m=0..10) are fixed 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 2010/08 Shuo Chen (blog.csdn.net/Solstice) 10
  • 11. Shifting is easy First three lines of toJulianDayNumber() month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] After shifting, much easier to calculate Days before the year, esp. days before the month 2010/08 Shuo Chen (blog.csdn.net/Solstice) 11 int a = (14 - month) / 12; // = (month < 3) ? 1 : 0 int y = year + 4800 – a; int m = month + 12 * a – 3;
  • 12. Days before the year The (shifted) year part of our magic formula The last three terms of the expression add in a day for each year that is a leap year. Recall that the leap day counts in pervious year The first term adds a day every 4 years The second subtracts a day back out every 100 years The third adds a day back in every 400 years To verify, calc how many leap years between 1~100 (24), 1~200 (48), 1~300 (72), 1~400 (97) 2010/08 Shuo Chen (blog.csdn.net/Solstice) 12 y*365 + y/4 - y/100 + y/400
  • 13. Days before the month The most magical part in the formula is about m It gives cumulative sum of days in month daysBeforeMonth(March) = 0 March is the first month daysBeforeMonth(April) = 31 March has 31 days daysBeforeMonth(May) = 61 plus April’s 30 days daysBeforeMonth(Feb) = 337 total days of March to Jan 2010/08 Shuo Chen (blog.csdn.net/Solstice) 13 daysBeforeMonth(m) := (153*m + 2) / 5
  • 14. Days before the month, cont’d By shifting 2-month, first 11 months have fixed length, so it’s much easier to calc cumulative sum No matter the last month (Feb) has 28 or 29 days, it will be addressed in the days in the month part It can be done either with look-up table, built with std::partial_sum(), or a simple function We will see how the coefficients come from Algorithm 199, Comm. of the ACM, Aug 1963 2010/08 Shuo Chen (blog.csdn.net/Solstice) 14
  • 15. Examples Calc Julian day number 2007-02-01 2454133 2007-02-28 2454160 2007-02-29* 2454161 2007-03-01 2454161 2008-02-01 2454498 2008-02-29 2454526 2008-03-01 2454527 isLeap(year) := (julianDay(year, 2, 29) != julianDay(year, 3, 1)) 2010/08 Shuo Chen (blog.csdn.net/Solstice) 15 28 365 337 366 29
  • 16. Function for days before month Find a function f(m), so that That is, find a line drills through each pair of dots 2010/08 Shuo Chen (blog.csdn.net/Solstice) 16
  • 17. Find a straight line that fits Since the number of days of first 11 months are either 30 or 31, we try a straight line first, that is, a linear function y=ax+b To find out a and b, we finda line which is close to themiddle points of every pairs That is, find a line that fits xi = {0, 1, 2, 3, …, 11} yi = {0.5, 31.5, 61.5, 92.5, …, 337.5} 2010/08 Shuo Chen (blog.csdn.net/Solstice) 17
  • 18. Simple linear regression The estimation of a and b are http://en.wikipedia.org/wiki/Simple_linear_regression Result a = 30.601, b = 0.52564 (regress.m in code) To get integer coefficients, we could round a and b a = 30.6 and b = 0.5 ⇒ (306*x+5)/10 a = 30.6 and b = 0.4 ⇒ (153*x+2)/5 Both happen to give correct answers, we’re done. 2010/08 Shuo Chen (blog.csdn.net/Solstice) 18
  • 19. Break down Julian Day Number Inverse function of toJulianDayNumber() Could it possibly be correct? 2010/08 Shuo Chen (blog.csdn.net/Solstice) 19 YearMonthDaytoYearMonthDay(intjulianDayNumber) { int a = julianDayNumber + 32044; int b = (4*a + 3) / 146097; int c = a - ((b * 146097) / 4); int d = (4*c + 3) / 1461; int e = c - ((1461 * d) / 4); int m = (5*e + 2) / 153; YearMonthDayymd; ymd.day = e - ((153*m + 2) / 5) + 1; ymd.month = m + 3 - 12 * (m / 10); ymd.year = b*100 + d - 4800 + m/10; return ymd; } structYearMonthDay { int year; int month; // [1..12] int day; };
  • 20. Two nested cycles of calendar A cycle consists four phases, first three phases are in the same length, last phase has one more day Outter cycle, 4 centuries, 146097 days Phase 0, year 1~100, 24 leap years, 36524 days Phase 1 and 2, same length (one century) Phase 3, year 301~400, 25 leap years, 36525 days Inner cycle, 4 years, 1461 days Phase 0, 1st year, 365 days Phase 1 and 2, same length (one year) Phase 3, 4th year, leap year, 366 days 2010/08 Shuo Chen (blog.csdn.net/Solstice) 20
  • 21. Examples of cycles Outer cycle, 2000-03-01~2400-02-29 (146097d) Phase 0, 2000-03-01 ~ 2100-02-28 36524d Phase 1, 2100-03-01 ~ 2200-02-28 36524d Phase 2, 2200-03-01 ~ 2300-02-28 36524d Phase 3, 2300-03-01 ~ 2400-02-29 36525d Inner cycle, 2000-03-01~2004-02-29 (1461d) Phase 0, 2000-03-01 ~ 2001-02-28 365d Phase 1, 2001-03-01 ~ 2002-02-28 365d Phase 2, 2002-03-01 ~ 2003-02-28 365d Phase 3, 2003-03-01 ~ 2004-02-29 366d 2010/08 Shuo Chen (blog.csdn.net/Solstice) 21
  • 22. Determine phases in cycles Formulas to break a day x into phases and offsets n is length of phase (365 or 36524), 4n+1 is cycle len. p is phase, 0~3 for cycle 1, 4~7 for cycle 2, and so on b is start day of phase p d is offset of x in phase p Eg. Day 2000 is Phase 5 of inner cycle 174th day of that year 2010/08 Shuo Chen (blog.csdn.net/Solstice) 22
  • 23. Put them together Review the code of toYearMonthDay() Are you convinced? 2010/08 Shuo Chen (blog.csdn.net/Solstice) 23 int a = julianDayNumber + 32044; // 0-th day is Mar 1, epoch year int b = (4*a + 3) / 146097; // century (phase of outer cycle) int c = a - ((b * 146097) / 4); // days in the century int d = (4*c + 3) / 1461; // year in the century (inner phase) int e = c - ((1461 * d) / 4); // days in the year int m = (5*e + 2) / 153; // shifted month in the year YearMonthDayymd; ymd.day = e - ((153*m + 2) / 5) + 1; // day of the month ymd.month = m + 3 - 12 * (m / 10); // normal month ymd.year = b*100 + d - 4800 + m/10; // normal year
  • 24. Final note Last inner cycle may be incomplete, but doesn’t matter. Eg. 2100 is not leap year Inner cycle 2096-03-01~2100-02-28 has 1460 days 2100-03-01 belongs to next (inner and outer) cycle Moving leap day to the end of year, simplified a lot The ancient Roman calendar started its year on 1st March Integer arithmetic can be tricky and useful 2010/08 Shuo Chen (blog.csdn.net/Solstice) 24
  • 25. What do we have A function maps dates in Gregorian calendar to consecutive positive integers (known as Julian Day Number) An inverse function breaks down Julian Day Number to year, month and day Be convinced those mappings work (I hope so.) Let’s put it into real program, design a Date class 2010/08 Shuo Chen (blog.csdn.net/Solstice) 25
  • 26. Design a Date class For civil or business usage Not for historian (dates before 1900) Not for rocket science or astronomy Assuming the current Gregorian calendar always was, and always will be, in effect. See the history of adoption of Gregorian calendar at the end of this slides 2010/08 Shuo Chen (blog.csdn.net/Solstice) 26
  • 27. Define data member(s) The text book way The professional way 2010/08 Shuo Chen (blog.csdn.net/Solstice) 27 class Date { // ... private: int year_; int month_; int day_; }; class Date { // ... private: intjulianDayNumber_; }; Full code: http://github.com/chenshuo/recipes/tree/master/datetime/
  • 28. Define member functions Date() Date(int y, int m, int d) explicit Date(intjdn) explicit Date(const struct tm&) // default copy-ctor and assignment bool valid() const int year() const int month() const int day() const intweekDay() const structYearMonthDayyeaMonthDay() const string toIsoString() const 2010/08 Shuo Chen (blog.csdn.net/Solstice) 28
  • 29. Design decisions Make it a value type, and make it immutable Doesn’t provide today() The date should be injected to the system, not simply taken from local time or GMT time. Make testing simple. You don’t have to wait for years to get a date like 2012-02-29 for testing Month ranges in [1, 12] Let printf(“%4d-%02d-%02d”, date.year(), date.month(), date.day()) prints intuitive result Same as other date libraries except C’s struct tm 2010/08 Shuo Chen (blog.csdn.net/Solstice) 29
  • 30. Unit Tests Inspired by The Elements of Programming Style 2/e, p.54 2010/08 Shuo Chen (blog.csdn.net/Solstice) 30 intjulianDayNumber = 2415021; // 1900-01-01 intweekDay = 1; // Monday int days[2][12+1] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }}; for (int year = 1900; year < 2500; ++year) { for (int month = 1; month <= 12; ++month) { for (int day = 1; day <= days[isLeap(year)][month]; ++day) { // check Date d(year, month, day) and d2(julianDayNumber) ++julianDayNumber; weekDay= (weekDay+1) % 7; } } }
  • 31. Alternative designs Strong type Year, Month and Day The Most Important Design Guideline?, by Scott Meyers, IEEE Software, July/August 2004. Move toIsoString() outside of class How Non-Member Functions Improve Encapsulation, by Scott Meyers, CUJ February 2000. Item 18 and 23 of Effective C++ 3rd ed. Provides more features parseIsoString() // yyyy-mm-dd getNextMonday(), etc. 2010/08 Shuo Chen (blog.csdn.net/Solstice) 31
  • 32. Available libraries Boost::date_time Use it if you need “customization and extension” Python datetime http://docs.python.org/library/datetime.html Ruby date http://ruby-doc.org/stdlib/libdoc/date/rdoc/ Java Joda Time http://joda-time.sourceforge.net/ 2010/08 Shuo Chen (blog.csdn.net/Solstice) 32
  • 33. POSIX.1 2003 Doesn’t provide date function Date time calc based on Unix time, ie. Seconds since 1970-01-01 00:00:00 UTC Exposed to year 2038 problem on 32-bit platform http://code.google.com/p/y2038/ Will talk more in coming parts, when we talk UTC time and local time 2010/08 Shuo Chen (blog.csdn.net/Solstice) 33
  • 34. A bit of history Calendars are notoriously idiosyncratic. http://en.wikipedia.org/wiki/Gregorian_calendar#History Gregorian calendar begins in 1582. Not all countries adopted at same time England, United States (ex. Alaska), 1752 $ cal Sep 1752 # try it on Linux 1700 was a leap year in England history Russia, 1918 October Revolution happened on 1917-11-07 Gregorian 1900 was a leap year in Russia history 2010/08 Shuo Chen (blog.csdn.net/Solstice) 34
  • 35. To be continued Time keeping in programs Always record happened event in UTC time Time zones and daylight saving time Consider keep scheduled event in local time Do not hardcode time zone offset or DST rules Time in a distributed system Two timestamps from two hosts are not comparable, unless you know the clock skew of those two hosts NTP isn’t good enough for monitoring latency in LAN 2010/08 Shuo Chen (blog.csdn.net/Solstice) 35