static gboolean _g_date_time_same_day (GDateTime *dt1, GDateTime *dt2) { int y1, m1, d1; int y2, m2, d2; g_date_time_get_ymd (dt1, &y1, &m1, &d1); g_date_time_get_ymd (dt2, &y2, &m2, &d2); return (y1 == y2) && (m1 == m2) && (d1 == d2); }
bool DateTime::is_same_day(const DateTime& a, const DateTime& b) { // it's meaningless to compare uninitialized dates if (!a.m_dt || !b.m_dt) return false; int ay, am, ad; int by, bm, bd; g_date_time_get_ymd(a.get(), &ay, &am, &ad); g_date_time_get_ymd(b.get(), &by, &bm, &bd); return (ay==by) && (am==bm) && (ad==bd); }
GDate* gnc_g_date_new_today () { GDateTime *gdt = gnc_g_date_time_new_now_local (); gint day, month, year; GDate *result; g_date_time_get_ymd (gdt, &year, &month, &day); result = g_date_new_dmy (day, month, year); g_date_time_unref (gdt); g_assert(g_date_valid (result)); return result; }
static void gnc_g_date_time_fill_struct_tm (GDateTime *gdt, struct tm* time) { g_date_time_get_ymd (gdt, &(time->tm_year), &(time->tm_mon), &(time->tm_mday)); time->tm_sec = g_date_time_get_second (gdt); time->tm_min = g_date_time_get_minute (gdt); time->tm_hour = g_date_time_get_hour (gdt); // Watch out: struct tm has wday=0..6 with Sunday=0, but GDateTime has wday=1..7 with Sunday=7. time->tm_wday = g_date_time_get_day_of_week (gdt) % 7; time->tm_yday = g_date_time_get_day_of_year (gdt); time->tm_isdst = g_date_time_is_daylight_savings (gdt); time->tm_year -= 1900; --time->tm_mon; }
/** * compare_timestamps: * @a: a date * @b: a date to compare with * * Compare @a to @b and return how similar the dates are. * * Returns: a value from GlUtilTimestamps */ static GlUtilTimestamps compare_timestamps (GDateTime *a, GDateTime *b) { gint ayear, amonth, aday; gint byear, bmonth, bday; g_date_time_get_ymd (a, &ayear, &amonth, &aday); g_date_time_get_ymd (b, &byear, &bmonth, &bday); if (ayear != byear) { return GL_UTIL_TIMESTAMPS_DIFFERENT_YEAR; } /* Same year, month and day. */ if ((amonth == bmonth) && (aday == bday)) { return GL_UTIL_TIMESTAMPS_SAME_DAY; } /* Same year, but differing by month or day. */ return GL_UTIL_TIMESTAMPS_SAME_YEAR; }
/** * Creates a GDate set to yesterday. * * * @return A GDate with year, month, and day set to yesterday. */ GDate *yesterday(void) { GTimeZone *tz = g_time_zone_new_local(); GDateTime *now = g_date_time_new_now(tz); g_time_zone_unref(tz); gint year, month, day; GDateTime *temp_copy = g_date_time_add_days(now, -1); g_date_time_unref(now); g_date_time_get_ymd(temp_copy, &year, &month, &day); g_date_time_unref(temp_copy); // and now we can create yesterday: GDate *yest = g_date_new_dmy(day, month, year); return yest; }
void CalendarDatePrivate::setDate(GDateTime * date, gboolean isForceLoad, GTimeSpan changed) { GDateTime* selectedDate = g_date_time_new_local(m_selectedYear, m_selectedMonth, m_selectedDayOfMonth, 1, 1, 1); if (!isTheSameDate(date, selectedDate)) { g_date_time_get_ymd(date, &(m_selectedYear), &(m_selectedMonth), &(m_selectedDayOfMonth)); switch (m_viewType) { case VIEWTYPE_DAY: updateDayView(isForceLoad, changed); break; case VIEWTYPE_MONTH: updateMonthView(isForceLoad, changed); break; case VIEWTYPE_YEAR: updateYearView(isForceLoad, changed); break; default: break; } } else { if (isForceLoad) { updateDayView(isForceLoad, changed); } } g_date_time_unref(selectedDate); }
/* Based on evolution/mail/message-list.c:filter_date() */ char * rb_utf_friendly_time (time_t date) { GDateTime *datetime, *now, *yesterday; int d, m, y; int nd, nm, ny; int yd, ym, yy; const char *format = NULL; char *str = NULL; if (date == 0) { return g_strdup (_("Never")); } now = g_date_time_new_now_local (); datetime = g_date_time_new_from_unix_local (date); g_date_time_get_ymd (datetime, &y, &m, &d); g_date_time_get_ymd (now, &ny, &nm, &nd); if (y == ny && m == nm && d == nd) { /* Translators: "friendly time" string for the current day, strftime format. like "Today 12:34 am" */ format = _("Today %I:%M %p"); } if (format == NULL) { yesterday = g_date_time_add_days (now, -1); g_date_time_get_ymd (yesterday, &yy, &ym, &yd); if (y == yy && m == ym && d == yd) { /* Translators: "friendly time" string for the previous day, * strftime format. e.g. "Yesterday 12:34 am" */ format = _("Yesterday %I:%M %p"); } g_date_time_unref (yesterday); } if (format == NULL) { int i; for (i = 2; i < 7; i++) { yesterday = g_date_time_add_days (now, -i); g_date_time_get_ymd (yesterday, &yy, &ym, &yd); if (y == yy && m == ym && d == yd) { /* Translators: "friendly time" string for a day in the current week, * strftime format. e.g. "Wed 12:34 am" */ format = _("%a %I:%M %p"); g_date_time_unref (yesterday); break; } g_date_time_unref (yesterday); } } if (format == NULL) { if (y == ny) { /* Translators: "friendly time" string for a day in the current year, * strftime format. e.g. "Feb 12 12:34 am" */ format = _("%b %d %I:%M %p"); } else { /* Translators: "friendly time" string for a day in a different year, * strftime format. e.g. "Feb 12 1997" */ format = _("%b %d %Y"); } } if (format != NULL) { str = g_date_time_format (datetime, format); } if (str == NULL) { /* impossible time or broken locale settings */ str = g_strdup (_("Unknown")); } g_date_time_unref (datetime); g_date_time_unref (now); return str; }
void DateTime::ymd(int& year, int& month, int& day) const { g_date_time_get_ymd(get(), &year, &month, &day); }
/** * gst_date_time_new_from_iso8601_string: * @string: ISO 8601-formatted datetime string. * * Tries to parse common variants of ISO-8601 datetime strings into a * #GstDateTime. Possible input formats are (for example): * 2012-06-30T22:46:43Z, 2012, 2012-06, 2012-06-30, 2012-06-30T22:46:43-0430, * 2012-06-30T22:46Z, 2012-06-30T22:46-0430, 2012-06-30 22:46, * 2012-06-30 22:46:43, 2012-06-00, 2012-00-00, 2012-00-30, 22:46:43Z, 22:46Z, * 22:46:43-0430, 22:46-0430, 22:46:30, 22:46 * If no date is provided, it is assumed to be "today" in the timezone * provided (if any), otherwise UTC. * * Free-function: gst_date_time_unref * * Returns: (transfer full) (nullable): a newly created #GstDateTime, * or %NULL on error */ GstDateTime * gst_date_time_new_from_iso8601_string (const gchar * string) { gint year = -1, month = -1, day = -1, hour = -1, minute = -1; gint gmt_offset_hour = -99, gmt_offset_min = -99; gdouble second = -1.0; gfloat tzoffset = 0.0; guint64 usecs; gint len, ret; g_return_val_if_fail (string != NULL, NULL); GST_DEBUG ("Parsing '%s' into a datetime", string); len = strlen (string); /* The input string is expected to start either with a year (4 digits) or * with an hour (2 digits). Hour must be followed by minute. In any case, * the string must be at least 4 characters long and start with 2 digits */ if (len < 4 || !g_ascii_isdigit (string[0]) || !g_ascii_isdigit (string[1])) return NULL; if (g_ascii_isdigit (string[2]) && g_ascii_isdigit (string[3])) { ret = sscanf (string, "%04d-%02d-%02d", &year, &month, &day); if (ret == 0) return NULL; if (ret == 3 && day <= 0) { ret = 2; day = -1; } if (ret >= 2 && month <= 0) { ret = 1; month = day = -1; } if (ret >= 1 && (year <= 0 || year > 9999 || month > 12 || day > 31)) return NULL; else if (ret >= 1 && len < 16) /* YMD is 10 chars. XMD + HM will be 16 chars. if it is less, * it make no sense to continue. We will stay with YMD. */ goto ymd; string += 10; /* Exit if there is no expeceted value on this stage */ if (!(*string == 'T' || *string == '-' || *string == ' ')) goto ymd; string += 1; } /* if hour or minute fails, then we will use only ymd. */ hour = g_ascii_strtoull (string, (gchar **) & string, 10); if (hour > 24 || *string != ':') goto ymd; /* minute */ minute = g_ascii_strtoull (string + 1, (gchar **) & string, 10); if (minute > 59) goto ymd; /* second */ if (*string == ':') { second = g_ascii_strtoull (string + 1, (gchar **) & string, 10); /* if we fail here, we still can reuse hour and minute. We * will still attempt to parse any timezone information */ if (second > 59) { second = -1.0; } else { /* microseconds */ if (*string == '.' || *string == ',') { const gchar *usec_start = string + 1; guint digits; usecs = g_ascii_strtoull (string + 1, (gchar **) & string, 10); if (usecs != G_MAXUINT64 && string > usec_start) { digits = (guint) (string - usec_start); second += (gdouble) usecs / pow (10.0, digits); } } } } if (*string == 'Z') goto ymd_hms; else { /* reuse some code from gst-plugins-base/gst-libs/gst/tag/gstxmptag.c */ gint gmt_offset = -1; gchar *plus_pos = NULL; gchar *neg_pos = NULL; gchar *pos = NULL; GST_LOG ("Checking for timezone information"); /* check if there is timezone info */ plus_pos = strrchr (string, '+'); neg_pos = strrchr (string, '-'); if (plus_pos) pos = plus_pos + 1; else if (neg_pos) pos = neg_pos + 1; if (pos && strlen (pos) >= 3) { gint ret_tz; if (pos[2] == ':') ret_tz = sscanf (pos, "%d:%d", &gmt_offset_hour, &gmt_offset_min); else ret_tz = sscanf (pos, "%02d%02d", &gmt_offset_hour, &gmt_offset_min); GST_DEBUG ("Parsing timezone: %s", pos); if (ret_tz == 2) { if (neg_pos != NULL && neg_pos + 1 == pos) { gmt_offset_hour *= -1; gmt_offset_min *= -1; } gmt_offset = gmt_offset_hour * 60 + gmt_offset_min; tzoffset = gmt_offset / 60.0; GST_LOG ("Timezone offset: %f (%d minutes)", tzoffset, gmt_offset); } else GST_WARNING ("Failed to parse timezone information"); } } ymd_hms: if (year == -1 || month == -1 || day == -1) { GDateTime *now_utc, *now_in_given_tz; /* No date was supplied: make it today */ now_utc = g_date_time_new_now_utc (); if (tzoffset != 0.0) { /* If a timezone offset was supplied, get the date of that timezone */ g_assert (gmt_offset_min != -99); g_assert (gmt_offset_hour != -99); now_in_given_tz = g_date_time_add_minutes (now_utc, (60 * gmt_offset_hour) + gmt_offset_min); g_date_time_unref (now_utc); } else { now_in_given_tz = now_utc; } g_date_time_get_ymd (now_in_given_tz, &year, &month, &day); g_date_time_unref (now_in_given_tz); } return gst_date_time_new (tzoffset, year, month, day, hour, minute, second); ymd: if (year == -1) { /* No date was supplied and time failed to parse */ return NULL; } return gst_date_time_new_ymd (year, month, day); }