~ubuntu-branches/debian/squeeze/tasks/squeeze

« back to all changes in this revision

Viewing changes to libkoto/koto-date-parser.c

  • Committer: Bazaar Package Importer
  • Author(s): Ross Burton, Ross Burton, Loïc Minier, Emilio Pozuelo Monfort
  • Date: 2009-08-10 10:08:52 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090810100852-6gp4pydhwflg4vnb
Tags: 0.16-1
[ Ross Burton ]
* New upstream release (Closes: #539510)
* Fix debug package section and depends
* Bump Standards

[ Loïc Minier ]
* Set LDFLAGS directly; bump cdbs bdep to >= 0.4.41.
* Create tasks.pot during build: Add a common-build-arch:: snippet to call
  langpack.mk's langpack-mk-update-pot target with
  DEB_BUILDDIR=$(DEB_SRCDIR); intltool-update can't be called in builddir as
  it needs POTFILES.in and even with it wont lookup files listed there in
  srcdir; add a clean:: snippet to remove the pot file; LP: #188690.

[ Emilio Pozuelo Monfort ]
* Wrap build-deps and deps.

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
populate_hash (GHashTable *hash, const char **aliases, int count)
51
51
{
52
52
  int i;
53
 
  
 
53
 
54
54
  g_assert (hash);
55
55
  g_assert (aliases);
56
56
  g_assert (count);
57
57
 
58
58
  for (i = 0; i < count; i++) {
59
59
    char **tokens, **s;
60
 
    
 
60
 
61
61
    if (aliases[i] == NULL)
62
62
      continue;
63
 
    
 
63
 
64
64
    tokens = g_strsplit (_(aliases[i]), "|", 0);
65
65
    for (s = tokens; *s; s++) {
66
66
      g_hash_table_insert (hash, g_utf8_casefold (*s, -1), GINT_TO_POINTER (i));
85
85
    N_("saturday|sat"),
86
86
    N_("sunday|sun"),
87
87
  };
88
 
  
 
88
 
89
89
  day_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
90
90
  populate_hash (day_hash, aliases, G_N_ELEMENTS (aliases));
91
91
}
111
111
    N_("november|nov"),
112
112
    N_("december|dec"),
113
113
  };
114
 
  
 
114
 
115
115
  month_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
116
116
  populate_hash (month_hash, aliases, G_N_ELEMENTS (aliases));
117
117
}
122
122
  char *folded;
123
123
  GDateWeekday day;
124
124
  g_assert (s);
125
 
  
 
125
 
126
126
  folded = g_utf8_casefold (s, -1);
127
127
#if DEBUG
128
128
  g_printerr ("Converting '%s' to a week day\n", folded);
163
163
#if BUILD_TESTS
164
164
  g_date_set_dmy (date, 11, 3, 2008);
165
165
#else
166
 
  g_date_set_time_t (date, time (NULL)); 
 
166
  g_date_set_time_t (date, time (NULL));
167
167
#endif
168
168
  return date;
169
169
}
201
201
  char *s;
202
202
  GDateDay current_day, next_day;
203
203
  int add_days;
204
 
  
 
204
 
205
205
  s = g_match_info_fetch_named (info, "day");
206
206
  next_day = convert_str_weekday (s);
207
207
  g_free (s);
208
208
  if (next_day == G_DATE_BAD_WEEKDAY)
209
209
    return NULL;
210
 
  
 
210
 
211
211
  date = today ();
212
212
  current_day = g_date_get_weekday (date);
213
213
  add_days = 7 + next_day - current_day;
214
214
 
215
215
  g_date_add_days (date, add_days);
216
 
  
 
216
 
217
217
  return date;
218
218
}
219
219
 
224
224
  char *s;
225
225
  GDateDay today_day;
226
226
  GDateWeekday target_day;
227
 
  
 
227
 
228
228
  s = g_match_info_fetch_named (info, "day");
