SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

How GPS works
How GPS worksHow GPS works
How GPS worksAmit Garg
 
Introduction of GIS & Remote Sensing (RS)
Introduction of GIS & Remote Sensing (RS)Introduction of GIS & Remote Sensing (RS)
Introduction of GIS & Remote Sensing (RS)Subtain Hussain Syed
 
Coordinate systems (Lecture 3)
Coordinate systems (Lecture 3)Coordinate systems (Lecture 3)
Coordinate systems (Lecture 3)Olexiy Pogurelskiy
 
Spatial interpolation techniques
Spatial interpolation techniquesSpatial interpolation techniques
Spatial interpolation techniquesManisha Shrivastava
 
PROCESS OF GEOREFERENCING IN QGIS
PROCESS OF GEOREFERENCING IN QGISPROCESS OF GEOREFERENCING IN QGIS
PROCESS OF GEOREFERENCING IN QGISCalcutta University
 
GEOcoding and Dynamic segmentation
  GEOcoding  and Dynamic segmentation  GEOcoding  and Dynamic segmentation
GEOcoding and Dynamic segmentationAbhiram Kanigolla
 
Map Projections, Datums, GIS and GPS for Everyone
Map Projections, Datums, GIS and GPS for EveryoneMap Projections, Datums, GIS and GPS for Everyone
Map Projections, Datums, GIS and GPS for EveryoneDr. Geophysics
 
2013 PLSC Track, How to describe Accuracy, and Why does it matter? by Jon Pr...
2013 PLSC Track, How to describe Accuracy, and Why does it matter?  by Jon Pr...2013 PLSC Track, How to describe Accuracy, and Why does it matter?  by Jon Pr...
2013 PLSC Track, How to describe Accuracy, and Why does it matter? by Jon Pr...GIS in the Rockies
 
Spatial Data Science with R
Spatial Data Science with RSpatial Data Science with R
Spatial Data Science with Ramsantac
 
Ultra high power ieee802.3bt PoE PD/PSE, Injectors/Splitters
Ultra high power ieee802.3bt PoE PD/PSE, Injectors/Splitters Ultra high power ieee802.3bt PoE PD/PSE, Injectors/Splitters
Ultra high power ieee802.3bt PoE PD/PSE, Injectors/Splitters MITS Component & System Corp.
 
Data models in geographical information system(GIS)
Data models in geographical information system(GIS)Data models in geographical information system(GIS)
Data models in geographical information system(GIS)Pramoda Raj
 
GNSS - GPS Surveying
GNSS - GPS SurveyingGNSS - GPS Surveying
GNSS - GPS SurveyingOpenmaps
 
FOSS4G 2017 Spatial Sql for Rookies
FOSS4G 2017 Spatial Sql for RookiesFOSS4G 2017 Spatial Sql for Rookies
FOSS4G 2017 Spatial Sql for RookiesTodd Barr
 
Fast, Scalable Quantized Neural Network Inference on FPGAs with FINN and Logi...
Fast, Scalable Quantized Neural Network Inference on FPGAs with FINN and Logi...Fast, Scalable Quantized Neural Network Inference on FPGAs with FINN and Logi...
Fast, Scalable Quantized Neural Network Inference on FPGAs with FINN and Logi...KTN
 

Was ist angesagt? (20)

Data aquisition unit iii final
Data aquisition unit iii finalData aquisition unit iii final
Data aquisition unit iii final
 
Types of Ellipsoid
Types of EllipsoidTypes of Ellipsoid
Types of Ellipsoid
 
Sparsh
SparshSparsh
Sparsh
 
How GPS works
How GPS worksHow GPS works
How GPS works
 
Introduction of GIS & Remote Sensing (RS)
Introduction of GIS & Remote Sensing (RS)Introduction of GIS & Remote Sensing (RS)
Introduction of GIS & Remote Sensing (RS)
 
Coordinate systems (Lecture 3)
Coordinate systems (Lecture 3)Coordinate systems (Lecture 3)
Coordinate systems (Lecture 3)
 
Spatial interpolation techniques
Spatial interpolation techniquesSpatial interpolation techniques
Spatial interpolation techniques
 
GIS data structure
GIS data structureGIS data structure
GIS data structure
 
PROCESS OF GEOREFERENCING IN QGIS
PROCESS OF GEOREFERENCING IN QGISPROCESS OF GEOREFERENCING IN QGIS
PROCESS OF GEOREFERENCING IN QGIS
 
GEOcoding and Dynamic segmentation
  GEOcoding  and Dynamic segmentation  GEOcoding  and Dynamic segmentation
GEOcoding and Dynamic segmentation
 
Ethiopia geodetic network
Ethiopia geodetic network Ethiopia geodetic network
Ethiopia geodetic network
 
Map Projections, Datums, GIS and GPS for Everyone
Map Projections, Datums, GIS and GPS for EveryoneMap Projections, Datums, GIS and GPS for Everyone
Map Projections, Datums, GIS and GPS for Everyone
 
2013 PLSC Track, How to describe Accuracy, and Why does it matter? by Jon Pr...
2013 PLSC Track, How to describe Accuracy, and Why does it matter?  by Jon Pr...2013 PLSC Track, How to describe Accuracy, and Why does it matter?  by Jon Pr...
2013 PLSC Track, How to describe Accuracy, and Why does it matter? by Jon Pr...
 
Spatial Data Science with R
Spatial Data Science with RSpatial Data Science with R
Spatial Data Science with R
 
Ultra high power ieee802.3bt PoE PD/PSE, Injectors/Splitters
Ultra high power ieee802.3bt PoE PD/PSE, Injectors/Splitters Ultra high power ieee802.3bt PoE PD/PSE, Injectors/Splitters
Ultra high power ieee802.3bt PoE PD/PSE, Injectors/Splitters
 
Data models in geographical information system(GIS)
Data models in geographical information system(GIS)Data models in geographical information system(GIS)
Data models in geographical information system(GIS)
 
GNSS - GPS Surveying
GNSS - GPS SurveyingGNSS - GPS Surveying
GNSS - GPS Surveying
 
FOSS4G 2017 Spatial Sql for Rookies
FOSS4G 2017 Spatial Sql for RookiesFOSS4G 2017 Spatial Sql for Rookies
FOSS4G 2017 Spatial Sql for Rookies
 
Fast, Scalable Quantized Neural Network Inference on FPGAs with FINN and Logi...
Fast, Scalable Quantized Neural Network Inference on FPGAs with FINN and Logi...Fast, Scalable Quantized Neural Network Inference on FPGAs with FINN and Logi...
Fast, Scalable Quantized Neural Network Inference on FPGAs with FINN and Logi...
 
Agriculture drought with remote sensing
Agriculture drought with remote sensingAgriculture drought with remote sensing
Agriculture drought with remote sensing
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 
Mining socio-political and socio-economic signals from social media content
Mining socio-political and socio-economic signals from social media contentMining socio-political and socio-economic signals from social media content
Mining socio-political and socio-economic signals from social media contentVasileios Lampos
 

Ähnlich wie 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
 
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
 
Mining socio-political and socio-economic signals from social media content
Mining socio-political and socio-economic signals from social media contentMining socio-political and socio-economic signals from social media content
Mining socio-political and socio-economic signals from social media content
 

Kürzlich hochgeladen

A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sectoritnewsafrica
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 

Kürzlich hochgeladen (20)

A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 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