Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-26753 DST issues
  3. MDL-25963

Fix DST adjustments in userdate function and add two dependant functions

XMLWordPrintable

    • Icon: Sub-task Sub-task
    • Resolution: Won't Fix
    • Icon: Major Major
    • None
    • 2.0
    • Libraries
    • MOODLE_20_STABLE
    • Moderate

      Replace function userdate with following code

      function userdate($date, $format = '', $timezone = 99, $fixday = true) {

      global $CFG;

      $strtimezone = NULL;
      if (!is_numeric($timezone))

      { $strtimezone = $timezone; }

      if (empty($format))

      { $format = get_string('strftimedaydatetime', 'langconfig'); }

      if (!empty($CFG->nofixday))

      { // Config.php can force %d not to be fixed. $fixday = false; }

      else if ($fixday)

      { $formatnoday = str_replace('%d', 'DD', $format); $fixday = ($formatnoday != $format); }

      //$date += dst_offset_on($date, $strtimezone);
      $dst = dst_offset_on($date, $strtimezone);
      $date +=$dst;
      $timezone = get_user_timezone_offset($timezone);

      global $SESSION;
      if(isset($SESSION->dst_offsettz) && trim($SESSION->dst_offsettz)!='')

      { return adjust_dsttimezone($date, $SESSION->dst_offsettz, $format, $dst); }

      if (abs($timezone) > 13) { /// Server time
      if ($fixday)

      { $datestring = strftime($formatnoday, $date); $daystring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $date))); $datestring = str_replace('DD', $daystring, $datestring); }

      else

      { $datestring = strftime($format, $date); }

      } else {
      $date += (int)($timezone * 3600);
      if ($fixday)

      { $datestring = gmstrftime($formatnoday, $date); $daystring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %d', $date))); $datestring = str_replace('DD', $daystring, $datestring); }

      else

      { $datestring = gmstrftime($format, $date); }

      }

      /// If we are running under Windows convert from windows encoding to UTF-8
      /// (because it's impossible to specify UTF-8 to fetch locale info in Win32)

      if ($CFG->ostype == 'WINDOWS') {
      if ($localewincharset = get_string('localewincharset', 'langconfig'))

      { $textlib = textlib_get_instance(); $datestring = $textlib->convert($datestring, $localewincharset, 'utf-8'); }

      }

      return $datestring;
      }

      /*

      • Function to change the date format from strftime supported format(e.g. %Y-%m-%d) to php date function format(Y-m-d)
        */
        function strftime_to_dateformat($strftime_format) {

      $caracs = array(
      // Day - no strf eq : S
      'd' => '%d', 'D' => '%a', 'j' => '%e', 'l' => '%A', 'N' => '%u', 'w' => '%w', 'z' => '%j',
      // Week - no date eq : %U, %W
      'W' => '%V',
      // Month - no strf eq : n, t
      'F' => '%B', 'm' => '%m', 'M' => '%b',
      // Year - no strf eq : L; no date eq : %C, %g
      'o' => '%G', 'Y' => '%Y', 'y' => '%y',
      // Time - no strf eq : B, G, u; no date eq : %r, %R, %T, %X
      'a' => '%P', 'A' => '%p', 'g' => '%l', 'h' => '%I', 'H' => '%H', 'i' => '%M', 's' => '%S',
      // Timezone - no strf eq : e, I, P, Z
      'O' => '%z', 'T' => '%Z',
      // Full Date / Time - no strf eq : c, r; no date eq : %c, %D, %F, %x
      'U' => '%s'
      );

      $caracs = array_flip($caracs);
      return strtr((string)$strftime_format, $caracs);
      }

      /*

      • For adjusting DST value for those timezones which are running under DST for a date ot datetime
        */
        function adjust_dsttimezone($date, $tz, $format='Y-m-d g:i A',$dst=0) {
        if(strstr($format, '%') != '') { $format = strftime_to_dateformat($format); }

        $timezone = new DateTimeZone($tz);
        $datetime = new DateTime(date('c', $date));
        $datetime->setTimeZone($timezone);
        if($dst > 0)

        { $datetime->modify('+'.trim($dst).' seconds'); }

        return $datetime->format($format);
        }

            Unassigned Unassigned
            anitamourya Anita Mourya
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.