229
229
  target_day = convert_str_weekday (s);
230
230
  g_free (s);
231
231
  if (target_day == G_DATE_BAD_WEEKDAY)
232
232
    return NULL;
233
 
    
 
233
 
234
234
  date = today ();
235
235
  today_day = g_date_get_day (date);
236
236
  g_date_subtract_days (date, g_date_get_weekday (date));
238
238
 
239
239
  if (g_date_get_day (date) <= today_day)
240
240
    g_date_add_days (date, 7);
241
 
  
 
241
 
242
242
  return date;
243
243
}
244
244
 
261
261
    return NULL;
262
262
  month = atoi (month_s);
263
263
  g_free (month_s);
264
 
  
 
264
 
265
265
  if (!g_date_valid_day (day) || !g_date_valid_month (month))
266
266
    return NULL;
267
267
 
268
268
  date = today ();
269
269
  g_date_set_day (date, day);
270
270
  g_date_set_month (date, month);
271
 
  
 
271
 
272
272
  return date;
273
273
}
274
274
 
294
294
 
295
295
  date = today ();
296
296
  g_date_set_day (date, day);
297
 
  
 
297
 
298
298
  return date;
299
299
}
300
300
 
322
322
  date = today ();
323
323
  g_date_set_day (date, day);
324
324
  g_date_set_month (date, month);
325
 
  
 
325
 
326
326
  return date;
327
327
}
328
328
 
335
335
  s = g_match_info_fetch_named (info, "digits");
336
336
  if (!s)
337
337
    return NULL;
338
 
  
 
338
 
339
339
  date = g_date_new ();
340
340
  g_date_set_parse (date, s);
341
341
  g_free (s);
342
 
  
 
342
 
343
343
  if (g_date_valid (date)) {
344
344
    return date;
345
345
  } else {
354
354
  GDate *date;
355
355
  char *week_s;
356
356
  int week, this_week;
357
 
  
 
357
 
358
358
  week_s = g_match_info_fetch_named (info, "week");
359
359
  if (!week_s)
360
360
    return NULL;
366
366
 
367
367
  date = today ();
368
368
  this_week = g_date_get_iso8601_week_of_year (date);
369
 
  
 
369
 
370
370
  /* Go to the Monday in week 1 */
371
371
  g_date_set_day (date, 1);
372
372
  g_date_set_month (date, G_DATE_JANUARY);
385
385
  GDate *date;
386
386
  char *days_s;
387
387
  int days;
388
 
  
 
388
 
389
389
  days_s = g_match_info_fetch_named (info, "days");
390
390
  if (!days_s)
391
391
    return NULL;
406
406
  GDate *date;
407
407
  char *weeks_s;
408
408
  int weeks;
409
 
  
 
409
 
410
410
  weeks_s = g_match_info_fetch_named (info, "weeks");
411
411
  if (!weeks_s)
412
412
    return NULL;
430
430
 
431
431
  g_assert (regex_str);
432
432
  g_assert (func);
433
 
  
 
433
 
434
434
  regex = g_regex_new (regex_str,
435
435
                       G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
436
436
                       &error);
437
 
  
 
437
 
438
438
  if (error) {
439
439
    g_warning ("Cannot parse regex '%s': %s", regex_str, error->message);
440
440
    g_error_free (error);
443
443
 
444
444
  m = g_slice_new (Matcher);
445
445
  m->regex = regex;
446
 
  m->func = func;  
 
446
  m->func = func;
447
447
  return m;
448
448
}
449
449
 
456
456
 
457
457
  init_days ();
458
458
  init_months ();
459
 
  
 
459
 
460
460
  /*
461
461
    Translators: WARNING: scary strings ahead.  If you don't understand regular
462
462
    expressions then skip these, otherwise
531
531
     translated.*/
532
532
  m = matcher_new (_("^(?<task>.+) in (?<weeks>\\d+) weeks?$"), match_weeks);
533
533
  if (m) matchers = g_list_prepend (matchers, m);
534
 
  
 
534
 
535
535
  return g_list_reverse (matchers);
536
536
}
537
537
 
