| 1 | /* Copyright (c) 2012 by Michael J. Roberts. All Rights Reserved. */ |
| 2 | /* |
| 3 | Name |
| 4 | osstzprs.h - portable POSIX-style "TZ" variable parser |
| 5 | Function |
| 6 | This routine parses a POSIX-style "TZ" variable, filling in an |
| 7 | os_tzinfo_t structure for os_get_timezone_info(). This is a portable |
| 8 | routine designed as a helper for implementing os_get_timezone_info() on |
| 9 | platforms where POSIX TZ strings can be used. Many Unix platforms use this |
| 10 | convention. |
| 11 | |
| 12 | We parse the following formats: |
| 13 | |
| 14 | EST5 - time zone with standard time only, no daylight time |
| 15 | EST5EDT - standard plus daylight time, with implied 1:00 daylight offset |
| 16 | EST5EDT4 - standard plus daylight time, with explicit daylight offset |
| 17 | EST5:00 - offset can include minutes |
| 18 | EST5:00:00 - ...and seconds |
| 19 | EST5EDT,M3.2.0,M11.1.0 - adds rules for daylight time start/stop dates |
| 20 | |
| 21 | This isn't a full drop-in replacement for os_get_timezone_info(), since |
| 22 | (a) we don't want to assume that the Unix getenv() API is the way (or the |
| 23 | only way) to get the string to parse, and (b) some platforms that use the |
| 24 | TZ format might also accept other formats or have other ways of setting |
| 25 | the timezone. For Unix-type platforms platforms that don't need to check |
| 26 | for other syntax, this should be all you need to do to implement |
| 27 | os_get_timezone_info(): |
| 28 | |
| 29 | #include "osstzprs.h" |
| 30 | int os_get_timezone_info(struct os_tzinfo_t *info) |
| 31 | { |
| 32 | const char *tz = getenv("TZ"); |
| 33 | return tz != 0 ? oss_parse_posix_tz(info, tz, strlen(tz), TRUE) : FALSE; |
| 34 | } |
| 35 | |
| 36 | Notes |
| 37 | |
| 38 | Modified |
| 39 | 02/13/12 MJRoberts - Creation |
| 40 | */ |
| 41 | |
| 42 | #ifndef OSSTZPRS_H |
| 43 | #define OSSTZPRS_H |
| 44 | |
| 45 | #include "os.h" |
| 46 | |
| 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
| 51 | /* |
| 52 | * Parse a string using the syntax of the POSIX "TZ" environment variable. |
| 53 | * If successful, fills in *info and returns true; if the string isn't in |
| 54 | * one of the recognized formats, returns false. |
| 55 | * |
| 56 | * 'west_is_positive' says whether UTC offsets are positive or negative to |
| 57 | * the west. The POSIX TZ convention is that west is positive, as in |
| 58 | * EST5EDT (New York time, 5 hours west of UTC), so this should be set to |
| 59 | * TRUE if the source material actually comes from a TZ variable. The |
| 60 | * reason we offer this option at all is that the zoneinfo database uses |
| 61 | * the opposite convention, where New York would be EST-5EDT. |
| 62 | */ |
| 63 | int oss_parse_posix_tz(struct os_tzinfo_t *info, |
| 64 | const char *tz, size_t len, |
| 65 | int west_is_positive); |
| 66 | |
| 67 | #ifdef __cplusplus |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | #endif /* OSSTZPRS_H */ |