Browse Source

Added daylight savings flag and ToEpochSecs() to SDateTime.

Brucey 2 years ago
parent
commit
1e1f46a392
2 changed files with 49 additions and 2 deletions
  1. 20 0
      stdc.mod/stdc.bmx
  2. 29 2
      stdc.mod/stdc.c

+ 20 - 0
stdc.mod/stdc.bmx

@@ -500,6 +500,15 @@ Struct SDateTime
 	bbdoc: The offset from UTC, in minutes.
 	End Rem
 	Field offset:Int
+	Rem
+	bbdoc: #True if the date time is observing daylight savings time, #False otherwise.
+    about: Daylight Saving Time (DST) is the practice of setting the clock ahead by one hour from standard time
+	during the warmer months, and then back again in the fall, in order to extend evening daylight and reduce the
+	need for artificial lighting. This can affect local time calculations, and so it's important to track whether a
+	given datetime object is observing DST. Note that not all regions observe DST, and the start and end dates
+	for DST can vary from one region to another.
+	End Rem
+	Field dst:Int
 
 	Rem
 	bbdoc: Returns a string representation of the date time in ISO 8601 format.
@@ -535,6 +544,16 @@ Struct SDateTime
 	Function FromEpoch:SDateTime(epochSecs:Long, fracNanoSecs:Long = 0)
 		Return bmx_datetime_from_epoch(epochSecs, fracNanoSecs)
 	End Function
+
+	Rem
+	bbdoc: Converts the current date and time to the number of seconds that have elapsed since the Unix Epoch.
+	returns: The number of seconds that have elapsed since the Unix Epoch, or -1 if the conversion failed.
+	about: The 'epoch' refers to the Unix epoch, which is a system for describing a point in time, defined as the number of seconds
+	that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, minus the number of leap seconds. 
+	End Rem
+	Method ToEpochSecs:Long()
+		Return bmx_datetime_to_epoch(Self)
+	End Method
 End Struct
 
 Rem
@@ -589,6 +608,7 @@ Extern "c"
 	Function Startup()="bb_stdc_Startup"
 	Function bmx_datetime_from_epoch:SDateTime(epochSecs:Long, fracNanoSecs:Long)
 	Function bmx_current_datetime_format:String(format:String)
+	Function bmx_datetime_to_epoch:Long(dt:SDateTime Var)
 End Extern
 
 Startup

+ 29 - 2
stdc.mod/stdc.c

@@ -924,14 +924,17 @@ typedef struct {
     int millisecond;
 	int utc;
 	int offset;
+	int dst;
 } SDateTime;
 
 #ifdef __WIN32__
-int bmx_calc_timeoffset_mins() {
+int bmx_calc_timeoffset_mins(SDateTime * dt) {
 	TIME_ZONE_INFORMATION tz;
 	DWORD rc = GetTimeZoneInformation(&tz);
 	int offset_minutes = tz.Bias + (TIME_ZONE_ID_DAYLIGHT != rc ?  tz.StandardBias : tz.DaylightBias);
 	
+	dt->dst = (rc == TIME_ZONE_ID_DAYLIGHT) ? 1 : 0;
+
     return offset_minutes;
 }
 
@@ -951,7 +954,7 @@ void bmx_current_datetime(SDateTime * dt, int utc) {
     dt->second = systemTime.wSecond;
     dt->millisecond = systemTime.wMilliseconds;
     dt->utc = utc;
-    dt->offset = utc ? 0 : bmx_calc_timeoffset_mins();
+    dt->offset = utc ? 0 : bmx_calc_timeoffset_mins(dt);
 }
 #else
 int bmx_calc_timeoffset_mins() {
@@ -983,8 +986,10 @@ void bmx_current_datetime(SDateTime * dt, int utc) {
 
     if (utc) {
         gmtime_r(&ts.tv_sec, &tm);
+		dt->dst = 0;
     } else {
         localtime_r(&ts.tv_sec, &tm);
+		dt->dst = tm.tm_isdst > 0 ? 1 : 0;
     }
 
     dt->year = tm.tm_year + 1900;
@@ -1020,10 +1025,32 @@ SDateTime bmx_datetime_from_epoch(BBLONG epochTimeSecs, BBLONG fracNanoseconds)
 
     dt.utc = 1;
     dt.offset = 0;
+	dt.dst = 0;
 
     return dt;
 }
 
+BBLONG bmx_datetime_to_epoch(SDateTime * dt) {
+    struct tm t;
+    time_t ts;
+
+    t.tm_year = dt->year - 1900;
+    t.tm_mon = dt->month - 1;
+    t.tm_mday = dt->day;
+    t.tm_hour = dt->hour;
+    t.tm_min = dt->minute;
+    t.tm_sec = dt->second;
+    t.tm_isdst = dt->dst;
+
+    // Convert struct tm to time_t (seconds since the Epoch)
+    ts = mktime(&t);
+    if (ts == -1) {
+        return -1;
+    }
+    
+    return (BBLONG)ts;
+}
+
 BBString * bmx_current_datetime_format(BBString * format) {
 	struct tm tm;
 	time_t rawtime;