544
544
#if DEBUG
545
545
  g_printerr ("Testing '%s'\n", s);
546
546
#endif
547
 
  
 
547
 
548
548
  if (matchers == NULL)
549
549
    matchers = init_matchers ();
550
 
  
 
550
 
551
551
  for (l = matchers; l != NULL; l = l->next) {
552
552
    Matcher *m = l->data;
553
553
    GMatchInfo *info;
560
560
      date = m->func (s, info);
561
561
      if (!date)
562
562
        goto next;
563
 
      
 
563
 
564
564
      if (!g_date_valid (date)) {
565
565
        g_date_free (date);
566
566
        goto next;
567
567
      }
568
 
      
 
568
 
569
569
      if (out_s) {
570
570
        *out_s = g_match_info_fetch_named (info, "task");
571
571
#if DEBUG
623
623
  char *s = NULL;
624
624
 
625
625
  /*
626
 
   * today() returns 11th March 2008 when running the test case.
 
626
   * today() returns Tuesday 11th March 2008 when running the test case.
627
627
   */
628
 
  
 
628
 
629
629
  /* Set the locale to C so we can do string comparisons easily */
630
630
  setlocale (LC_ALL, "C");
631
631
 
632
632
  /* Sanity checks for non-date strings */
633
 
  
 
633
 
634
634
  TEST_PARSER_FAIL ("foo");
635
635
  TEST_PARSER_FAIL ("foo and bar");
636
636
 
644
644
 
645
645
  g_date_subtract_days (expected, 2);
646
646
  TEST_PARSER ("Foo Yesterday", "Foo");
647
 
  
 
647
 
648
648
  /* On 21st, etc */
649
649
 
650
650
  g_date_set_dmy (expected, 1, 3, 2008);
661
661
 
662
662
  TEST_PARSER_FAIL ("foo by 999th");
663
663
  TEST_PARSER_FAIL ("foo on 32th");
664
 
  
 
664
 
665
665
  /* On 1st June, etc */
666
666
 
667
667
  g_date_set_dmy (expected, 1, 4, 2008);
680
680
  TEST_PARSER ("foo by jan 3rd", "foo");
681
681
 
682
682
  /* This/etc [weekday] */
683
 
  
 
683
 
684
684
  g_date_set_dmy (expected, 12, 3, 2008);
685
685
  TEST_PARSER ("foo this wednesday", "foo");
686
686
  TEST_PARSER ("foo this wed", "foo");
713
713
  TEST_PARSER_FAIL ("foo due fish");
714
714
  TEST_PARSER_FAIL ("foo by fish");
715
715
  TEST_PARSER_FAIL ("fish this flob");
716
 
  
 
716
 
717
717
  /* Next [weekday] */
718
718
 
719
719
  g_date_set_dmy (expected, 17, 3, 2008);
738
738
  TEST_PARSER ("foo next sunday", "foo");
739
739
 
740
740
  TEST_PARSER_FAIL ("foo next flobday");
741
 
  
 
741
 
742
742
  /* Digits.  Remember we're in in the C locale so the dates are m/d/y */
743
743
 
744
744
  g_date_set_dmy (expected, 17, 3, 2008);
759
759
  TEST_PARSER_FAIL ("foo on 4/31/07");
760
760
 
761
761
  /* Week numbers */
762
 
  
 
762
 
763
763
  g_date_set_dmy (expected, 17, 3, 2008);
764
764
  TEST_PARSER ("foo on w12", "foo");
765
765
 
810
810
  TEST_PARSER_FAIL ("foo in 0 weeks");
811
811
 
812
812
  g_date_free (expected);
813
 
  
 
813
 
814
814
  return 0;
815
815
}
816
816
#endif /* BUILD_TESTS */