SlideShare a Scribd company logo
1 of 35
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

Modul 5 bss parameter
Modul 5   bss parameterModul 5   bss parameter
Modul 5 bss parameterWijaya Kusuma
 
3 g parameter ericsson
3 g parameter ericsson3 g parameter ericsson
3 g parameter ericssonMitul Shah
 
2G optimization_with_optima
2G optimization_with_optima2G optimization_with_optima
2G optimization_with_optimaZIZI Yahia
 
3 g event description
3 g event description3 g event description
3 g event descriptionSaif Haider
 
Real time image processing in fpga
Real time image processing in fpgaReal time image processing in fpga
Real time image processing in fpgaSneha Nidhi
 
raster and random scan
raster and random scanraster and random scan
raster and random scanSonia Pahuja
 
Zte product description
Zte product descriptionZte product description
Zte product descriptionWijaya Kusuma
 
Study of inter and intra chip variations
Study of inter and intra chip variationsStudy of inter and intra chip variations
Study of inter and intra chip variationsRajesh M
 
Pci mod3,6,30 analysis and auto optimization
Pci mod3,6,30 analysis and auto optimizationPci mod3,6,30 analysis and auto optimization
Pci mod3,6,30 analysis and auto optimizationShuangquan Lei
 
Ericsson 2 g ran optimization complete training
Ericsson 2 g ran optimization complete trainingEricsson 2 g ran optimization complete training
Ericsson 2 g ran optimization complete trainingsekit123
 
163027027 location-routing-service-and-utran-area-planning-aspects-in-wcdma
163027027 location-routing-service-and-utran-area-planning-aspects-in-wcdma163027027 location-routing-service-and-utran-area-planning-aspects-in-wcdma
163027027 location-routing-service-and-utran-area-planning-aspects-in-wcdmaMery Muklis
 
3d transformation computer graphics
3d transformation computer graphics 3d transformation computer graphics
3d transformation computer graphics University of Potsdam
 
Drive test from a t z (part 3)-actix
Drive test from a t z (part 3)-actixDrive test from a t z (part 3)-actix
Drive test from a t z (part 3)-actixSyed Muhammad Zaidi
 
C++工程实践
C++工程实践C++工程实践
C++工程实践Shuo Chen
 
Cluster optimization procedure v1
Cluster optimization procedure v1Cluster optimization procedure v1
Cluster optimization procedure v1Terra Sacrifice
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Saikrishna Tanguturu
 

What's hot (20)

Modul 5 bss parameter
Modul 5   bss parameterModul 5   bss parameter
Modul 5 bss parameter
 
48315828 tensor-analysis
48315828 tensor-analysis48315828 tensor-analysis
48315828 tensor-analysis
 
3 g parameter ericsson
3 g parameter ericsson3 g parameter ericsson
3 g parameter ericsson
 
Zte umts-handover-description
Zte umts-handover-descriptionZte umts-handover-description
Zte umts-handover-description
 
2G optimization_with_optima
2G optimization_with_optima2G optimization_with_optima
2G optimization_with_optima
 
3 g event description
3 g event description3 g event description
3 g event description
 
Real time image processing in fpga
Real time image processing in fpgaReal time image processing in fpga
Real time image processing in fpga
 
raster and random scan
raster and random scanraster and random scan
raster and random scan
 
Zte product description
Zte product descriptionZte product description
Zte product description
 
Gsm hosr
Gsm hosrGsm hosr
Gsm hosr
 
Study of inter and intra chip variations
Study of inter and intra chip variationsStudy of inter and intra chip variations
Study of inter and intra chip variations
 
Pci mod3,6,30 analysis and auto optimization
Pci mod3,6,30 analysis and auto optimizationPci mod3,6,30 analysis and auto optimization
Pci mod3,6,30 analysis and auto optimization
 
Anr feature in lte
Anr feature in lteAnr feature in lte
Anr feature in lte
 
Ericsson 2 g ran optimization complete training
Ericsson 2 g ran optimization complete trainingEricsson 2 g ran optimization complete training
Ericsson 2 g ran optimization complete training
 
163027027 location-routing-service-and-utran-area-planning-aspects-in-wcdma
163027027 location-routing-service-and-utran-area-planning-aspects-in-wcdma163027027 location-routing-service-and-utran-area-planning-aspects-in-wcdma
163027027 location-routing-service-and-utran-area-planning-aspects-in-wcdma
 
3d transformation computer graphics
3d transformation computer graphics 3d transformation computer graphics
3d transformation computer graphics
 
Drive test from a t z (part 3)-actix
Drive test from a t z (part 3)-actixDrive test from a t z (part 3)-actix
Drive test from a t z (part 3)-actix
 
C++工程实践
C++工程实践C++工程实践
C++工程实践
 
Cluster optimization procedure v1
Cluster optimization procedure v1Cluster optimization procedure v1
Cluster optimization procedure v1
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
 

Viewers also liked

Where destructors meet threads
Where destructors meet threadsWhere destructors meet threads
Where destructors meet threadsShuo 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 libraryShuo 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-decimalsohailsun46
 
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 PackTeaching 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 JeopardyMr. Godin
 
Dates and ordinal numbers
Dates and ordinal numbersDates and ordinal numbers
Dates and ordinal numbersMaiteSaenz
 
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 easysantosh Mr dexter
 
Fractions - Add, Subtract, Multiply and Divide
Fractions - Add, Subtract, Multiply and DivideFractions - Add, Subtract, Multiply and Divide
Fractions - Add, Subtract, Multiply and Dividesondrateer
 
4 Rules of Fractions
4 Rules of Fractions4 Rules of Fractions
4 Rules of FractionsSteve 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 itPrincePayeng
 
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 3allanh0526
 
Date and time manipulation
Date and time manipulationDate and time manipulation
Date and time manipulationShahjahan Samoon
 
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.pdfAnkitchhabra28
 
Ch 34 calender and time
Ch   34 calender and timeCh   34 calender and time
Ch 34 calender and timeMehrFath
 
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 .pptxmarialuisarada
 
Brand new Date and Time API
Brand new Date and Time APIBrand new Date and Time API
Brand new Date and Time APIKenji HASUNUMA
 
Brand New Date and Time API
Brand New Date and Time APIBrand New Date and Time API
Brand New Date and Time APIKenji HASUNUMA
 
Excel DAYS360 Function
Excel DAYS360 FunctionExcel DAYS360 Function
Excel DAYS360 FunctionExcel
 
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.pdfvani311954
 
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

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Recently uploaded (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

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