You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by da...@apache.org on 2023/02/13 15:22:36 UTC

[beam] 01/01: Better error for missing timezone info

This is an automated email from the ASF dual-hosted git repository.

damccorm pushed a commit to branch users/damccorm/tzinfo
in repository https://gitbox.apache.org/repos/asf/beam.git

commit 12293153ab67c3bee645188a0275a9a3cf68b317
Author: Danny McCormick <da...@google.com>
AuthorDate: Mon Feb 13 10:22:22 2023 -0500

    Better error for missing timezone info
---
 sdks/python/apache_beam/runners/direct/transform_evaluator.py | 5 ++++-
 sdks/python/apache_beam/utils/timestamp.py                    | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/sdks/python/apache_beam/runners/direct/transform_evaluator.py b/sdks/python/apache_beam/runners/direct/transform_evaluator.py
index a0600255028..bfb27c4adc0 100644
--- a/sdks/python/apache_beam/runners/direct/transform_evaluator.py
+++ b/sdks/python/apache_beam/runners/direct/transform_evaluator.py
@@ -678,7 +678,10 @@ class _PubSubReadEvaluator(_TransformEvaluator):
       else:
         if message.publish_time is None:
           raise ValueError('No publish time present in message: %s' % message)
-        timestamp = Timestamp.from_utc_datetime(message.publish_time)
+        try:
+          timestamp = Timestamp.from_utc_datetime(message.publish_time)
+        except ValueError as e:
+          raise ValueError('Bad timestamp value for message %s: %s', message, e)
 
       return timestamp, parsed_message
 
diff --git a/sdks/python/apache_beam/utils/timestamp.py b/sdks/python/apache_beam/utils/timestamp.py
index 502d1f78fa7..0e4f402a695 100644
--- a/sdks/python/apache_beam/utils/timestamp.py
+++ b/sdks/python/apache_beam/utils/timestamp.py
@@ -103,6 +103,8 @@ class Timestamp(object):
     Args:
       dt: A ``datetime.datetime`` object in UTC (offset-aware).
     """
+    if dt.tzinfo is None:
+      raise ValueError('dt has no timezone info (https://docs.python.org/3/library/datetime.html#datetime.tzinfo:~:text=For%20applications%20requiring,is%20in%20effect.): %s' % dt)
     if dt.tzinfo != pytz.utc and dt.tzinfo != datetime.timezone.utc:
       raise ValueError('dt not in UTC: %s' % dt)
     duration = dt - cls._epoch_datetime_utc()